Class: LocalModel::Model
- Inherits:
-
Object
- Object
- LocalModel::Model
- Defined in:
- lib/local_model/model.rb
Direct Known Subclasses
Defined Under Namespace
Classes: SchemaBuilder
Class Method Summary collapse
- .belongs_to(association, class_name: nil, foreign_key: nil, polymorphic: false) ⇒ Object
- .has_many(association, through: nil, class_name: nil, foreign_key: nil, as: nil) ⇒ Object
- .has_one(association, through: nil, class_name: nil, foreign_key: nil, as: nil) ⇒ Object
- .schema(&block) ⇒ Object
- .storage_path ⇒ Object
Instance Method Summary collapse
Class Method Details
.belongs_to(association, class_name: nil, foreign_key: nil, polymorphic: false) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/local_model/model.rb', line 8 def self.belongs_to(association, class_name: nil, foreign_key: nil, polymorphic: false) if foreign_key.nil? keyname = "#{association}_id" typename = "#{association}_type" else keyname = foreign_key end if class_name.nil? association_class_name = LocalModel::Functions.snake_to_camel(association) association_class_name[0] = association_class_name[0].upcase association_class_name = namespace_classname(association_class_name) else association_class_name = namespace_classname(class_name) end define_method association do if polymorphic association_type = self.send(typename) return nil if association_type.nil? || association_type.empty? polymorphic_class_name = LocalModel::Functions.snake_to_camel(association_type.gsub("_type", "")) polymorphic_class_name[0] = polymorphic_class_name[0].upcase association_class = Object.const_get(polymorphic_class_name) else association_class = Object.const_get(association_class_name) end id = self.send(keyname) association_class.find_by(id: id) end define_method "#{association}=" do |association_obj| self.send("#{keyname}=", association_obj&.id) if polymorphic if !association_obj.nil? self.send("#{typename}=", association_obj.class.to_s) end end end end |
.has_many(association, through: nil, class_name: nil, foreign_key: nil, as: nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/local_model/model.rb', line 49 def self.has_many(association, through: nil, class_name: nil, foreign_key: nil, as: nil) if class_name.nil? association_classname = namespace_classname(get_classname_from_association(association)) else association_classname = namespace_classname(class_name) end if through.nil? if as.nil? current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id" else current_class_id_methodname = "#{as}_id" end belongs_to_id_sym = current_class_id_methodname.to_sym add_to_collection = Proc.new do |arg, model| arg.send("#{belongs_to_id_sym}=", model.id) end define_method association do collection_args = {belongs_to_id_sym => self.id} if !as.nil? collection_args["#{as}_type".to_sym] = self.class.to_s end association_class = Object.const_get(association_classname) LocalModel::Collection.create_from( array: association_class.where(**collection_args), for_model: self, for_collection_class: association_class, add_to_collection_proc: add_to_collection ) end else current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(LocalModel::Functions.singularize(association))}_id" belongs_to_id_sym = current_class_id_methodname.to_sym add_to_collection = Proc.new do |arg, model| through_collection = model.send(through) through_classname = through_collection.collection_class new_join = through_classname.new new_join.send("#{belongs_to_id_sym}=", arg.id) through_collection << new_join end define_method association do association_class = Object.const_get(association_classname) LocalModel::Collection.create_from( array: self.send(through).map{|through_obj| association_class.find_by(id: through_obj.send(belongs_to_id_sym))}, for_model: self, for_collection_class: association_class, add_to_collection_proc: add_to_collection ) end end end |
.has_one(association, through: nil, class_name: nil, foreign_key: nil, as: nil) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/local_model/model.rb', line 103 def self.has_one(association, through: nil, class_name: nil, foreign_key: nil, as: nil) if class_name.nil? association_classname = LocalModel::Functions.snake_to_camel(association) association_classname[0] = association_classname[0].upcase association_classname = namespace_classname(association_classname) else association_classname = namespace_classname(class_name) end if through.nil? if as.nil? current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(denamespace_classname(self))}_id" else current_class_id_methodname = "#{as}_id" end belongs_to_id_sym = current_class_id_methodname.to_sym define_method association do collection_args = {belongs_to_id_sym => self.id} if !as.nil? collection_args["#{as}_type".to_sym] = self.class.to_s end association_class = Object.const_get(association_classname) association_class.where(**collection_args).first end else current_class_id_methodname = foreign_key || "#{LocalModel::Functions.camel_to_snake(association)}_id" belongs_to_id_sym = current_class_id_methodname.to_sym define_method association do association_class = Object.const_get(association_classname) association_class.where(id: self.send(through)&.send(belongs_to_id_sym)).first end end end |
.schema(&block) ⇒ Object
3 4 5 6 |
# File 'lib/local_model/model.rb', line 3 def self.schema(&block) raise NoMethodError.new("self.schema must be defined") # yield(SchemaBuilder.new(self)) end |
.storage_path ⇒ Object
163 164 165 166 |
# File 'lib/local_model/model.rb', line 163 def self.storage_path slash = LocalModel.path == '/' ? '' : '/' "#{LocalModel.path}#{slash}#{self}.csv" end |
Instance Method Details
#==(other) ⇒ Object
186 187 188 189 |
# File 'lib/local_model/model.rb', line 186 def ==(other) self.class == other.class && self.id == other.id end |
#reload ⇒ Object
182 183 184 |
# File 'lib/local_model/model.rb', line 182 def reload self.class.find(self.id) end |
#update(**args) ⇒ Object
168 169 170 171 172 173 |
# File 'lib/local_model/model.rb', line 168 def update(**args) args.each do |k,v| self.send("#{k.to_s}=", v) end self.save end |
#update!(**args) ⇒ Object
175 176 177 178 179 180 |
# File 'lib/local_model/model.rb', line 175 def update!(**args) args.each do |k,v| self.send("#{k.to_s}=", v) end self.save! end |