Module: Mince::DataModel::ClassMethods
- Defined in:
- lib/mince/data_model.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#add(hash) ⇒ Object
Adds a new record with provided data hash.
-
#all ⇒ Object
Finds all records in the collection.
-
#all_before(field, value) ⇒ Object
Finds all records that match a set of key / value pairs.
-
#all_by_field(field, value) ⇒ Object
Returns all records in the collection that has the given field and value.
-
#all_by_fields(hash) ⇒ Object
Finds all records that match a set of key / value pairs.
-
#array_contains(field, value) ⇒ Object
Finds all records where the field, which must be an array field, contains the value.
-
#containing_any(field, values) ⇒ Object
Finds all records where the field contains any of the values.
-
#data_collection(collection_name = nil) ⇒ Object
Sets the name of the data collection Returns the name of the data collection.
-
#data_fields(*fields) ⇒ Object
Sets what data fields to accept Returns the fields.
-
#delete_by_params(params) ⇒ Object
Deletes all records that matches the field / value key pairs in the params provided in the collection.
-
#delete_collection ⇒ Object
Deletes the entire collection from the database.
-
#delete_field(field) ⇒ Object
Deletes a field from all records in the collection.
-
#find(id) ⇒ Object
Returns a record that has the given id.
-
#find_by_field(field, value) ⇒ Object
Finds One record that matches a field / value pair.
-
#find_by_fields(hash) ⇒ Object
Finds One record that matches all of the field / value pairs.
-
#generate_unique_id(seed) ⇒ Object
Generates a new id to be used for a primary key.
-
#increment_field_by_amount(id, field, amount) ⇒ Object
Increments a field by a given amount.
-
#infer_fields? ⇒ Boolean
Returns true if the data model is infering fields from the model.
-
#infer_fields_from_model ⇒ Object
Allows models to define fields without the data model needing to define them also.
-
#interface ⇒ Object
The interface configured by Mince::Config.interface=.
- #primary_key ⇒ Object
-
#push_to_array(id, field, value) ⇒ Object
Pushes a value to an array field.
-
#remove_from_array(id, field, value) ⇒ Object
Removes a value from an field that is an array.
-
#store(model) ⇒ Object
Stores the given model into the data store.
-
#timestamps? ⇒ Boolean
Returns true if the data model uses the Timestamps mixin.
- #translate_each_from_interface(data) ⇒ Object
- #translate_from_interface(hash) ⇒ Object
-
#update(model) ⇒ Object
Updates the given model in the data store with the fields and values in model.
-
#update_field_with_value(id, field, value) ⇒ Object
Updates a specific field with a value for the record with the given id.
Instance Method Details
#add(hash) ⇒ Object
Adds a new record with provided data hash
259 260 261 262 263 |
# File 'lib/mince/data_model.rb', line 259 def add(hash) hash = HashWithIndifferentAccess.new(hash.merge(primary_key => generate_unique_id(hash))) (hash) if interface.add(data_collection, hash).first end |
#all ⇒ Object
Finds all records in the collection
180 181 182 |
# File 'lib/mince/data_model.rb', line 180 def all translate_each_from_interface interface.find_all(data_collection) end |
#all_before(field, value) ⇒ Object
Finds all records that match a set of key / value pairs
206 207 208 |
# File 'lib/mince/data_model.rb', line 206 def all_before(field, value) translate_each_from_interface interface.all_before(data_collection, field, value) end |
#all_by_field(field, value) ⇒ Object
Returns all records in the collection that has the given field and value
189 190 191 |
# File 'lib/mince/data_model.rb', line 189 def all_by_field(field, value) translate_each_from_interface interface.get_all_for_key_with_value(data_collection, field, value) end |
#all_by_fields(hash) ⇒ Object
Finds all records that match a set of key / value pairs
197 198 199 |
# File 'lib/mince/data_model.rb', line 197 def all_by_fields(hash) translate_each_from_interface interface.get_by_params(data_collection, hash) end |
#array_contains(field, value) ⇒ Object
Finds all records where the field, which must be an array field, contains the value
241 242 243 |
# File 'lib/mince/data_model.rb', line 241 def array_contains(field, value) translate_each_from_interface interface.array_contains(data_collection, field, value) end |
#containing_any(field, values) ⇒ Object
Finds all records where the field contains any of the values
232 233 234 |
# File 'lib/mince/data_model.rb', line 232 def containing_any(field, values) translate_each_from_interface interface.containing_any(data_collection, field, values) end |
#data_collection(collection_name = nil) ⇒ Object
Sets the name of the data collection Returns the name of the data collection
82 83 84 85 |
# File 'lib/mince/data_model.rb', line 82 def data_collection(collection_name = nil) set_data_collection(collection_name) if collection_name @data_collection end |
#data_fields(*fields) ⇒ Object
Sets what data fields to accept Returns the fields
- Calling multiple times will do an ammendment to the existing list of fields
- Calling without any fields will simply return the current list of fields
71 72 73 74 75 |
# File 'lib/mince/data_model.rb', line 71 def data_fields(*fields) @data_fields ||= [] create_data_fields(*fields) if fields.any? @data_fields end |
#delete_by_params(params) ⇒ Object
Deletes all records that matches the field / value key pairs in the params provided in the collection
This will only delete records that match all key/value pairs in the params hash.
DataModel.delete_by_params(username: 'railsgrammer', first_name: 'Matt Simpson')
173 174 175 |
# File 'lib/mince/data_model.rb', line 173 def delete_by_params(params) interface.delete_by_params(data_collection, params) end |
#delete_collection ⇒ Object
Deletes the entire collection from the database
246 247 248 |
# File 'lib/mince/data_model.rb', line 246 def delete_collection interface.delete_collection(data_collection) end |
#delete_field(field) ⇒ Object
Deletes a field from all records in the collection
160 161 162 |
# File 'lib/mince/data_model.rb', line 160 def delete_field(field) interface.delete_field(data_collection, field) end |
#find(id) ⇒ Object
Returns a record that has the given id
Returns nil if nothing found
153 154 155 |
# File 'lib/mince/data_model.rb', line 153 def find(id) translate_from_interface interface.find(data_collection, id) end |
#find_by_field(field, value) ⇒ Object
Finds One record that matches a field / value pair
223 224 225 |
# File 'lib/mince/data_model.rb', line 223 def find_by_field(field, value) translate_from_interface interface.get_for_key_with_value(data_collection, field, value) end |
#find_by_fields(hash) ⇒ Object
Finds One record that matches all of the field / value pairs
214 215 216 |
# File 'lib/mince/data_model.rb', line 214 def find_by_fields(hash) translate_from_interface all_by_fields(hash).first end |
#generate_unique_id(seed) ⇒ Object
Generates a new id to be used for a primary key
251 252 253 |
# File 'lib/mince/data_model.rb', line 251 def generate_unique_id(seed) interface.generate_unique_id(seed) end |
#increment_field_by_amount(id, field, amount) ⇒ Object
Increments a field by a given amount
Some databases provide very efficient algorithms for incrementing or decrementing a value.
122 123 124 125 |
# File 'lib/mince/data_model.rb', line 122 def increment_field_by_amount(id, field, amount) interface.increment_field_by_amount(data_collection, id, field, amount) (data_collection, id) if end |
#infer_fields? ⇒ Boolean
Returns true if the data model is infering fields from the model
59 60 61 |
# File 'lib/mince/data_model.rb', line 59 def infer_fields? !!@infer_fields_from_model end |
#infer_fields_from_model ⇒ Object
Allows models to define fields without the data model needing to define them also
54 55 56 |
# File 'lib/mince/data_model.rb', line 54 def infer_fields_from_model @infer_fields_from_model = true end |
#interface ⇒ Object
The interface configured by Mince::Config.interface=
49 50 51 |
# File 'lib/mince/data_model.rb', line 49 def interface Config.interface end |
#primary_key ⇒ Object
281 282 283 |
# File 'lib/mince/data_model.rb', line 281 def primary_key @primary_key ||= interface.primary_key end |
#push_to_array(id, field, value) ⇒ Object
Pushes a value to an array field
142 143 144 145 |
# File 'lib/mince/data_model.rb', line 142 def push_to_array(id, field, value) interface.push_to_array(data_collection, id, field, value) (data_collection, id) if end |
#remove_from_array(id, field, value) ⇒ Object
Removes a value from an field that is an array
132 133 134 135 |
# File 'lib/mince/data_model.rb', line 132 def remove_from_array(id, field, value) interface.remove_from_array(data_collection, id, field, value) (data_collection, id) if end |
#store(model) ⇒ Object
Stores the given model into the data store
91 92 93 |
# File 'lib/mince/data_model.rb', line 91 def store(model) new(model).create end |
#timestamps? ⇒ Boolean
Returns true if the data model uses the Timestamps mixin
266 267 268 |
# File 'lib/mince/data_model.rb', line 266 def include? Mince::DataModel::Timestamps end |
#translate_each_from_interface(data) ⇒ Object
277 278 279 |
# File 'lib/mince/data_model.rb', line 277 def translate_each_from_interface(data) data.collect {|d| translate_from_interface(d) } end |
#translate_from_interface(hash) ⇒ Object
270 271 272 273 274 275 |
# File 'lib/mince/data_model.rb', line 270 def translate_from_interface(hash) if hash hash["id"] = hash[primary_key] if hash[primary_key] && (primary_key != :id || primary_key != 'id') HashWithIndifferentAccess.new hash end end |
#update(model) ⇒ Object
Updates the given model in the data store with the fields and values in model
Uses model.id to find the record to update
100 101 102 |
# File 'lib/mince/data_model.rb', line 100 def update(model) new(model).update end |
#update_field_with_value(id, field, value) ⇒ Object
Updates a specific field with a value for the record with the given id
109 110 111 112 |
# File 'lib/mince/data_model.rb', line 109 def update_field_with_value(id, field, value) interface.update_field_with_value(data_collection, id, field, value) (data_collection, id) if end |