Class: Epuber::DSL::TreeObject
- Defined in:
- lib/epuber/dsl/tree_object.rb
Direct Known Subclasses
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#parent ⇒ self
readonly
Reference to parent.
-
#sub_items ⇒ Array<self>
readonly
Child items.
Attributes inherited from Object
Instance Method Summary collapse
- #create_child_item(*args) {|child_item| ... } ⇒ self
-
#create_child_items {|_self| ... } ⇒ Object
Nil.
-
#flat_sub_items ⇒ Array<self>
Child items.
-
#freeze ⇒ Object
Nil.
-
#initialize(parent = nil) ⇒ TreeObject
constructor
A new instance of TreeObject.
-
#root? ⇒ Bool
Receiver is root.
-
#validate ⇒ Object
Nil.
Methods inherited from Object
from_file, #from_file?, from_string, #to_s
Methods included from AttributeSupport
#attribute, #define_method_attr, #dsl_attributes, #find_root
Constructor Details
#initialize(parent = nil) ⇒ TreeObject
Returns a new instance of TreeObject.
10 11 12 13 14 15 16 17 |
# File 'lib/epuber/dsl/tree_object.rb', line 10 def initialize(parent = nil) super() @parent = parent @sub_items = [] parent.sub_items << self unless parent.nil? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Epuber::DSL::Object
Class Attribute Details
.current_parent_object ⇒ Self
63 64 65 |
# File 'lib/epuber/dsl/tree_object.rb', line 63 def current_parent_object @current_parent_object end |
Instance Attribute Details
#parent ⇒ self
Returns reference to parent.
28 29 30 |
# File 'lib/epuber/dsl/tree_object.rb', line 28 def parent @parent end |
#sub_items ⇒ Array<self>
Returns child items.
32 33 34 |
# File 'lib/epuber/dsl/tree_object.rb', line 32 def sub_items @sub_items end |
Instance Method Details
#create_child_item(*args) {|child_item| ... } ⇒ self
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/epuber/dsl/tree_object.rb', line 71 def create_child_item(*args) child = self.class.new(*args) parent_object_before = self.class.current_parent_object child.parent = parent_object_before || self child.parent.sub_items << child self.class.current_parent_object = child yield child if block_given? self.class.current_parent_object = parent_object_before child end |
#create_child_items {|_self| ... } ⇒ Object
Returns nil.
90 91 92 |
# File 'lib/epuber/dsl/tree_object.rb', line 90 def create_child_items yield self if block_given? end |
#flat_sub_items ⇒ Array<self>
Returns child items.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/epuber/dsl/tree_object.rb', line 36 def flat_sub_items all = [] sub_items.each do |item| all << item all.concat(item.flat_sub_items) end all end |
#freeze ⇒ Object
Returns nil.
21 22 23 24 |
# File 'lib/epuber/dsl/tree_object.rb', line 21 def freeze super @sub_items.freeze end |
#root? ⇒ Bool
Returns receiver is root.
49 50 51 |
# File 'lib/epuber/dsl/tree_object.rb', line 49 def root? @parent.nil? end |
#validate ⇒ Object
Returns nil.
55 56 57 58 |
# File 'lib/epuber/dsl/tree_object.rb', line 55 def validate super sub_items.each(&:validate) end |