Class: Epuber::DSL::TreeObject

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/dsl/tree_object.rb

Direct Known Subclasses

Book::Target, Book::TocItem

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Object

#file_path

Instance Method Summary collapse

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.

Parameters:



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_objectSelf

Returns:

  • (Self)


63
64
65
# File 'lib/epuber/dsl/tree_object.rb', line 63

def current_parent_object
  @current_parent_object
end

Instance Attribute Details

#parentself

Returns reference to parent.

Returns:

  • (self)

    reference to parent



28
29
30
# File 'lib/epuber/dsl/tree_object.rb', line 28

def parent
  @parent
end

#sub_itemsArray<self>

Returns child items.

Returns:

  • (Array<self>)

    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

Yields:

  • (child_item)

Yield Parameters:

  • child_item (self)

    created child item

Returns:

  • (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.

Yields:

  • (_self)

Yield Parameters:

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_itemsArray<self>

Returns child items.

Returns:

  • (Array<self>)

    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

#freezeObject

Returns nil.

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.

Returns:

  • (Bool)

    receiver is root



49
50
51
# File 'lib/epuber/dsl/tree_object.rb', line 49

def root?
  @parent.nil?
end

#validateObject

Returns nil.

Returns:

  • nil



55
56
57
58
# File 'lib/epuber/dsl/tree_object.rb', line 55

def validate
  super
  sub_items.each(&:validate)
end