Module: Rumx::Bean

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

Constant Summary collapse

@@root_hash =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_root(name, bean) ⇒ Object



158
159
160
# File 'lib/rumx/bean.rb', line 158

def self.add_root(name, bean)
  @@root_hash[name.to_sym] = bean
end

.find(name_array) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/rumx/bean.rb', line 174

def self.find(name_array)
  bean = root.bean_find(name_array)
  return bean if bean
  my_root = root(name_array[0])
  return nil unless my_root
  return my_root.bean_find(name_array, 1)
end

.find_operation(name_array) ⇒ Object

Return [bean, operation] pair or nil if not found



183
184
185
186
187
188
189
190
191
192
# File 'lib/rumx/bean.rb', line 183

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



154
155
156
# File 'lib/rumx/bean.rb', line 154

def self.included(base)
  base.extend(ClassMethods)
end

.remove_root(name) ⇒ Object



162
163
164
# File 'lib/rumx/bean.rb', line 162

def self.remove_root(name)
  @@root_hash.delete(name.to_sym)
end

.root(name = nil) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/rumx/bean.rb', line 166

def self.root(name = nil)
  if name
    @@root_hash[name.to_sym]
  else
    @root ||= Beans::Folder.new
  end
end

Instance Method Details

#bean_add_child(name, child_bean) ⇒ Object



212
213
214
215
216
# File 'lib/rumx/bean.rb', line 212

def bean_add_child(name, child_bean)
  bean_synchronize do
    bean_children[name.to_sym] = child_bean
  end
end

#bean_childrenObject



207
208
209
210
# File 'lib/rumx/bean.rb', line 207

def bean_children
  # TODO: How to initialize this in a module and avoid race condition?
  @bean_children ||= {}
end

#bean_each(ancestry = []) {|_self, ancestry| ... } ⇒ Object

Yields:

  • (_self, ancestry)

Yield Parameters:

  • _self (Rumx::Bean)

    the object that the method was called on



233
234
235
236
237
238
# File 'lib/rumx/bean.rb', line 233

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)



251
252
253
254
255
256
257
258
# File 'lib/rumx/bean.rb', line 251

def bean_each_child(&block)
  bean_children.each do |name, bean|
    yield name, bean
  end
  bean_each_embedded_child do |name, bean|
    yield name, bean
  end
end

#bean_each_child_recursive(ancestry, &block) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'lib/rumx/bean.rb', line 240

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



261
262
263
264
265
266
267
268
269
270
# File 'lib/rumx/bean.rb', line 261

def bean_each_embedded_child(&block)
  self.class.bean_embeds.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



321
322
323
324
325
# File 'lib/rumx/bean.rb', line 321

def bean_each_operation(&block)
  self.class.bean_operations.each do |operation|
    yield operation
  end
end

#bean_each_operation_recursive(&block) ⇒ Object



327
328
329
330
331
332
333
334
335
336
# File 'lib/rumx/bean.rb', line 327

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



272
273
274
275
276
277
278
279
280
# File 'lib/rumx/bean.rb', line 272

def bean_embedded(name)
  return nil unless self.class.bean_embeds.key?(name)
  bean = send(name)
  if bean
    bean_klass = self.class.bean_embeds[name]
    bean = bean_klass.new(bean) if bean_klass
  end
  return bean
end

#bean_find(name_array, index = 0) ⇒ Object

Find the bean



225
226
227
228
229
230
231
# File 'lib/rumx/bean.rb', line 225

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] || bean_embedded(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



302
303
304
305
306
307
308
# File 'lib/rumx/bean.rb', line 302

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



290
291
292
293
294
# File 'lib/rumx/bean.rb', line 290

def bean_get_attributes(ancestry=[], &block)
  bean_synchronize do
    do_bean_get_attributes(ancestry, &block)
  end
end

#bean_has_attributes?Boolean

Returns:

  • (Boolean)


282
283
284
285
286
287
288
# File 'lib/rumx/bean.rb', line 282

def bean_has_attributes?
  return true unless self.class.bean_attributes.empty?
  bean_each_embedded_child do |name, bean|
    return true if bean.bean_has_attributes?
  end
  return false
end

#bean_has_operations?Boolean

Returns:

  • (Boolean)


317
318
319
# File 'lib/rumx/bean.rb', line 317

def bean_has_operations?
  !self.class.bean_operations.empty?
end

#bean_monitorObject

Monitor for synchronization of attributes/operations



195
196
197
198
# File 'lib/rumx/bean.rb', line 195

def bean_monitor
  # TODO: How to initialize this in a module and avoid race condition?
  @monitor ||= Monitor.new
end

#bean_remove_child(name) ⇒ Object



218
219
220
221
222
# File 'lib/rumx/bean.rb', line 218

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



310
311
312
313
314
315
# File 'lib/rumx/bean.rb', line 310

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



296
297
298
299
300
# File 'lib/rumx/bean.rb', line 296

def bean_set_attributes(params)
  bean_synchronize do
    do_bean_set_attributes(params)
  end
end

#bean_synchronizeObject

Synchronize access to attributes and operations



201
202
203
204
205
# File 'lib/rumx/bean.rb', line 201

def bean_synchronize
  bean_monitor.synchronize do
    yield
  end
end