class Easydsl::Node

Public Class Methods

new(name, args, index, parent, node_builders = []) click to toggle source
# File lib/easydsl/node.rb, line 6
def initialize(name, args, index, parent, node_builders = [])
  @name = name.to_s.chomp('!').to_sym
  @singleton = name[-1] == '!'
  @args = args
  @index = index
  @parent = parent
  add_hierarchy(self, node_builders, 0)
end

Public Instance Methods

define(&block) click to toggle source
# File lib/easydsl/node.rb, line 49
def define(&block)
  add_block(&block)
end
get_all_nodes() click to toggle source
# File lib/easydsl/node.rb, line 19
def get_all_nodes
  all = []
  nodes.each { |k, _v| all += nodes[k] }
  all.sort! { |a, b| a.get_index <=> b.get_index }
end
get_args() click to toggle source
# File lib/easydsl/node.rb, line 29
def get_args
  @args
end
get_index() click to toggle source
# File lib/easydsl/node.rb, line 41
def get_index
  @index
end
get_max_index() click to toggle source
# File lib/easydsl/node.rb, line 37
def get_max_index
  get_all_nodes.map(&:get_index).max || 0
end
get_name() click to toggle source
# File lib/easydsl/node.rb, line 25
def get_name
  @name
end
get_nodes() click to toggle source
# File lib/easydsl/node.rb, line 15
def get_nodes
  @nodes ||= Hash.new { |h, k| h[k] = NodeArray.new }
end
get_parent() click to toggle source
# File lib/easydsl/node.rb, line 45
def get_parent
  @parent
end
is_singleton?() click to toggle source
# File lib/easydsl/node.rb, line 53
def is_singleton?
  @singleton
end
set_args(value) click to toggle source
# File lib/easydsl/node.rb, line 33
def set_args(value)
  @args = value
end

Protected Instance Methods

add_block(&block) click to toggle source
# File lib/easydsl/node.rb, line 69
def add_block(&block)
  tree = NodeBuilder.new('tree')
  tree.instance_exec(&block)
  add_hierarchy(self, tree.get_nodes, get_max_index)
end
add_child(name, args, index, parent, node_builders = []) click to toggle source
# File lib/easydsl/node.rb, line 75
def add_child(name, args, index, parent, node_builders = [])
  node = handle_singleton(name, args, node_builders)
  return node unless node.nil?
  node = Node.new(name, args, index, parent, node_builders)
  nodes[node.get_name] << node
  node
end
add_hierarchy(to, tree, base_index) click to toggle source
# File lib/easydsl/node.rb, line 63
def add_hierarchy(to, tree, base_index)
  tree.each_with_index do |item, index|
    to.add_child(item.get_name, item.get_args, base_index + index, to, item.get_nodes)
  end
end
clean_method_symbol(method_symbol) click to toggle source
# File lib/easydsl/node.rb, line 92
def clean_method_symbol(method_symbol)
  method_symbol.to_s.gsub(/[=?]/, '').to_sym
end
handle_assignment(method_symbol, *args) click to toggle source
# File lib/easydsl/node.rb, line 123
def handle_assignment(method_symbol, *args)
  collection = nodes[method_symbol]
  if collection.count > 0
    collection.first.set_args(args)
  else
    add_child(method_symbol, args, get_max_index, self)
  end
end
handle_block(method_symbol, *args, &block) click to toggle source
# File lib/easydsl/node.rb, line 100
def handle_block(method_symbol, *args, &block)
  collection = nodes[method_symbol]
  child = if collection.count > 0
    collection.first
  else
    add_child(method_symbol, args, get_max_index, self)
  end
  child.add_block(&block)
end
handle_brackets(_method_symbol, *args) click to toggle source
# File lib/easydsl/node.rb, line 110
def handle_brackets(_method_symbol, *args)
  @args.first[args.first]
end
handle_node(method_symbol, *_args) click to toggle source
# File lib/easydsl/node.rb, line 141
def handle_node(method_symbol, *_args)
  return nodes[method_symbol].first.value_or_self if nodes[method_symbol].count > 0
  singular = singular_method_symbol(method_symbol)
  return nodes[singular] if method_symbol != singular
  nil
end
handle_operators(method_symbol, *args) click to toggle source
# File lib/easydsl/node.rb, line 114
def handle_operators(method_symbol, *args)
  method_symbol_wo = clean_method_symbol(method_symbol)
  case method_symbol[-1]
  when ']' then handle_brackets(method_symbol_wo, *args)
  when '=' then handle_assignment(method_symbol_wo, *args)
  when '?' then handle_question(method_symbol_wo, *args)
  end
end
handle_question(method_symbol, *_args) click to toggle source
# File lib/easydsl/node.rb, line 132
def handle_question(method_symbol, *_args)
  if nodes[method_symbol].count > 0
    true
  else
    singular = singular_method_symbol(method_symbol)
    method_symbol != singular ? nodes[singular].count > 0 : false
  end
end
handle_singleton(name, args, node_builders = []) click to toggle source
# File lib/easydsl/node.rb, line 83
def handle_singleton(name, args, node_builders = [])
  return nil unless nodes.key?(name)
  node = nodes[name].first
  return nil if node.nil? || node.is_singleton? == false
  node.set_args(args)
  add_hierarchy(node, node_builders, node.get_max_index)
  nodes[name].first
end
method_missing(method_symbol, *args, &block) click to toggle source
# File lib/easydsl/node.rb, line 153
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
nodes() click to toggle source
# File lib/easydsl/node.rb, line 59
def nodes
  get_nodes
end
singular_method_symbol(method_symbol) click to toggle source
# File lib/easydsl/node.rb, line 96
def singular_method_symbol(method_symbol)
  method_symbol.to_s.singularize.to_sym
end
value_or_self() click to toggle source
# File lib/easydsl/node.rb, line 148
def value_or_self
  return @args.first if nodes.count == 0 && @args.count == 1 && @args.first.class != Node
  self
end