class FactoryGirl::Proxy::ObjectWrapper

Public Class Methods

new(klass) click to toggle source
# File lib/factory_girl/proxy.rb, line 92
def initialize(klass)
  @klass      = klass
  @attributes = []
  @cached_attribute_values = {}
end

Public Instance Methods

get(attribute) click to toggle source
# File lib/factory_girl/proxy.rb, line 120
def get(attribute)
  @cached_attribute_values[attribute] ||= anonymous_instance.send(attribute)
end
object() click to toggle source
# File lib/factory_girl/proxy.rb, line 105
def object
  @object ||= @klass.new
  assign_object_attributes
  @object
end
set(attribute, value) click to toggle source
# File lib/factory_girl/proxy.rb, line 111
def set(attribute, value)
  define_attribute(attribute, value)
  @attributes << attribute.name
end
set_ignored(attribute, value) click to toggle source
# File lib/factory_girl/proxy.rb, line 116
def set_ignored(attribute, value)
  define_attribute(attribute, value)
end
to_hash() click to toggle source
# File lib/factory_girl/proxy.rb, line 98
def to_hash
  @attributes.inject({}) do |result, attribute|
    result[attribute] = get(attribute)
    result
  end
end

Private Instance Methods

anonymous_class() click to toggle source
# File lib/factory_girl/proxy.rb, line 136
def anonymous_class
  @anonymous_class ||= Class.new
end
anonymous_instance() click to toggle source
# File lib/factory_girl/proxy.rb, line 140
def anonymous_instance
  @anonymous_instance ||= anonymous_class.new
end
assign_object_attributes() click to toggle source
# File lib/factory_girl/proxy.rb, line 130
def assign_object_attributes
  (@attributes - @cached_attribute_values.keys).each do |attribute|
    @object.send("#{attribute}=", get(attribute))
  end
end
define_attribute(attribute, value) click to toggle source
# File lib/factory_girl/proxy.rb, line 126
def define_attribute(attribute, value)
  anonymous_class.send(:define_method, attribute.name, value)
end