module EasyMapper::Model::ClassMacros
Attributes
associations_to_many[RW]
Public Instance Methods
associations_to_one()
click to toggle source
# File lib/easy_mapper/model/class_macros.rb, line 57 def associations_to_one @associations_to_one = [] unless @associations_to_one @associations_to_one end
attributes(*attributes)
click to toggle source
# File lib/easy_mapper/model/class_macros.rb, line 11 def attributes(*attributes) return @attributes if attributes.empty? @attributes = attributes + [:id] @attributes.each do |attribute| define_singleton_method "find_by_#{attribute}" do |value| objects.where(attribute => value).exec end create_attr_accessor(attribute) end end
belongs_to(cls, attr_name:, id_column: nil)
click to toggle source
# File lib/easy_mapper/model/class_macros.rb, line 49 def belongs_to(cls, attr_name:, id_column: nil) id_column = "#{attr_name}_id" unless id_column association = Associations::BelongsTo.new(cls, attr_name, id_column) create_association_accessor(attr_name) create_attr_accessor(id_column) end
has_many(attr_name, cls:, mapped_by: nil)
click to toggle source
# File lib/easy_mapper/model/class_macros.rb, line 42 def has_many(attr_name, cls:, mapped_by: nil) association = Associations::HasMany.new(attr_name, cls, mapped_by) associations_to_many << association create_association_accessor(attr_name) end
has_one(attr_name, cls:, column: nil)
click to toggle source
# File lib/easy_mapper/model/class_macros.rb, line 35 def has_one(attr_name, cls:, column: nil) association = Associations::HasOne.new(attr_name, cls, column) associations_to_one << association create_association_accessor(attr_name) end
repository(repository = nil)
click to toggle source
# File lib/easy_mapper/model/class_macros.rb, line 24 def repository(repository = nil) return @repository unless repository @repository = repository end
table_name(name = nil)
click to toggle source
# File lib/easy_mapper/model/class_macros.rb, line 30 def table_name(name = nil) return @table_name unless name @table_name = name end
Private Instance Methods
create_association_accessor(name)
click to toggle source
# File lib/easy_mapper/model/class_macros.rb, line 74 def create_association_accessor(name) define_method(name) { @associations[name] } define_method("#{name}=") { |value| @associations[name] = value } end
create_attr_accessor(attribute)
click to toggle source
# File lib/easy_mapper/model/class_macros.rb, line 69 def create_attr_accessor(attribute) define_method(attribute) { @object[attribute] } define_method("#{attribute}=") { |value| @object[attribute] = value } end