Class: Easydsl::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/easydsl/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, args, index, parent, node_builders = []) ⇒ Node

Returns a new instance of Node.



9
10
11
12
13
14
15
16
17
18
# File 'lib/easydsl/node.rb', line 9

def initialize(name, args, index, parent, node_builders = [])
  @name = name.to_s.chomp('!').to_sym
  @singleton = name[-1] == '!'
  @args = args
  @index = index
  @parent = parent
  node_builders.each_with_index do |item, i|
    add_child(item.name, item.args, i, self, item.nodes)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *args, &block) ⇒ Object (protected)



130
131
132
133
134
# File 'lib/easydsl/node.rb', line 130

def method_missing(method_symbol, *args, &block)
  return handle_block(method_symbol, *args, &block) if block_given?
  return handle_operators(method_symbol, *args) if method_symbol.to_s.end_with?('=', '?', '[]')
  handle_node(method_symbol, *args, &block)
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



7
8
9
# File 'lib/easydsl/node.rb', line 7

def args
  @args
end

#indexObject (readonly)

Returns the value of attribute index.



6
7
8
# File 'lib/easydsl/node.rb', line 6

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/easydsl/node.rb', line 6

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/easydsl/node.rb', line 6

def parent
  @parent
end

#singletonObject (readonly)

Returns the value of attribute singleton.



6
7
8
# File 'lib/easydsl/node.rb', line 6

def singleton
  @singleton
end

Instance Method Details

#all_nodesObject



24
25
26
27
28
# File 'lib/easydsl/node.rb', line 24

def all_nodes
  all = []
  nodes.each { |k, _v| all += nodes[k] }
  all.sort! { |a, b| a.index <=> b.index }
end

#define(&block) ⇒ Object



34
35
36
# File 'lib/easydsl/node.rb', line 34

def define(&block)
  add_block(&block)
end

#max_indexObject



30
31
32
# File 'lib/easydsl/node.rb', line 30

def max_index
  all_nodes.map(&:index).max || 0
end

#nodesObject



20
21
22
# File 'lib/easydsl/node.rb', line 20

def nodes
  @nodes ||= Hash.new { |h, k| h[k] = NodeArray.new }
end