Module: EasyMapper::Model::ClassMacros
- Defined in:
- lib/easy_mapper/model/class_macros.rb
Instance Attribute Summary collapse
-
#associations_to_many ⇒ Object
Returns the value of attribute associations_to_many.
Instance Method Summary collapse
- #associations_to_one ⇒ Object
- #attributes(*attributes) ⇒ Object
- #belongs_to(cls, attr_name:, id_column: nil) ⇒ Object
- #has_many(attr_name, cls:, mapped_by: nil) ⇒ Object
- #has_one(attr_name, cls:, column: nil) ⇒ Object
- #repository(repository = nil) ⇒ Object
- #table_name(name = nil) ⇒ Object
Instance Attribute Details
#associations_to_many ⇒ Object
Returns the value of attribute associations_to_many.
9 10 11 |
# File 'lib/easy_mapper/model/class_macros.rb', line 9 def associations_to_many @associations_to_many end |
Instance Method Details
#associations_to_one ⇒ Object
57 58 59 60 |
# 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) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# 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) ⇒ Object
49 50 51 52 53 54 55 |
# 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) ⇒ Object
42 43 44 45 46 47 |
# 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) ⇒ Object
35 36 37 38 39 40 |
# 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) ⇒ Object
24 25 26 27 28 |
# 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) ⇒ Object
30 31 32 33 |
# File 'lib/easy_mapper/model/class_macros.rb', line 30 def table_name(name = nil) return @table_name unless name @table_name = name end |