Module: Mince::DataModel::ClassMethods

Defined in:
lib/mince/data_model.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add(hash) ⇒ Object

Adds a new record with provided data hash

Parameters:

  • data (Hash)

    to add to the data collection



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)))
  add_timestamps_to_hash(hash) if timestamps?
  interface.add(data_collection, hash).first
end

#allObject

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

Parameters:

  • field (Symbol)

    the field to query for

  • Time (*)

    object to find all records where field is less than value



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

Parameters:

  • field (Symbol)

    the field to query for

  • value (*)

    the value to query on the field for



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

Parameters:

  • hash (Hash)

    a hash of field / value pairs to query records for



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

Parameters:

  • field (Symbol)

    the field to query against

  • value (*)

    the value to query against



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

Parameters:

  • field (Symbol)

    the field to query against

  • values (Array)

    an array of values to get records for



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

Parameters:

  • collection_name (String) (defaults to: nil)

    the name of the collection to use



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

Parameters:

  • *fields (*Array)

    an array of fields to add to the data model



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')

Parameters:

  • params (Hash)

    the key/value pair hash to delete records by



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_collectionObject

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

Parameters:

  • field (Symbol)

    the field to remove



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

Parameters:

  • id (id)

    the id of the record to find



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

Parameters:

  • field (Symbol)

    the field to query for

  • value (*)

    the value to query on the field for



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

Parameters:

  • hash (Hash)

    the hash to query for



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.

Parameters:

  • id (id)

    the id of the record to update

  • field (Symbol)

    the field to update

  • amount (Numeric)

    the amount to increment or decrement the field with



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)
  set_update_timestamp_for(data_collection, id) if timestamps?
end

#infer_fields?Boolean

Returns true if the data model is infering fields from the model

Returns:

  • (Boolean)


59
60
61
# File 'lib/mince/data_model.rb', line 59

def infer_fields?
  !!@infer_fields_from_model
end

#infer_fields_from_modelObject

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

#interfaceObject

The interface configured by Mince::Config.interface=



49
50
51
# File 'lib/mince/data_model.rb', line 49

def interface
  Config.interface
end

#primary_keyObject



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

Parameters:

  • id (id)

    the id of the record to update

  • field (Symbol)

    the field to update

  • value (*)

    the value to update the field with



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)
  set_update_timestamp_for(data_collection, id) if timestamps?
end

#remove_from_array(id, field, value) ⇒ Object

Removes a value from an field that is an array

Parameters:

  • id (id)

    the id of the record to update

  • field (Symbol)

    the field to update

  • value (*)

    the value to update the field with



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)
  set_update_timestamp_for(data_collection, id) if timestamps?
end

#store(model) ⇒ Object

Stores the given model into the data store

Parameters:

  • model (Class)

    a ruby class instance object to store 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

Returns:

  • (Boolean)


266
267
268
# File 'lib/mince/data_model.rb', line 266

def timestamps?
  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

Parameters:

  • model (Class)

    a ruby class instance object to store into the data store



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

Parameters:

  • id (id)

    the id of the record to update

  • field (Symbol)

    the field to update

  • value (*)

    the value to update the field with



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)
  set_update_timestamp_for(data_collection, id) if timestamps?
end