# File lib/preserves/mapping.rb, line 32 def primary_key(key_name=nil) @primary_key = key_name unless key_name.nil? @primary_key end
class Preserves::Mapping
Attributes
belongs_to_mappings[RW]
has_many_mappings[RW]
model_class[RW]
name_mappings[RW]
repository[RW]
type_mappings[RW]
Public Class Methods
new(repository, model_class, &block)
click to toggle source
# File lib/preserves/mapping.rb, line 11 def initialize(repository, model_class, &block) table_name pluralize(model_class.name.downcase) primary_key "id" self.repository = repository self.model_class = model_class self.name_mappings = {} self.type_mappings = {} self.has_many_mappings = {} self.belongs_to_mappings = {} self.instance_eval(&block) end
Public Instance Methods
primary_key(key_name=nil)
click to toggle source
Note that this works to set or get the primary key. TODO: We don’t want to allow publicly setting this, but we need to publicly get it.
table_name(name=nil)
click to toggle source
Note that this works to set or get the table name. TODO: We don’t want to allow publicly setting this, but we need to publicly get it.
# File lib/preserves/mapping.rb, line 25 def table_name(name=nil) @table_name = name unless name.nil? @table_name end
Protected Instance Methods
belongs_to(related_attribute_name, options)
click to toggle source
# File lib/preserves/mapping.rb, line 57 def belongs_to(related_attribute_name, options) self.belongs_to_mappings[related_attribute_name] = options end
has_many(related_attribute_name, options)
click to toggle source
# File lib/preserves/mapping.rb, line 53 def has_many(related_attribute_name, options) self.has_many_mappings[related_attribute_name] = options end
map(*args)
click to toggle source
# File lib/preserves/mapping.rb, line 39 def map(*args) if args[0].is_a?(Hash) database_field_name = args[0].values.first model_attribute_name = args[0].keys.first self.name_mappings[database_field_name] = model_attribute_name elsif args[0].is_a?(Symbol) model_attribute_name = args[0] end if args[1].is_a?(Class) self.type_mappings[model_attribute_name] = args[1] end end
pluralize(string)
click to toggle source
# File lib/preserves/mapping.rb, line 61 def pluralize(string) case string when /(x|ch|ss|sh)$/i string + "es" when /s$/i string else string + "s" end end