Class: Wx::SF::ShapeList
- Inherits:
-
Object
- Object
- Wx::SF::ShapeList
- Includes:
- Enumerable, FIRM::Serializable
- Defined in:
- lib/wx/shapes/shape_list.rb
Overview
This class implements an indexed container for unique, non-nil, shapes (no duplicates).
Instance Method Summary collapse
-
#all(collection = []) ⇒ Object
Recursively collects shapes and returns collection.
-
#append(shape) ⇒ self
(also: #push, #<<)
Appends a new shape to the list if not yet in list.
-
#clear ⇒ self
Empties the shape list.
-
#delete(shape) ⇒ Shape?
Removes the given shape from the list and returns that.
-
#each {|shape| ... } ⇒ self
Iterates over contained shapes.
-
#empty? ⇒ Boolean
Returns true if no shapes are contained, false otherwise.
-
#include?(shape, recursive = false) ⇒ Boolean
Returns true if the given shape is included in the list.
-
#initialize(enum = nil) ⇒ ShapeList
constructor
Constructor.
Constructor Details
#initialize(enum = nil) ⇒ ShapeList
Constructor.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/wx/shapes/shape_list.rb', line 19 def initialize(enum = nil) if enum if enum.is_a?(ShapeList) @list = enum.instance_variable_get('@list').dup elsif enum.is_a?(::Enumerable) @list = ::Set.new enum.each { |elem| self << elem } else @list = ::Set.new self << enum end else @list = ::Set.new end end |
Instance Method Details
#all(collection = []) ⇒ Object
Recursively collects shapes and returns collection.
52 53 54 |
# File 'lib/wx/shapes/shape_list.rb', line 52 def all(collection = []) @list.inject(collection.concat(@list.to_a)) { |list, shape| shape.instance_variable_get('@child_shapes').all(list) } end |
#append(shape) ⇒ self Also known as: push, <<
Appends a new shape to the list if not yet in list. Does not perform a recursive check.
73 74 75 76 77 78 |
# File 'lib/wx/shapes/shape_list.rb', line 73 def append(shape) unless @list.include?(check_elem(shape)) @list << shape end self end |
#clear ⇒ self
Empties the shape list.
64 65 66 67 |
# File 'lib/wx/shapes/shape_list.rb', line 64 def clear @list.clear self end |
#delete(shape) ⇒ Shape?
Removes the given shape from the list and returns that.
85 86 87 88 89 90 91 92 |
# File 'lib/wx/shapes/shape_list.rb', line 85 def delete(shape) if @list.include?(check_elem(shape)) @list.delete(shape) shape else nil end end |
#each {|shape| ... } ⇒ self
Iterates over contained shapes. When a block given, passes each successive shape to the block. Allows the array to be modified during iteration. When no block given, returns a new ::Enumerator.
41 42 43 44 45 46 47 48 |
# File 'lib/wx/shapes/shape_list.rb', line 41 def each(&block) if block_given? @list.each(&block) self else @list.each end end |
#empty? ⇒ Boolean
Returns true if no shapes are contained, false otherwise.
58 59 60 |
# File 'lib/wx/shapes/shape_list.rb', line 58 def empty? @list.empty? end |
#include?(shape, recursive = false) ⇒ Boolean
Returns true if the given shape is included in the list. Performs a recursive search in case :recursive is true.
99 100 101 102 |
# File 'lib/wx/shapes/shape_list.rb', line 99 def include?(shape, recursive = false) found = @list.include?(check_elem(shape)) found || (recursive && @list.any? { |child| child.include_child_shape?(shape, recursive) }) end |