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'
Class Method Summary collapse
-
.add(collection_name, hash) ⇒ Object
Inserts one record into a collection.
- .all_before(collection_name, field, value) ⇒ Object
-
.array_contains(collection_name, key, value) ⇒ Array
Returns all records where the given key contains the given value.
-
.clear ⇒ Hash
Clears the data store.
-
.containing_any(collection_name, key, values) ⇒ Array
Returns all records where the given key contains any of the values provided.
-
.data ⇒ Object
Alias to Mince::HashyDb::DataStore.data.
-
.delete_by_params(collection_name, params) ⇒ Object
Deletes a record that matches the given criteria from the data store.
-
.delete_collection(collection_name) ⇒ Array
Deletes an entire collection.
-
.delete_field(collection_name, field) ⇒ Object
Deletes a field from all records in the given collection.
-
.find(collection_name, value) ⇒ Hash
Gets a record matching a key and value.
-
.find_all(collection_name) ⇒ Array
Gets all records for a collection.
-
.generate_unique_id(salt) ⇒ String
Generates a unique ID for a database record.
-
.get_all_for_key_with_value(collection_name, key, value) ⇒ Array
Gets all records that have the value for a given key.
-
.get_by_params(collection_name, hash) ⇒ Array
Gets all records that have all of the keys and values in the given hash.
-
.get_for_key_with_value(collection_name, key, value) ⇒ Hash
Gets the first record that has the value for a given key.
-
.increment_field_by_amount(collection_name, primary_key_value, field_name, amount) ⇒ void
Increments or decrements the field by the given amount for the record for the given id.
-
.insert(collection_name, data_to_insert) ⇒ Object
Replaces the entire collection with the given data.
- .primary_key ⇒ Object
-
.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.
-
.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.
-
.replace(collection_name, hash) ⇒ Object
Replaces a record in the collection based on the primary key's value.
-
.set_data(hash) ⇒ Object
Alias to Mince::HashyDb::DataStore.data=.
-
.update_field_with_value(collection_name, primary_key_value, field_name, new_value) ⇒ void
Updates the field with a value for the record for the given id.
Class Method Details
.add(collection_name, hash) ⇒ Object
Inserts one record into a 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
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 |
.clear ⇒ Hash
Clears the data store.
Mainly used for rolling back the data store for tests.
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
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 |
.data ⇒ Object
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
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
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
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
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
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
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.
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.
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.
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.
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
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_key ⇒ Object
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
155 156 157 158 159 160 161 162 163 |
# File 'lib/hashy_db/interface.rb', line 155 def self.push_to_array(collection_name, , array_key, value_to_push) record = find(collection_name, ) 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
171 172 173 174 |
# File 'lib/hashy_db/interface.rb', line 171 def self.remove_from_array(collection_name, , array_key, value_to_pop) record = find(collection_name, ) 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.
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.
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 |