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
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'motion-prime/models/_base_mixin.rb', line 207 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
239 240 241 242 243 244 |
# File 'motion-prime/models/_base_mixin.rb', line 239 def attributes(*attrs) if attrs.size > 0 attrs.each{|attr| attribute attr} end @attributes ||= [] end |
#count ⇒ Object
Count of models
264 265 266 |
# File 'motion-prime/models/_base_mixin.rb', line 264 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)
192 193 194 195 196 |
# File 'motion-prime/models/_base_mixin.rb', line 192 def create(data = {}) object = self.new(data) object.save object end |
#default_sort(sort_options) ⇒ Object
293 294 295 |
# File 'motion-prime/models/_base_mixin.rb', line 293 def default_sort() self. = end |
#delete(*args) ⇒ Object
Delete objects from store by given options
@example:
User.delete(:name => "Bob") #
275 276 277 278 279 280 281 |
# File 'motion-prime/models/_base_mixin.rb', line 275 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
289 290 291 |
# File 'motion-prime/models/_base_mixin.rb', line 289 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)
179 180 181 182 183 |
# File 'motion-prime/models/_base_mixin.rb', line 179 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
249 250 251 |
# File 'motion-prime/models/_base_mixin.rb', line 249 def store @store ||= MotionPrime::Store.shared_store end |
#store=(store) ⇒ Object
Define store associated with model class
257 258 259 |
# File 'motion-prime/models/_base_mixin.rb', line 257 def store=(store) @store = store end |