class DomainMapper::Map

Public Class Methods

new(object_class, &block) click to toggle source
# File lib/domain_mapper.rb, line 5
def initialize(object_class, &block)
  @object_class = object_class
  @rules = []
  instance_eval &block
end

Public Instance Methods

attribute(attribute_name, hash_key) click to toggle source
# File lib/domain_mapper.rb, line 11
def attribute(attribute_name, hash_key)
  @rules << [attribute_name, hash_key]
end
build_object(data_hash) click to toggle source
# File lib/domain_mapper.rb, line 15
def build_object(data_hash)
  object = @object_class.new
  @rules.each do |attribute_name, hash_key|
    instance_variable_name = instance_variable_symbol(attribute_name)
    value = data_hash[hash_key]
    object.instance_variable_set(instance_variable_name, value)
  end
  object
end
instance_variable_symbol(name) click to toggle source
# File lib/domain_mapper.rb, line 35
def instance_variable_symbol(name)
  "@#{name}".to_sym
end
serialize(object) click to toggle source
# File lib/domain_mapper.rb, line 25
def serialize(object)
  fields = []
  @rules.each do |attribute_name, hash_key|
    instance_variable_name = instance_variable_symbol(attribute_name)
    value = object.instance_variable_get(instance_variable_name)
    fields << [hash_key, value]
  end
  Hash[fields]
end