# File lib/preserves/mapper.rb, line 32 def primary_key_attribute column_name_to_attribute_name(mapping.primary_key) end
class Preserves::Mapper
Attributes
mapping[RW]
Public Class Methods
new(mapping)
click to toggle source
# File lib/preserves/mapper.rb, line 13 def initialize(mapping) self.mapping = mapping end
Public Instance Methods
map(result, relations={})
click to toggle source
# File lib/preserves/mapper.rb, line 17 def map(result, relations={}) result.map do |record| map_one(record, relations) end end
map_one(record, relations={})
click to toggle source
# File lib/preserves/mapper.rb, line 23 def map_one(record, relations={}) mapping.model_class.new.tap do |object| map_attributes(object, record) map_relations(object, record, relations) end end
Protected Instance Methods
primary_key_attribute()
click to toggle source
Private Instance Methods
attribute_name_to_attribute_type(attribute_name)
click to toggle source
# File lib/preserves/mapper.rb, line 61 def attribute_name_to_attribute_type(attribute_name) mapping.type_mappings.fetch(attribute_name.to_sym) { String } end
attribute_value_to_field_value(attribute_name, attribute_value)
click to toggle source
# File lib/preserves/mapper.rb, line 56 def attribute_value_to_field_value(attribute_name, attribute_value) attribute_type = attribute_name_to_attribute_type(attribute_name) uncoerce(attribute_value, to: attribute_type) end
coerce(field_value, options={})
click to toggle source
# File lib/preserves/mapper.rb, line 87 def coerce(field_value, options={}) return nil if field_value.nil? if options[:to] == Integer Integer(field_value) else field_value end end
column_name_to_attribute_name(column_name)
click to toggle source
# File lib/preserves/mapper.rb, line 47 def column_name_to_attribute_name(column_name) mapping.name_mappings.fetch(column_name) { column_name } end
field_value_to_attribute_value(attribute_name, field_value)
click to toggle source
# File lib/preserves/mapper.rb, line 51 def field_value_to_attribute_value(attribute_name, field_value) attribute_type = attribute_name_to_attribute_type(attribute_name) coerce(field_value, to: attribute_type) end
map_attributes(object, record)
click to toggle source
# File lib/preserves/mapper.rb, line 38 def map_attributes(object, record) record.each_pair do |column_name, field_value| attribute_name = column_name_to_attribute_name(column_name) if object.respond_to?("#{attribute_name}=") object.send("#{attribute_name}=", field_value_to_attribute_value(attribute_name, field_value)) end end end
map_belongs_to_relations(object, record, relations)
click to toggle source
# File lib/preserves/mapper.rb, line 80 def map_belongs_to_relations(object, record, relations) # TODO: Ensure that there's a setter for every relation_name before we iterate through the relations. relations.each do |relation_name, relation_result_set| Mapper::BelongsTo.new(object, record, relation_name, relation_result_set, mapping).map! end end
map_has_many_relations(object, record, relations)
click to toggle source
# File lib/preserves/mapper.rb, line 73 def map_has_many_relations(object, record, relations) # TODO: Ensure that there's a setter for every relation_name before we iterate through the relations. relations.each do |relation_name, relation_result_set| Mapper::HasMany.new(object, record, relation_name, relation_result_set, mapping).map! end end
map_relations(object, record, relations)
click to toggle source
# File lib/preserves/mapper.rb, line 65 def map_relations(object, record, relations) has_many_relations = relations.select{ |k, _v| mapping.has_many_mappings.keys.include?(k) } map_has_many_relations(object, record, has_many_relations) belongs_to_relations = relations.select{ |k, _v| mapping.belongs_to_mappings.keys.include?(k) } map_belongs_to_relations(object, record, belongs_to_relations) # TODO: Raise an exception if any of the relations weren't found in any of the relation mappings. end
uncoerce(attribute_value, options={})
click to toggle source
# File lib/preserves/mapper.rb, line 97 def uncoerce(attribute_value, options={}) return "NULL" if attribute_value.nil? if options[:to] == String "'#{attribute_value}'" else attribute_value.to_s end end