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

#optionsObject

Returns the value of attribute options.



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

def options
  @options
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



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

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

#add_config(config) ⇒ Object



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

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

#child_config(type) ⇒ Object



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

def child_config(type)
  config.__children[type] || nil
end

#child_config_options(type) ⇒ Object



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

def child_config_options(type)
  child_config(type).__options || {} rescue {}
end

#config_nameObject

Node Config



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

def config_name
  config.__name
end

#config_optionsObject



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

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

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



88
89
90
91
92
# File 'lib/qor_dsl/node.rb', line 88

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

#dummy?Boolean

Returns:

  • (Boolean)


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

def dummy?
  dummy
end

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



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/qor_dsl/node.rb', line 94

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]
  results = process_find_results(results, type)

  return results[0] if !name.nil? && results.is_a?(Array) && results.length == 1

  results
end

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



107
108
109
110
# File 'lib/qor_dsl/node.rb', line 107

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

#inspect_nameObject

Inspect



113
114
115
# File 'lib/qor_dsl/node.rb', line 113

def inspect_name
  "{#{config_name}: #{name || 'nil'}}"
end

#is_node?(cname = nil, sname = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_node?(cname=nil, sname=nil)
  (cname.nil? || (config_name.to_s == cname.to_s)) && (sname.nil? || (name.to_s == sname.to_s))
end

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



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

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

#parentsObject



45
46
47
# File 'lib/qor_dsl/node.rb', line 45

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

#rootObject



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

def root
  parent ? parent.root : self
end

#root?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/qor_dsl/node.rb', line 37

def root?
  root == self
end

#to_sObject



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

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

#valueObject



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

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