Class: Bricks::Builder::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/bricks/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, attrs, parent) ⇒ Proxy

Returns a new instance of Proxy.



145
146
147
148
149
150
# File 'lib/bricks/builder.rb', line 145

def initialize(obj, attrs, parent)
  @obj       = obj
  @attrs_in  = attrs.dup
  @attrs_out = {}
  @parent    = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/bricks/builder.rb', line 152

def method_missing(name, *args)
  name_y = name.to_sym

  if @attrs_in.assoc(name_y) && ! @attrs_out.has_key?(name_y)
    fix_attr name
  else
    @obj.send name, *args
  end
end

Instance Attribute Details

#objObject (readonly)

Returns the value of attribute obj.



143
144
145
# File 'lib/bricks/builder.rb', line 143

def obj
  @obj
end

Instance Method Details

#buildObject



162
163
164
165
166
# File 'lib/bricks/builder.rb', line 162

def build
  @attrs_in.each { |(k, _)| send k }

  @obj
end

#fix_attr(name) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/bricks/builder.rb', line 168

def fix_attr(name)
  val = case v = @attrs_in.assoc(name).last
        when Proc
          case r = v.call(*[self, @parent].take([v.arity, 0].max))
          when Proxy
            r.obj
          else
            r
          end
        when Builder, BuilderSet
          v.generate(:parent => self)
        else
          v
        end

  @attrs_out[name] = @obj.send("#{name}=", val)
end