Module: Mince::HashyDb::Interface

Defined in:
lib/hashy_db/interface.rb

Overview

HashyDb Data Store

HashyDb Data Store stores and retrieves data from a ruby in-memory hash.

Using this library offers more extensibility and growth for your application. If at any point in time you want to support a different database for all, or even one, of your classes, you only need to implement the few methods defined in this class.

HashyDb::DataStore.add 'fruits', id: '1', name: 'Shnawzberry', color: 'redish', quantity: '20'
HashyDb::DataStore.get_for_key_with_value 'fruits', :color, 'redish'

Author:

  • Matt Simpson

Class Method Summary collapse

Class Method Details

.add(collection_name, hash) ⇒ Object

Inserts one record into a collection.

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • hash (Hash)

    a hash of data to be added to the collection


50
51
52
# File 'lib/hashy_db/interface.rb', line 50

def self.add(collection_name, hash)
  find_all(collection_name) << hash
end

.all_before(collection_name, field, value) ⇒ Object


126
127
128
129
130
# File 'lib/hashy_db/interface.rb', line 126

def self.all_before(collection_name, field, value)
  self.find_all(collection_name).select do |record|
    record[field] < value
  end
end

.array_contains(collection_name, key, value) ⇒ Array

Returns all records where the given key contains the given value

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • key (String)

    the key to find records by

  • value (*)

    the value to find a record by

Returns:

  • (Array)

    all records where the key contains the given value


198
199
200
201
202
# File 'lib/hashy_db/interface.rb', line 198

def self.array_contains(collection_name, key, value)
  find_all(collection_name).select do |x|
    x[key] && x[key].include?(value)
  end
end

.clearHash

Clears the data store.

Mainly used for rolling back the data store for tests.

Returns:

  • (Hash)

    the data store


225
226
227
# File 'lib/hashy_db/interface.rb', line 225

def self.clear
  data.clear
end

.containing_any(collection_name, key, values) ⇒ Array

Returns all records where the given key contains any of the values provided

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • key (String)

    the key to find the record by

  • values (Array)

    an array of values that the record could contain

Returns:

  • (Array)

    all records that contain any of the values given


182
183
184
185
186
187
188
189
190
# File 'lib/hashy_db/interface.rb', line 182

def self.containing_any(collection_name, key, values)
  find_all(collection_name).select do |x|
    if x[key].is_a?(Array)
      (x[key] & values).any?
    else
      values.include?(x[key])
    end
  end
end

.dataObject

Alias to Mince::HashyDb::DataStore.data


235
236
237
# File 'lib/hashy_db/interface.rb', line 235

def self.data
  DataStore.data
end

.delete_by_params(collection_name, params) ⇒ Object

Deletes a record that matches the given criteria from the data store.


67
68
69
70
71
# File 'lib/hashy_db/interface.rb', line 67

def self.delete_by_params(collection_name, params)
  find_all(collection_name).reject! do |record|
    params.all?{|k,v| record[k] == v }
  end
end

.delete_collection(collection_name) ⇒ Array

Deletes an entire collection

Parameters:

  • collection_name (Symbol)

    the collection to delete from the database

Returns:

  • (Array)

    the records in the collection being deleted


216
217
218
# File 'lib/hashy_db/interface.rb', line 216

def self.delete_collection(collection_name)
  data.delete(collection_name)
end

.delete_field(collection_name, field) ⇒ Object

Deletes a field from all records in the given collection

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • field (symbol)

    the name of the field to delete


40
41
42
43
44
# File 'lib/hashy_db/interface.rb', line 40

def self.delete_field(collection_name, field)
  find_all(collection_name).each do |row|
    row.delete(field)
  end
end

.find(collection_name, value) ⇒ Hash

Gets a record matching a key and value

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • value (*)

    a value the find a record by

Returns:

  • (Hash)

    a record that matches the given key and value


145
146
147
# File 'lib/hashy_db/interface.rb', line 145

def self.find(collection_name, value)
  find_all(collection_name).find { |x| x[primary_key] == value }
end

.find_all(collection_name) ⇒ Array

Gets all records for a collection

Parameters:

  • collection_name (Symbol)

    the name of the collection

Returns:

  • (Array)

    all records for the given collection name


136
137
138
# File 'lib/hashy_db/interface.rb', line 136

def self.find_all(collection_name)
  DataStore.collection(collection_name)
end

.generate_unique_id(salt) ⇒ String

Note:

This is necessary because different databases use different types of primary key values. Thus, each mince implementation must define how to generate a unique id.

Generates a unique ID for a database record

