Class: Bemer::Tree::Node

Inherits:
BaseNode show all
Defined in:
lib/bemer/tree/node.rb

Instance Attribute Summary collapse

Attributes inherited from BaseNode

#entity, #entity_builder, #tree

Instance Method Summary collapse

Methods inherited from BaseNode

#render

Constructor Details

#initialize(tree, block = '', element = nil, **options, &content) ⇒ Node

Returns a new instance of Node.



14
15
16
17
18
19
20
21
22
23
# File 'lib/bemer/tree/node.rb', line 14

def initialize(tree, block = '', element = nil, **options, &content)
  super(tree, block, element, options, &content)

  @applied_modes    = Pipeline::MODES.map { |mode| [mode, false] }.to_h
  @children         = []
  @content_replaced = false
  @need_replace     = false
  @params           = tree.parent_node.nil? ? {} : Hash[tree.parent_node.params]
  @replacers        = []
end

Instance Attribute Details

#applied_modesObject (readonly)

Returns the value of attribute applied_modes.



9
10
11
# File 'lib/bemer/tree/node.rb', line 9

def applied_modes
  @applied_modes
end

#childrenObject (readonly)

Returns the value of attribute children.



9
10
11
# File 'lib/bemer/tree/node.rb', line 9

def children
  @children
end

#content_replacedObject Also known as: content_replaced?

Returns the value of attribute content_replaced.



8
9
10
# File 'lib/bemer/tree/node.rb', line 8

def content_replaced
  @content_replaced
end

#need_replaceObject Also known as: need_replace?

Returns the value of attribute need_replace.



8
9
10
# File 'lib/bemer/tree/node.rb', line 8

def need_replace
  @need_replace
end

#paramsObject

Returns the value of attribute params.



8
9
10
# File 'lib/bemer/tree/node.rb', line 8

def params
  @params
end

#replacersObject (readonly)

Returns the value of attribute replacers.



9
10
11
# File 'lib/bemer/tree/node.rb', line 9

def replacers
  @replacers
end

Instance Method Details

#add_child_nodesObject



37
38
39
40
41
42
43
# File 'lib/bemer/tree/node.rb', line 37

def add_child_nodes
  return content unless content.respond_to?(:call)

  builder = Builders::Tree::Element.new(tree, block) if block?

  content.binding.receiver.capture(builder, &content)
end

#apply(mode, template, **params) ⇒ Object



68
69
70
# File 'lib/bemer/tree/node.rb', line 68

def apply(mode, template, **params)
  tree.pipeline.apply(mode, template, self, params)
end

#apply_next(template, **params) ⇒ Object



64
65
66
# File 'lib/bemer/tree/node.rb', line 64

def apply_next(template, **params)
  tree.pipeline.apply_next(template, self, params)
end

#first?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/bemer/tree/node.rb', line 29

def first?
  position.eql?(1)
end

#last?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/bemer/tree/node.rb', line 25

def last?
  tree.[object_id][:last]
end

#positionObject



33
34
35
# File 'lib/bemer/tree/node.rb', line 33

def position
  tree.[object_id][:position]
end


56
57
58
59
60
61
62
# File 'lib/bemer/tree/node.rb', line 56

def print(level = 0)
  super(level)

  children.each do |node|
    node.print(level + 1)
  end
end

#replace_parent_and_executeObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/bemer/tree/node.rb', line 45

def replace_parent_and_execute
  return unless block_given?

  old_parent_node  = tree.parent_node
  tree.parent_node = self

  yield
ensure
  tree.parent_node = old_parent_node
end