Class: Bemer::Tree

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/bemer/tree.rb,
lib/bemer/tree/node.rb,
lib/bemer/tree/base_node.rb,
lib/bemer/tree/text_node.rb

Defined Under Namespace

Classes: BaseNode, Node, TextNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_catalog, **params) ⇒ Tree

Returns a new instance of Tree.



19
20
21
22
23
24
25
# File 'lib/bemer/tree.rb', line 19

def initialize(template_catalog, **params)
  @node_metadata = {}
  @params        = params
  @parent_node   = nil
  @pipeline      = Pipeline.new(template_catalog)
  @root_nodes    = []
end

Instance Attribute Details

#node_metadataObject (readonly)

Returns the value of attribute node_metadata.



17
18
19
# File 'lib/bemer/tree.rb', line 17

def 
  @node_metadata
end

#parent_nodeObject

Returns the value of attribute parent_node.



16
17
18
# File 'lib/bemer/tree.rb', line 16

def parent_node
  @parent_node
end

#pipelineObject (readonly)

Returns the value of attribute pipeline.



17
18
19
# File 'lib/bemer/tree.rb', line 17

def pipeline
  @pipeline
end

Instance Method Details

#add(node) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bemer/tree.rb', line 53

def add(node)
  if need_replace_parent_node?
    parent_node.replacers << node
  else
    (node.object_id)

    parent_node.nil? ? root_nodes << node : parent_node.children << node
  end

  nil
end

#add_node(block = '', element = nil, bem_cascade: nil, **options, &content) ⇒ Object



65
66
67
68
69
70
# File 'lib/bemer/tree.rb', line 65

def add_node(block = '', element = nil, bem_cascade: nil, **options, &content)
  bem_cascade = inherited_bem_cascade if bem_cascade.nil?
  new_options = { **params, bem_cascade: bem_cascade, **options }

  add Node.new(self, block, element, new_options, &content)
end

#add_text_node(content = nil, &callback) ⇒ Object



72
73
74
# File 'lib/bemer/tree.rb', line 72

def add_text_node(content = nil, &callback)
  add TextNode.new(self, content, &callback)
end


49
50
51
# File 'lib/bemer/tree.rb', line 49

def print
  root_nodes.each(&:print)
end

#render(&block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/bemer/tree.rb', line 27

def render(&block)
  return unless block_given?

  builder = Builders::Tree.new(self)
  output  = ::ActionView::OutputBuffer.new

  output << block.binding.receiver.capture(builder, &block)
  output << render_root_nodes
end

#replace(node) ⇒ Object

rubocop:disable Metrics/AbcSize



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bemer/tree.rb', line 37

def replace(node) # rubocop:disable Metrics/AbcSize
  siblings = parent_node.nil? ? root_nodes : parent_node.children
   = .delete(node.object_id)
  position = [:position]
  offset   = position + node.replacers.length

  (position, [:last], node.replacers)
  (offset, siblings[position..-1])

  siblings[position - 1...position] = node.replacers
end