Class: Conglomerate::BuilderCall::BuilderCallInstance
- Inherits:
-
Object
- Object
- Conglomerate::BuilderCall::BuilderCallInstance
- Extended by:
- Forwardable
- Defined in:
- lib/conglomerate/builder_call.rb
Instance Attribute Summary collapse
-
#attr_name ⇒ Object
Returns the value of attribute attr_name.
-
#attrs ⇒ Object
Returns the value of attribute attrs.
-
#bc ⇒ Object
Returns the value of attribute bc.
-
#context ⇒ Object
Returns the value of attribute context.
-
#objects ⇒ Object
Returns the value of attribute objects.
Instance Method Summary collapse
-
#initialize(bc, context, objects, attrs, attr_name) ⇒ BuilderCallInstance
constructor
A new instance of BuilderCallInstance.
- #invoke(item = nil) ⇒ Object
- #run ⇒ Object
- #value_from_proc(value) ⇒ Object
Constructor Details
#initialize(bc, context, objects, attrs, attr_name) ⇒ BuilderCallInstance
Returns a new instance of BuilderCallInstance.
30 31 32 33 34 35 36 |
# File 'lib/conglomerate/builder_call.rb', line 30 def initialize(bc, context, objects, attrs, attr_name) self.bc = bc self.context = context self.objects = objects self.attrs = attrs self.attr_name = attr_name end |
Instance Attribute Details
#attr_name ⇒ Object
Returns the value of attribute attr_name.
26 27 28 |
# File 'lib/conglomerate/builder_call.rb', line 26 def attr_name @attr_name end |
#attrs ⇒ Object
Returns the value of attribute attrs.
26 27 28 |
# File 'lib/conglomerate/builder_call.rb', line 26 def attrs @attrs end |
#bc ⇒ Object
Returns the value of attribute bc.
26 27 28 |
# File 'lib/conglomerate/builder_call.rb', line 26 def bc @bc end |
#context ⇒ Object
Returns the value of attribute context.
26 27 28 |
# File 'lib/conglomerate/builder_call.rb', line 26 def context @context end |
#objects ⇒ Object
Returns the value of attribute objects.
26 27 28 |
# File 'lib/conglomerate/builder_call.rb', line 26 def objects @objects end |
Instance Method Details
#invoke(item = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/conglomerate/builder_call.rb', line 55 def invoke(item = nil) serializer = builder.serializer builder_klass = Class.new do include serializer end if item builder_klass.class_exec(item, &block) if block else builder_klass.class_eval(&block) if block end local_objects = item ? [item] : objects builder = builder_klass.new(local_objects, :name => name, :context => context) local_attrs = {} opts.each do |key, value| local_attrs[key] = value_from_proc(value) end builder.build(local_attrs) end |
#run ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/conglomerate/builder_call.rb', line 38 def run return unless should_run? if iterates attrs[attr_name] ||= [] objects.each do |item| attrs[attr_name] << invoke(item) end elsif array attrs[attr_name] ||= [] attrs[attr_name] << invoke else attrs[attr_name] = invoke end end |
#value_from_proc(value) ⇒ Object
79 80 81 82 |
# File 'lib/conglomerate/builder_call.rb', line 79 def value_from_proc(value) value = context.instance_eval(&value) if value.respond_to?(:call) value end |