Parameters:

  • salt (#to_s)

    any object that responds to #to_s

Returns:

  • (String)

    A unique id based on the salt and the current time


28
29
30
# File 'lib/hashy_db/interface.rb', line 28

def self.generate_unique_id(salt)
  PrimaryKey.generate(salt)
end

.get_all_for_key_with_value(collection_name, key, value) ⇒ Array

Gets all records that have the value for a given key.

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • key (String)

    the key, or field, to get a record for

  • value (*)

    the value to get a record for

Returns:

  • (Array)

    an array of records that match the key and value


101
102
103
# File 'lib/hashy_db/interface.rb', line 101

def self.get_all_for_key_with_value(collection_name, key, value)
  find_all(collection_name).select { |a| a[key] == value }
end

.get_by_params(collection_name, hash) ⇒ Array

Gets all records that have all of the keys and values in the given hash.

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • hash (Hash)

    a hash to get a record for

Returns:

  • (Array)

    an array of all records matching the given hash for the collection


120
121
122
123
124
# File 'lib/hashy_db/interface.rb', line 120

def self.get_by_params(collection_name, hash)
  self.find_all(collection_name).select do |record|
    hash.all?{|k,v| record[k] == v }
  end
end

.get_for_key_with_value(collection_name, key, value) ⇒ Hash

Gets the first record that has the value for a given key.

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • key (String)

    the key to find a record by

  • value (String)

    the value to find a record by

Returns:

  • (Hash)

    a hash for the record found by the key and value in the collection


111
112
113
# File 'lib/hashy_db/interface.rb', line 111

def self.get_for_key_with_value(collection_name, key, value)
  self.get_all_for_key_with_value(collection_name, key, value)[0]
end

.increment_field_by_amount(collection_name, primary_key_value, field_name, amount) ⇒ void

This method returns an undefined value.

Increments or decrements the field by the given amount for the record for the given id.

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • primary_key_value (String)

    the id of the record

  • field_name (Symbol)

    the name of the field to update

  • amount (String)

    the amount to increment or decrement the field by


91
92
93
# File 'lib/hashy_db/interface.rb', line 91

def self.increment_field_by_amount(collection_name, primary_key_value, field_name, amount)
  find(collection_name, primary_key_value)[field_name] += amount
end

.insert(collection_name, data_to_insert) ⇒ Object

Replaces the entire collection with the given data

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • data (Array)

    an array of data hashes to replace and insert into the collection


208
209
210
# File 'lib/hashy_db/interface.rb', line 208

def self.insert(collection_name, data_to_insert)
  data[collection_name] = data_to_insert
end

.primary_keyObject


32
33
34
# File 'lib/hashy_db/interface.rb', line 32

def self.primary_key
  PrimaryKey.name
end

.push_to_array(collection_name, identifying_value, array_key, value_to_push) ⇒ Object

Pushes a value to a record's key that is an array

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • identifying_value (*)

    the value used to find the record

  • array_key (String)

    the field to push an array to

  • value_to_push (*)

    the value to push to the array


155
156
157
158
159
160
161
162
163
# File 'lib/hashy_db/interface.rb', line 155

def self.push_to_array(collection_name, identifying_value, array_key, value_to_push)
  record = find(collection_name, identifying_value)
  if (record[array_key])
    record[array_key] << value_to_push
  else
    record[array_key] = [value_to_push]
  end
  record[array_key].uniq!
end

.remove_from_array(collection_name, identifying_value, array_key, value_to_pop) ⇒ Object

Removes a value from a record's key that is an array

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • identifying_value (*)

    the value used to find the record

  • array_key (String)

    the field to push an array from

  • value_to_remove (*)

    the value to remove from the array


171
172
173
174
# File 'lib/hashy_db/interface.rb', line 171

def self.remove_from_array(collection_name, identifying_value, array_key, value_to_pop)
  record = find(collection_name, identifying_value)
  record[array_key].reject! { |x| x == value_to_pop }
end

.replace(collection_name, hash) ⇒ Object

Replaces a record in the collection based on the primary key's value. The hash must contain a key, defined by the primary_key_identifier method, with a value. If a record in the data store is found with that key and value, the entire record will be replaced with the given hash.

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • hash (Hash)

    a hash to replace the record in the collection with


60
61
62
63
64
# File 'lib/hashy_db/interface.rb', line 60

def self.replace(collection_name, hash)
  collection = find_all(collection_name)
  index = collection.index{ |object| object[primary_key] == hash[primary_key] }
  collection[index] =  hash
end

.set_data(hash) ⇒ Object

Alias to Mince::HashyDb::DataStore.data=


230
231
232
# File 'lib/hashy_db/interface.rb', line 230

def self.set_data(hash)
  DataStore.set_data hash
end

.update_field_with_value(collection_name, primary_key_value, field_name, new_value) ⇒ void

This method returns an undefined value.

Updates the field with a value for the record for the given id.

Parameters:

  • collection_name (Symbol)

    the name of the collection

  • primary_key_value (String)

    the id of the record

  • field_name (Symbol)

    the name of the field to update

  • field_value (String)

    the value to update the field to


80
81
82
# File 'lib/hashy_db/interface.rb', line 80

def self.update_field_with_value(collection_name, primary_key_value, field_name, new_value)
  find(collection_name, primary_key_value)[field_name] = new_value
end