Module: Tengine::Support::Config::Definition::HasManyChildren
Instance Method Summary collapse
- #[](child_name) ⇒ Object
- #[]=(child_name, value) ⇒ Object
- #accept_visitor(visitor) ⇒ Object
- #action(name, *args, &block) ⇒ Object (also: #__action__)
- #action? ⇒ Boolean
- #add(name, klass, options = {}, &block) ⇒ Object
- #child_by_name(name) ⇒ Object
- #children ⇒ Object
- #field(name, *args, &block) ⇒ Object
- #find(name_array) ⇒ Object
- #get_value(obj) ⇒ Object
- #group(name, options = {}, &block) ⇒ Object
- #ignore(*names) ⇒ Object
- #load(hash) ⇒ Object
- #load_config(name, *args) ⇒ Object
- #name_array ⇒ Object
- #separator(description) ⇒ Object
- #separator? ⇒ Boolean
- #to_hash ⇒ Object
Instance Method Details
#[](child_name) ⇒ Object
179 180 181 182 183 184 185 186 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 179 def [](child_name) child = child_by_name(child_name) if child.is_a?(Tengine::Support::Config::Definition::Field) self.send(child_name) else child end end |
#[]=(child_name, value) ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 188 def []=(child_name, value) child = child_by_name(child_name) if child.is_a?(Tengine::Support::Config::Definition::Field) self.send("#{child_name}=", value) else raise ArgumentError, "can't replace #{child_name.inspect}" end end |
#accept_visitor(visitor) ⇒ Object
167 168 169 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 167 def accept_visitor(visitor) visitor.visit(self) end |
#action(name, *args, &block) ⇒ Object Also known as: __action__
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 102 def action(name, *args, &block) attrs = args.last.is_a?(Hash) ? args.pop : {} attrs.update({ :__name__ => name, :__parent__ => self, :__block__ => block, :__type__ => :action, }) attrs[:description] = args.first unless args.empty? field = Tengine::Support::Config::Definition::Field.new(attrs) self.children << field end |
#action? ⇒ Boolean
116 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 116 def action?; false; end |
#add(name, klass, options = {}, &block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 24 def add(name, klass, = {}, &block) result = klass.new result.__parent__ = self result.__name__ = name result.instantiate_children dependencies = [:dependencies] || {} klass.definition_reference_names.each do |res_name| name_array = dependencies[res_name] raise "missing dependency of #{name.inspect} in :dependencies options to add(#{name.inspect}, #{klass.name}...)" unless name_array obj = root.find(Array(name_array)) raise "#{name_array.inspect} not found" unless obj result.send("#{res_name}=", obj) end ([:parameters] || {}).each do |param, value| result.send("#{param}=", value.respond_to?(:to_proc) ? result.instance_eval(&value) : value) end defaults = [:defaults] || {} defaults.each do |key, value| child = result.child_by_name(key) raise "child not found for #{key.inspct} in #{result.__name__}" unless child child.default = value if value end children << result (class << self; self; end).class_eval{ define_method(name){ result } } result.instance_eval(&block) if block result end |
#child_by_name(name) ⇒ Object
9 10 11 12 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 9 def child_by_name(name) name = name.to_sym if name.respond_to?(:to_sym) children.detect{|child| child.__name__ == name} end |
#children ⇒ Object
5 6 7 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 5 def children @children ||= [] end |
#field(name, *args, &block) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 66 def field(name, *args, &block) attrs = args.last.is_a?(Hash) ? args.pop : {} attrs[:description] = args.first unless args.empty? attrs.update({ :__name__ => name, :__parent__ => self, :__type__ => attrs[:__type__] || :field, :convertor => block, }) if field = children.detect{|child| child.__name__ == name} new_field = field.dup new_field.update(attrs) idx = self.children.index(field) self.children[idx] = new_field field = new_field else field = Tengine::Support::Config::Definition::Field.new(attrs) self.children << field end (class << self; self; end).module_eval do define_method(field.__name__) do instance_variable_get("@#{field.__name__}") || (field.default.respond_to?(:to_proc) ? self.instance_eval(&field.deault) : field.default) end define_method("#{field.__name__}=") do |value| instance_variable_set("@#{field.__name__}", field.convert(value, self)) end end end |
#find(name_array) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 14 def find(name_array) name_array = Array(name_array) head = name_array.shift if child = child_by_name(head) name_array.empty? ? child : child.find(name_array) else nil end end |
#get_value(obj) ⇒ Object
175 176 177 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 175 def get_value(obj) obj.is_a?(Proc) ? self.instance_eval(&obj) : obj end |
#group(name, options = {}, &block) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 57 def group(name, = {}, &block) result = Tengine::Support::Config::Definition::Group.new(name, ) result.__parent__ = self (class << self; self; end).class_eval{ define_method(name){ result } } children << result result.instance_eval(&block) if block result end |
#ignore(*names) ⇒ Object
96 97 98 99 100 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 96 def ignore(*names) @ignoreds ||= [] names = names.flatten.map(&:to_sym) @ignoreds.concat(names) end |
#load(hash) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 150 def load(hash) hash.each do |name, value| name = name.to_sym next if @ignoreds && @ignoreds.include?(name) child = child_by_name(name) unless child where = respond_to?(:__name__) ? " on " + __name__.inspect : "" raise "child not found for #{name.to_s.inspect}#{where}" end if child.is_a?(Tengine::Support::Config::Definition::Field) self.send("#{name}=", value) else child.load(value) end end end |
#load_config(name, *args) ⇒ Object
131 132 133 134 135 136 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 131 def load_config(name, *args) = args.last.is_a?(Hash) ? args.pop : {} [:type] = :load_config args << field(name, *args) end |
#name_array ⇒ Object
171 172 173 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 171 def name_array (__parent__ ? __parent__.name_array : []) + [__name__] end |
#separator(description) ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 118 def separator(description) attrs = { :description => description, :__name__ => :"separator#{children.count + 1}", :__parent__ => self, :__type__ => :separator, } field = Tengine::Support::Config::Definition::Field.new(attrs) self.children << field end |
#separator? ⇒ Boolean
129 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 129 def separator?; false; end |
#to_hash ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/tengine/support/config/definition/has_many_children.rb', line 138 def to_hash children.inject({}) do |dest, child| unless child.action? || child.separator? value = child.to_hash unless value.is_a?(Hash) && value.empty? dest[child.__name__] = child.to_hash end end dest end end |