Module: MotionPrime::ModelBaseMixin::ClassMethods
- Defined in:
- motion-prime/models/_base_mixin.rb
Instance Method Summary collapse
-
#attribute(name, options = {}) ⇒ Object
Define model attribute.
-
#attributes(*attrs) ⇒ Object
Set and/or return all model attribute names.
-
#count ⇒ Object
Count of models.
-
#create(data = {}) ⇒ Object
Initialize a new object and save it to store.
- #default_sort(sort_options) ⇒ Object
-
#delete(*args) ⇒ Object
Delete objects from store by given options.
-
#delete_all ⇒ Object
Delete all objects with this Prime::Model.
-
#new(data = {}, options = {}) ⇒ Object
Initialize a new object.
-
#store ⇒ Object
Return store associated with model class, or shared store by default.
-
#store=(store) ⇒ Object
Define store associated with model class.
Instance Method Details
#attribute(name, options = {}) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'motion-prime/models/_base_mixin.rb', line 203 def attribute(name, = {}) attributes << name.to_sym attributes.uniq! define_method(:"#{name}=") do |value| tracking_block = proc { if [:convert] || !.has_key?(:convert) self.info[name] = attribute_convert_in(value, [:type]) else self.info[name] = value end } return tracking_block.call if @_tracking_changes track_changed_attributes(&tracking_block) end define_method(name.to_sym) do if [:convert] || !.has_key?(:convert) attribute_convert_out(self.info[name], [:type]) else self.info[name] end end define_method("#{name}?") do self.info[name].present? end end |
#attributes(*attrs) ⇒ Object
Set and/or return all model attribute names
235 236 237 238 239 240 |
# File 'motion-prime/models/_base_mixin.rb', line 235 def attributes(*attrs) if attrs.size > 0 attrs.each{|attr| attribute attr} end @attributes ||= [] end |
#count ⇒ Object
Count of models
260 261 262 |
# File 'motion-prime/models/_base_mixin.rb', line 260 def count self.store.count(self) end |
#create(data = {}) ⇒ Object
Initialize a new object and save it to store
@example:
User.create(name: "Bob", age: 10)
188 189 190 191 192 |
# File 'motion-prime/models/_base_mixin.rb', line 188 def create(data = {}) object = self.new(data) object.save object end |
#default_sort(sort_options) ⇒ Object
289 290 291 |
# File 'motion-prime/models/_base_mixin.rb', line 289 def default_sort() self. = end |
#delete(*args) ⇒ Object
Delete objects from store by given options
@example:
User.delete(:name => "Bob") #
271 272 273 274 275 276 277 |
# File 'motion-prime/models/_base_mixin.rb', line 271 def delete(*args) if args.blank? raise "Using delete with no args is not allowed. Please use delete_all to delete all records" end keys = find_keys(*args) self.store.delete_keys(keys) end |
#delete_all ⇒ Object
Delete all objects with this Prime::Model
@example:
User.delete_all
285 286 287 |
# File 'motion-prime/models/_base_mixin.rb', line 285 def delete_all self.store.delete_keys(find_keys) end |
#new(data = {}, options = {}) ⇒ Object
Initialize a new object
@example:
User.new(name: "Bob", age: 10)
175 176 177 178 179 |
# File 'motion-prime/models/_base_mixin.rb', line 175 def new(data = {}, = {}) object = self.nanoObjectWithDictionary({}) object.assign_attributes(data, ) object end |
#store ⇒ Object
Return store associated with model class, or shared store by default
245 246 247 |
# File 'motion-prime/models/_base_mixin.rb', line 245 def store @store ||= MotionPrime::Store.shared_store end |
#store=(store) ⇒ Object
Define store associated with model class
253 254 255 |
# File 'motion-prime/models/_base_mixin.rb', line 253 def store=(store) @store = store end |