# File lib/easy_mapper/model.rb, line 81 def primary_keys self.class.primary_keys end
module EasyMapper::Model
Public Class Methods
included(cls)
click to toggle source
# File lib/easy_mapper/model.rb, line 9 def self.included(cls) cls.extend ClassMacros cls.extend QueryMethods db_adapter = EasyMapper::Config.db_adapter cls.repository EasyMapper::DbRepository.new(cls, db_adapter) cls.class_exec do def initialize(initial_values = {}) @object = initial_values.select do |key, _| self.class.attributes.include? key end all_associations = associations_to_one + associations_to_many defined_assoc_names = all_associations.map(&:name) @associations = initial_values.select do |key, _| defined_assoc_names.include? key end associations_to_many .reject { |assoc| initial_values.include? assoc.name } .each do |assoc| @associations[assoc.name] = [] end end end def save associations_to_one.each do |assoc_to_one| result = @associations[assoc_to_one.name].save @object[assoc_to_one.id_column] = result.id end if id repository.update(id, @object) else @object[:id] = repository.next_id repository.create(@object) end associations_to_many.each do |assoc_to_many| @associations[assoc_to_many.name].each do |model| model.public_send "#{assoc_to_many.mapped_by}=", id model.save end end self end def delete raise Errors::DeleteUnsavedRecordError unless id associations_to_many.each do |assoc_to_many| @associations[assoc_to_many.name].each(&:delete) end repository.delete(id: id) associations_to_one.each do |assoc_to_one| result = @associations[assoc_to_one.name].delete end end def ==(other) return id == other.id if id && other.id equal? other end private def primary_keys self.class.primary_keys end def repository self.class.repository end def associations_to_many self.class.associations_to_many end def associations_to_one self.class.associations_to_one end end
new(initial_values = {})
click to toggle source
# File lib/easy_mapper/model.rb, line 17 def initialize(initial_values = {}) @object = initial_values.select do |key, _| self.class.attributes.include? key end all_associations = associations_to_one + associations_to_many defined_assoc_names = all_associations.map(&:name) @associations = initial_values.select do |key, _| defined_assoc_names.include? key end associations_to_many .reject { |assoc| initial_values.include? assoc.name } .each do |assoc| @associations[assoc.name] = [] end end
Public Instance Methods
==(other)
click to toggle source
# File lib/easy_mapper/model.rb, line 74 def ==(other) return id == other.id if id && other.id equal? other end
associations_to_many()
click to toggle source
# File lib/easy_mapper/model.rb, line 89 def associations_to_many self.class.associations_to_many end
associations_to_one()
click to toggle source
# File lib/easy_mapper/model.rb, line 93 def associations_to_one self.class.associations_to_one end
delete()
click to toggle source
# File lib/easy_mapper/model.rb, line 60 def delete raise Errors::DeleteUnsavedRecordError unless id associations_to_many.each do |assoc_to_many| @associations[assoc_to_many.name].each(&:delete) end repository.delete(id: id) associations_to_one.each do |assoc_to_one| result = @associations[assoc_to_one.name].delete end end
primary_keys()
click to toggle source
repository()
click to toggle source
# File lib/easy_mapper/model.rb, line 85 def repository self.class.repository end
save()
click to toggle source
# File lib/easy_mapper/model.rb, line 37 def save associations_to_one.each do |assoc_to_one| result = @associations[assoc_to_one.name].save @object[assoc_to_one.id_column] = result.id end if id repository.update(id, @object) else @object[:id] = repository.next_id repository.create(@object) end associations_to_many.each do |assoc_to_many| @associations[assoc_to_many.name].each do |model| model.public_send "#{assoc_to_many.mapped_by}=", id model.save end end self end