Class: Qor::Dsl::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, options = {}) ⇒ Node

Returns a new instance of Node.



6
7
8
9
10
# File 'lib/qor_dsl/node.rb', line 6

def initialize(name=nil, options={})
  self.name   = name
  self.add_config(options[:config] || Qor::Dsl::Config.new('ROOT', self))
  self.dummy = options[:dummy]
end

Instance Attribute Details

#all_nodesObject

Returns the value of attribute all_nodes.



4
5
6
# File 'lib/qor_dsl/node.rb', line 4

def all_nodes
  @all_nodes
end

#blockObject

Returns the value of attribute block.



4
5
6
# File 'lib/qor_dsl/node.rb', line 4

def block
  @block
end

#childrenObject

Returns the value of attribute children.



4
5
6
# File 'lib/qor_dsl/node.rb', line 4

def children
  @children
end

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/qor_dsl/node.rb', line 4

def config
  @config
end

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/qor_dsl/node.rb', line 4

def data
  @data
end

#dummyObject

Returns the value of attribute dummy.



4
5
6
# File 'lib/qor_dsl/node.rb', line 4

def dummy
  @dummy
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/qor_dsl/node.rb', line 4

def name
  @name
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/qor_dsl/node.rb', line 4

def parent
  @parent
end

Instance Method Details

#add_child(child) ⇒ Object



21
22
23
24
25
26
# File 'lib/qor_dsl/node.rb', line 21

def add_child(child)
  child.parent = self
  children << child
  root.all_nodes ||= []
  root.all_nodes << child
end

#add_config(config) ⇒ Object



16
17
18
19
# File 'lib/qor_dsl/node.rb', line 16

def add_config(config)
  self.config = config
  config.__node = self
end

#config_nameObject

Node Config



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

def config_name
  config.__name
end

#config_optionsObject



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

def config_options
  config.__options || {} rescue {}
end

#deep_find(type = nil, name = nil, &block) ⇒ Object



102
103
104
105
106
# File 'lib/qor_dsl/node.rb', line 102

def deep_find(type=nil, name=nil, &block)
  nodes = root.all_nodes
  nodes = nodes.select {|n| n.parents.include?(self) } unless root?
  find(type, name, nodes, &block)
end

#default_blockObject



87
88
89
# File 'lib/qor_dsl/node.rb', line 87

def default_block
  config_options[:default_block]
end

#default_optionsObject



76
77
78
# File 'lib/qor_dsl/node.rb', line 76

def default_options
  config_options[:default_options]
end

#default_valueObject

Node Data



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

def default_value
  config_options[:default_value]
end

#dummy?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/qor_dsl/node.rb', line 58

def dummy?
  !!dummy
end

#find(type = nil, name = nil, nodes = children, &block) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/qor_dsl/node.rb', line 108

def find(type=nil, name=nil, nodes=children, &block)
  results = nodes.select do |child|
    child.is_node?(type, name) && (block.nil? ? true : block.call(child))
  end

  results = parent.find(type, name, &block) if results.length == 0 && child_config_options(type)[:inherit]
  process_find_results(results, type)
end

#first(type = nil, name = nil, &block) ⇒ Object

Query



97
98
99
100
# File 'lib/qor_dsl/node.rb', line 97

def first(type=nil, name=nil, &block)
  selected_children = find(type, name, &block)
  selected_children.is_a?(Array) ? selected_children[0] : selected_children
end

#inspectObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/qor_dsl/node.rb', line 118

def inspect
  obj_options = {
    'name' => name,
    'config' => config_name,
    'parent' => parent && parent.inspect_name,
    'children' => children.map(&:inspect_name),
    'data' => data,
    'block' => block
  }
  Qor::Dsl.inspect_object(self, obj_options)
end

#is_node?(node_type = nil, name_str = nil) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/qor_dsl/node.rb', line 62

def is_node?(node_type=nil, name_str=nil)
  (node_type.nil? || (config_name.to_s == node_type.to_s)) && (name_str.nil? || (name.to_s == name_str.to_s))
end

#node(type, options = {}, &blk) ⇒ Object



12
13
14
# File 'lib/qor_dsl/node.rb', line 12

def node(type, options={}, &blk)
  config.node(type, options, &blk)
end

#optionsObject



80
81
82
83
84
85
# File 'lib/qor_dsl/node.rb', line 80

def options
  return data[-1] if data.is_a?(Array) && data[-1].is_a?(Hash)
  return data if data.is_a?(Hash)
  return name if name.is_a?(Hash)
  return default_options || {}
end

#parentsObject

Node



40
41
42
# File 'lib/qor_dsl/node.rb', line 40

def parents
  parent ? [parent, parent.parents].flatten : []
end

#rootObject



50
51
52
# File 'lib/qor_dsl/node.rb', line 50

def root
  parent ? parent.root : self
end

#root?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/qor_dsl/node.rb', line 54

def root?
  root == self
end

#valueObject



72
73
74
# File 'lib/qor_dsl/node.rb', line 72

def value
  ((config.__children.size > 0 || block.nil?) ? (options[:value] || name) : block.call) || default_value
end