Class: Conglomerate::BuilderCall::BuilderCallInstance

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/conglomerate/builder_call.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject

Returns the value of attribute attr_name.



26
27
28
# File 'lib/conglomerate/builder_call.rb', line 26

def attr_name
  @attr_name
end

#attrsObject

Returns the value of attribute attrs.



26
27
28
# File 'lib/conglomerate/builder_call.rb', line 26

def attrs
  @attrs
end

#bcObject

Returns the value of attribute bc.



26
27
28
# File 'lib/conglomerate/builder_call.rb', line 26

def bc
  @bc
end

#contextObject

Returns the value of attribute context.



26
27
28
# File 'lib/conglomerate/builder_call.rb', line 26

def context
  @context
end

#objectsObject

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

#runObject



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