Module: Rumx::Bean
- Included in:
- Rumx::Beans::Error, Rumx::Beans::Folder, Rumx::Beans::Hash, Rumx::Beans::Message, Rumx::Beans::Timer, HashBean, ListBean
- Defined in:
- lib/rumx/bean.rb
Overview
Defines a Rumx bean that allows access to the defined attributes and operations. All public instance methods are prefixed with “bean_” to try to avoid collisions.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .find(name_array) ⇒ Object
-
.find_attribute(name_array) ⇒ Object
Return [bean, attribute, param_name, value] list or nil if not found.
-
.find_operation(name_array) ⇒ Object
Return [bean, operation] pair or nil if not found.
- .included(base) ⇒ Object
- .root ⇒ Object
Instance Method Summary collapse
- #bean_add_child(name, child_bean) ⇒ Object
- #bean_children ⇒ Object
- #bean_each(ancestry = []) {|_self, ancestry| ... } ⇒ Object
-
#bean_each_child(&block) ⇒ Object
Call the block for each direct child of this bean (includes the bean_children and the embedded beans).
- #bean_each_child_recursive(ancestry, &block) ⇒ Object
-
#bean_each_embedded_child(&block) ⇒ Object
Call the block for all the embedded beans.
- #bean_each_operation(&block) ⇒ Object
- #bean_each_operation_recursive(&block) ⇒ Object
- #bean_embedded(name) ⇒ Object
-
#bean_find(name_array, index = 0) ⇒ Object
Find the bean.
- #bean_get_and_set_attributes(params, ancestry = [], &block) ⇒ Object
- #bean_get_attributes(ancestry = [], &block) ⇒ Object
- #bean_has_attributes? ⇒ Boolean
- #bean_has_operations? ⇒ Boolean
-
#bean_monitor ⇒ Object
Monitor for synchronization of attributes/operations.
- #bean_remove_child(name) ⇒ Object
- #bean_set_and_get_attributes(params, ancestry = [], &block) ⇒ Object
- #bean_set_attributes(params) ⇒ Object
-
#bean_synchronize ⇒ Object
Synchronize access to attributes and operations.
Class Method Details
.find(name_array) ⇒ Object
160 161 162 |
# File 'lib/rumx/bean.rb', line 160 def self.find(name_array) root.bean_find(name_array) end |
.find_attribute(name_array) ⇒ Object
Return [bean, attribute, param_name, value] list or nil if not found
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rumx/bean.rb', line 165 def self.find_attribute(name_array) attribute_name = name_array.last name_array = name_array[0..-2] # If it's a list attribute if name.match(/^\d+$/) index = name.to_i name = name_array.pop bean = Bean.find(name_array) return nil unless bean name = name.to_sym # else just a regular attribute else bean = Bean.find(name_array) return nil unless bean name = name.to_sym bean.class.bean_attributes.each do |attribute| if name == attribute.name return [bean, attribute, attribute.name, attribute.get_value(bean)] end end end return nil end |
.find_operation(name_array) ⇒ Object
Return [bean, operation] pair or nil if not found
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/rumx/bean.rb', line 190 def self.find_operation(name_array) name = name_array.pop bean = Bean.find(name_array) return nil unless bean name = name.to_sym bean.class.bean_operations.each do |operation| return [bean, operation] if name == operation.name end return nil end |
.included(base) ⇒ Object
152 153 154 |
# File 'lib/rumx/bean.rb', line 152 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#bean_add_child(name, child_bean) ⇒ Object
219 220 221 222 223 |
# File 'lib/rumx/bean.rb', line 219 def bean_add_child(name, child_bean) bean_synchronize do bean_children[name.to_sym] = child_bean end end |
#bean_children ⇒ Object
214 215 216 217 |
# File 'lib/rumx/bean.rb', line 214 def bean_children # TODO: How to initialize this in a module and avoid race condition? @bean_children ||= {} end |
#bean_each(ancestry = []) {|_self, ancestry| ... } ⇒ Object
240 241 242 243 244 245 |
# File 'lib/rumx/bean.rb', line 240 def bean_each(ancestry=[], &block) yield self, ancestry bean_each_child_recursive(ancestry) do |child_bean, child_ancestry| yield child_bean, child_ancestry end end |
#bean_each_child(&block) ⇒ Object
Call the block for each direct child of this bean (includes the bean_children and the embedded beans)
258 259 260 261 262 263 264 265 |
# File 'lib/rumx/bean.rb', line 258 def bean_each_child(&block) bean_children.each do |name, bean| yield name, bean end do |name, bean| yield name, bean end end |
#bean_each_child_recursive(ancestry, &block) ⇒ Object
247 248 249 250 251 252 253 254 255 |
# File 'lib/rumx/bean.rb', line 247 def bean_each_child_recursive(ancestry, &block) child_ancestry = ancestry.dup # Save some object creation child_index = child_ancestry.size bean_each_child do |name, bean| child_ancestry[child_index] = name bean.bean_each(child_ancestry, &block) end end |
#bean_each_embedded_child(&block) ⇒ Object
Call the block for all the embedded beans
268 269 270 271 272 273 274 275 276 277 |
# File 'lib/rumx/bean.rb', line 268 def (&block) self.class..each do |name, bean_klass| bean = send(name) if bean # bean_klass is either ListBean or HashBean, otherwise we already have our bean bean = bean_klass.new(bean) if bean_klass yield name, bean end end end |
#bean_each_operation(&block) ⇒ Object
328 329 330 331 332 |
# File 'lib/rumx/bean.rb', line 328 def bean_each_operation(&block) self.class.bean_operations.each do |operation| yield operation end end |
#bean_each_operation_recursive(&block) ⇒ Object
334 335 336 337 338 339 340 341 342 343 |
# File 'lib/rumx/bean.rb', line 334 def bean_each_operation_recursive(&block) bean_each do |bean, ancestry| operation_ancestry = ancestry.dup index = operation_ancestry.size bean.class.bean_operations.each do |operation| operation_ancestry[index] = operation.name yield operation, operation_ancestry end end end |
#bean_embedded(name) ⇒ Object
279 280 281 282 283 284 285 286 287 |
# File 'lib/rumx/bean.rb', line 279 def (name) return nil unless self.class..key?(name) bean = send(name) if bean bean_klass = self.class.[name] bean = bean_klass.new(bean) if bean_klass end return bean end |
#bean_find(name_array, index = 0) ⇒ Object
Find the bean
232 233 234 235 236 237 238 |
# File 'lib/rumx/bean.rb', line 232 def bean_find(name_array, index = 0) return self if index == name_array.size name = name_array[index].to_sym child_bean = bean_children[name] || (name) return nil unless child_bean return child_bean.bean_find(name_array, index+1) end |
#bean_get_and_set_attributes(params, ancestry = [], &block) ⇒ Object
309 310 311 312 313 314 315 |
# File 'lib/rumx/bean.rb', line 309 def bean_get_and_set_attributes(params, ancestry=[], &block) bean_synchronize do val = do_bean_get_attributes(ancestry, &block) do_bean_set_attributes(params) val end end |
#bean_get_attributes(ancestry = [], &block) ⇒ Object
297 298 299 300 301 |
# File 'lib/rumx/bean.rb', line 297 def bean_get_attributes(ancestry=[], &block) bean_synchronize do do_bean_get_attributes(ancestry, &block) end end |
#bean_has_attributes? ⇒ Boolean
289 290 291 292 293 294 295 |
# File 'lib/rumx/bean.rb', line 289 def bean_has_attributes? return true unless self.class.bean_attributes.empty? do |name, bean| return true if bean.bean_has_attributes? end return false end |
#bean_has_operations? ⇒ Boolean
324 325 326 |
# File 'lib/rumx/bean.rb', line 324 def bean_has_operations? !self.class.bean_operations.empty? end |
#bean_monitor ⇒ Object
Monitor for synchronization of attributes/operations
202 203 204 205 |
# File 'lib/rumx/bean.rb', line 202 def bean_monitor # TODO: How to initialize this in a module and avoid race condition? @monitor ||= Monitor.new end |
#bean_remove_child(name) ⇒ Object
225 226 227 228 229 |
# File 'lib/rumx/bean.rb', line 225 def bean_remove_child(name) bean_synchronize do bean_children.delete(name.to_sym) end end |
#bean_set_and_get_attributes(params, ancestry = [], &block) ⇒ Object
317 318 319 320 321 322 |
# File 'lib/rumx/bean.rb', line 317 def bean_set_and_get_attributes(params, ancestry=[], &block) bean_synchronize do do_bean_set_attributes(params) do_bean_get_attributes(ancestry, &block) end end |
#bean_set_attributes(params) ⇒ Object
303 304 305 306 307 |
# File 'lib/rumx/bean.rb', line 303 def bean_set_attributes(params) bean_synchronize do do_bean_set_attributes(params) end end |
#bean_synchronize ⇒ Object
Synchronize access to attributes and operations
208 209 210 211 212 |
# File 'lib/rumx/bean.rb', line 208 def bean_synchronize bean_monitor.synchronize do yield end end |