hashy_db 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # What is this?
2
2
 
3
- Provides an inteface to store and retrieve data in a Hash.
3
+ Light weight ORM to persist data to a hash.
4
4
 
5
5
  # Why would you want this?
6
6
 
@@ -13,18 +13,9 @@ Example in a rails app:
13
13
 
14
14
  initializers/datastore.rb
15
15
 
16
- `ruby
16
+ `
17
17
  ::DB_HASH = {}
18
18
  `
19
-
20
- View the examples folder for an example implementation.
21
-
22
- # Todos
23
-
24
- - Make this into a gem
25
- - Rename *.get_one into *.find
26
- - Add a better readme
27
-
28
19
  # Contribute
29
20
 
30
21
  - fork into a topic branch, write specs, make a pull request.
@@ -4,12 +4,12 @@ require 'hashy_db/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "hashy_db"
7
- s.version = HashyDB::VERSION
7
+ s.version = HashyDb::VERSION
8
8
  s.authors = ["Matt Simpson", "Jason Mayer"]
9
9
  s.email = ["matt@railsgrammer.com", "jason.mayer@gmail.com"]
10
- s.homepage = "https://github.com/coffeencoke/HashyDB"
11
- s.summary = %q{Provides an inteface to store and retrieve data in a Hash.}
12
- s.description = %q{Provides an inteface to store and retrieve data in a Hash.}
10
+ s.homepage = "https://github.com/asynchrony/HashyDb"
11
+ s.summary = %q{Provides an interface to store and retrieve data in a Hash.}
12
+ s.description = %q{Provides an interface to store and retrieve data in a Hash.}
13
13
 
14
14
  s.rubyforge_project = "hashy_db"
15
15
 
@@ -1,2 +1 @@
1
- require 'hashy_db/data_store'
2
- require 'hashy_db/data_model'
1
+ require 'hashy_db/data_store'
@@ -1,25 +1,32 @@
1
1
  # SRP: Model with implementation on how to store each collection in a data store
2
2
  require 'singleton'
3
3
  require 'active_support/core_ext/module/delegation'
4
+ require 'digest'
4
5
 
5
- module HashyDB
6
+ module HashyDb
6
7
  class DataStore
7
8
  include Singleton
8
9
 
9
- attr_writer :data_store
10
+ def self.primary_key_identifier
11
+ :id
12
+ end
13
+
14
+ def self.generate_unique_id(salt)
15
+ Digest::SHA256.hexdigest("#{Time.current.utc}#{salt}")[0..6]
16
+ end
10
17
 
11
18
  def add(collection_name, hash)
12
- get(collection_name) << hash
19
+ find_all(collection_name) << hash
13
20
  end
14
21
 
15
22
  def replace(collection_name, hash)
16
- collection = get(collection_name)
23
+ collection = find_all(collection_name)
17
24
  index = collection.index{ |object| object[:id] == hash[:id] }
18
25
  collection[index] = hash
19
26
  end
20
27
 
21
28
  def get_all_for_key_with_value(collection_name, key, value)
22
- get(collection_name).select { |a| a[key] == value }
29
+ find_all(collection_name).select { |a| a[key] == value }
23
30
  end
24
31
 
25
32
  def get_for_key_with_value(collection_name, key, value)
@@ -27,21 +34,21 @@ module HashyDB
27
34
  end
28
35
 
29
36
  def get_by_params(collection_name, hash)
30
- get(collection_name).select do |record|
37
+ find_all(collection_name).select do |record|
31
38
  hash.all?{|k,v| record[k] == v }
32
39
  end
33
40
  end
34
41
 
35
- def get(collection_name)
42
+ def find_all(collection_name)
36
43
  data_store[collection_name] ||= []
37
44
  end
38
45
 
39
- def get_one(collection_name, key, value)
40
- get(collection_name).find { |x| x[key] == value }
46
+ def find(collection_name, key, value)
47
+ find_all(collection_name).find { |x| x[key] == value }
41
48
  end
42
49
 
43
50
  def push_to_array(collection_name, identifying_key, identifying_value, array_key, value_to_push)
44
- record = get_one(collection_name, identifying_key, identifying_value)
51
+ record = find(collection_name, identifying_key, identifying_value)
45
52
  if (record[array_key])
46
53
  record[array_key] << value_to_push
47
54
  else
@@ -51,13 +58,13 @@ module HashyDB
51
58
  end
52
59
 
53
60
  def remove_from_array(collection_name, identifying_key, identifying_value, array_key, value_to_pop)
54
- record = get_one(collection_name, identifying_key, identifying_value)
61
+ record = find(collection_name, identifying_key, identifying_value)
55
62
  record[array_key].reject! { |x| x == value_to_pop }
56
63
  end
57
64
 
58
65
  def containing_any(collection_name, key, values)
59
- get(collection_name).select do |x|
60
- result = if x[key].is_a?(Array)
66
+ find_all(collection_name).select do |x|
67
+ if x[key].is_a?(Array)
61
68
  (x[key] & values).any?
62
69
  else
63
70
  values.include?(x[key])
@@ -66,7 +73,7 @@ module HashyDB
66
73
  end
67
74
 
68
75
  def array_contains(collection_name, key, value)
69
- get(collection_name).select do |x|
76
+ find_all(collection_name).select do |x|
70
77
  x[key] && x[key].include?(value)
71
78
  end
72
79
  end
@@ -79,6 +86,10 @@ module HashyDB
79
86
  data_store[collection_name] = data
80
87
  end
81
88
 
89
+ def set_data_store(hash)
90
+ @data_store = hash
91
+ end
92
+
82
93
  private
83
94
 
84
95
  def data_store
@@ -1,3 +1,3 @@
1
- module HashyDB
2
- VERSION = '0.0.3'
1
+ module HashyDb
2
+ VERSION = '0.0.4'
3
3
  end
@@ -1,40 +1,64 @@
1
1
  require_relative '../../lib/hashy_db/data_store'
2
2
 
3
- describe HashyDB::DataStore do
4
- subject { HashyDB::DataStore.instance }
3
+ describe HashyDb::DataStore do
4
+ subject { HashyDb::DataStore.instance }
5
5
 
6
- let(:data1) { { id: 1, field_1: 'value 1', field_2: 3, field_3: [1,2,3], shared_between_1_and_2: 'awesome_value', :some_array => [1,2,3,4] } }
7
- let(:data2) { { id: 2, field_1: 'value 1.2', field_2: 6, shared_between_1_and_2: 'awesome_value', :some_array => [4,5,6] } }
8
- let(:data3) { { id: 3, field_1: 'value 3', field_2: 9, shared_between_1_and_2: 'not the same as 1 and 2', :some_array => [1,7] } }
6
+ let(:data1) { {id: 1, field_1: 'value 1', field_2: 3, field_3: [1, 2, 3], shared_between_1_and_2: 'awesome_value', :some_array => [1, 2, 3, 4]} }
7
+ let(:data2) { {id: 2, field_1: 'value 1.2', field_2: 6, shared_between_1_and_2: 'awesome_value', :some_array => [4, 5, 6]} }
8
+ let(:data3) { {id: 3, field_1: 'value 3', field_2: 9, shared_between_1_and_2: 'not the same as 1 and 2', :some_array => [1, 7]} }
9
9
 
10
10
  before do
11
- subject.data_store = {}
11
+ subject.set_data_store({})
12
+
12
13
  subject.insert(:some_collection, [data1, data2, data3])
13
14
  end
14
15
 
16
+ it 'has a primary key identifier' do
17
+ described_class.primary_key_identifier.should == :id
18
+ end
19
+
20
+ describe "Generating a primary key" do
21
+ let(:data_model_id) { full_data_model_id[0..6] }
22
+ let(:full_data_model_id) { '1234567891423456789' }
23
+ let(:utc) { mock 'utc'}
24
+ let(:time) { mock 'time', :utc => utc}
25
+ let(:salt) { mock 'salt to ensure uniqeness' }
26
+
27
+ before do
28
+ Digest::SHA256.stub(:hexdigest => full_data_model_id)
29
+ Time.stub(:current => time)
30
+ end
31
+
32
+ it 'should create a reasonably unique id' do
33
+ Digest::SHA256.should_receive(:hexdigest).with("#{utc}#{salt}").and_return(full_data_model_id)
34
+
35
+ described_class.generate_unique_id(salt).should == data_model_id
36
+ end
37
+ end
38
+
15
39
  it 'can write and read data to and from a collection' do
16
- data4 = { id: 3, field_1: 'value 3', field_2: 9, shared_between_1_and_2: 'not the same as 1 and 2', :some_array => [1,7] }
40
+ data4 = {id: 3, field_1: 'value 3', field_2: 9, shared_between_1_and_2: 'not the same as 1 and 2', :some_array => [1, 7]}
17
41
 
18
42
  subject.add(:some_collection, data4)
19
- subject.get(:some_collection).should == [data1, data2, data3, data4]
43
+ subject.find_all(:some_collection).should == [data1, data2, data3, data4]
20
44
  end
21
45
 
22
46
  it 'can replace a record' do
23
47
  data2[:field_1] = 'value modified'
24
48
  subject.replace(:some_collection, data2)
25
49
 
26
- subject.get_one(:some_collection, :id, 2)[:field_1].should == 'value modified'
50
+ subject.find(:some_collection, :id, 2)[:field_1].should == 'value modified'
27
51
  end
28
52
 
29
53
  it 'can get one document' do
30
- subject.get_one(:some_collection, :field_1, 'value 1').should == data1
31
- subject.get_one(:some_collection, :field_2, 6).should == data2
54
+ subject.find(:some_collection, :field_1, 'value 1').should == data1
55
+ subject.find(:some_collection, :field_2, 6).should == data2
32
56
  end
33
57
 
34
58
  it 'can clear the data store' do
35
59
  subject.clear
36
60
 
37
- subject.get(:some_collection).should == []
61
+ subject.find_all(:some_collection).should == []
38
62
  end
39
63
 
40
64
  it 'can get all records of a specific key value' do
@@ -46,7 +70,7 @@ describe HashyDB::DataStore do
46
70
  subject.containing_any(:some_collection, :some_array, [7, 2, 3]).should == [data1, data3]
47
71
  subject.containing_any(:some_collection, :id, [1, 2, 5]).should == [data1, data2]
48
72
  end
49
-
73
+
50
74
  it 'can get all records where the array includes a value' do
51
75
  subject.array_contains(:some_collection, :some_array, 1).should == [data1, data3]
52
76
  subject.array_contains(:some_collection, :some_array_2, 1).should == []
@@ -56,24 +80,24 @@ describe HashyDB::DataStore do
56
80
  subject.push_to_array(:some_collection, :id, 1, :field_3, 'add to existing array')
57
81
  subject.push_to_array(:some_collection, :id, 1, :new_field, 'add to new array')
58
82
 
59
- subject.get_one(:some_collection, :id, 1)[:field_3].should include('add to existing array')
60
- subject.get_one(:some_collection, :id, 1)[:new_field].should == ['add to new array']
83
+ subject.find(:some_collection, :id, 1)[:field_3].should include('add to existing array')
84
+ subject.find(:some_collection, :id, 1)[:new_field].should == ['add to new array']
61
85
  end
62
86
 
63
87
  it 'can remove a value from an array for a specific record' do
64
88
  subject.remove_from_array(:some_collection, :id, 1, :field_3, 2)
65
89
 
66
- subject.get_one(:some_collection, :id, 1)[:field_3].should_not include(2)
90
+ subject.find(:some_collection, :id, 1)[:field_3].should_not include(2)
67
91
  end
68
92
 
69
93
  it 'can get all records that match a given set of keys and values' do
70
94
  records = subject.get_by_params(:some_collection, field_1: 'value 1', shared_between_1_and_2: 'awesome_value')
71
95
  records.size.should be(1)
72
96
  records.first[:id].should == 1
73
- subject.get(:some_collection).size.should == 3
97
+ subject.find_all(:some_collection).size.should == 3
74
98
  end
75
99
 
76
100
  it 'can get a record for a specific key and value' do
77
101
  subject.get_for_key_with_value(:some_collection, :field_1, 'value 1').should == data1
78
102
  end
79
- end
103
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashy_db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-02-02 00:00:00.000000000 Z
13
+ date: 2012-03-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
17
- requirement: &70143401652780 !ruby/object:Gem::Requirement
17
+ requirement: &6801620 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '3.1'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70143401652780
25
+ version_requirements: *6801620
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rake
28
- requirement: &70143401651840 !ruby/object:Gem::Requirement
28
+ requirement: &6800440 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70143401651840
36
+ version_requirements: *6800440
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec
39
- requirement: &70143401650920 !ruby/object:Gem::Requirement
39
+ requirement: &6799580 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,8 +44,8 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70143401650920
48
- description: Provides an inteface to store and retrieve data in a Hash.
47
+ version_requirements: *6799580
48
+ description: Provides an interface to store and retrieve data in a Hash.
49
49
  email:
50
50
  - matt@railsgrammer.com
51
51
  - jason.mayer@gmail.com
@@ -60,19 +60,12 @@ files:
60
60
  - LICENSE.txt
61
61
  - README.md
62
62
  - Rakefile
63
- - examples/guitar.rb
64
- - examples/guitar_document.rb
65
63
  - hashy_db.gemspec
66
64
  - lib/hashy_db.rb
67
- - lib/hashy_db/data_model.rb
68
65
  - lib/hashy_db/data_store.rb
69
66
  - lib/hashy_db/version.rb
70
- - spec/examples/guitar_document_spec.rb
71
- - spec/examples/guitar_spec.rb
72
67
  - spec/lib/data_store_spec.rb
73
- - spec/spec_helper.rb
74
- - spec/support/examples/data_model_examples.rb
75
- homepage: https://github.com/coffeencoke/HashyDB
68
+ homepage: https://github.com/asynchrony/HashyDb
76
69
  licenses: []
77
70
  post_install_message:
78
71
  rdoc_options: []
@@ -95,10 +88,5 @@ rubyforge_project: hashy_db
95
88
  rubygems_version: 1.8.10
96
89
  signing_key:
97
90
  specification_version: 3
98
- summary: Provides an inteface to store and retrieve data in a Hash.
99
- test_files:
100
- - spec/examples/guitar_document_spec.rb
101
- - spec/examples/guitar_spec.rb
102
- - spec/lib/data_store_spec.rb
103
- - spec/spec_helper.rb
104
- - spec/support/examples/data_model_examples.rb
91
+ summary: Provides an interface to store and retrieve data in a Hash.
92
+ test_files: []
@@ -1,16 +0,0 @@
1
- require_relative 'guitar_document'
2
-
3
- class Guitar
4
- attr_reader :brand, :price, :type, :color, :id
5
-
6
- def initialize(attrs)
7
- @brand = attrs[:brand]
8
- @price = attrs[:price]
9
- @type = attrs[:type]
10
- @color = attrs[:color]
11
- end
12
-
13
- def store
14
- @id = GuitarDocument.store(self)
15
- end
16
- end
@@ -1,8 +0,0 @@
1
- require_relative '../lib/hashy_db/data_model'
2
-
3
- class GuitarDocument
4
- include HashyDB::DataModel
5
-
6
- data_collection :guitars
7
- data_fields :brand, :price, :type, :color
8
- end
@@ -1,151 +0,0 @@
1
- # SRP: Interface to read and write to and from the data store class for a specific collection
2
-
3
- require_relative '../hashy_db/data_store'
4
-
5
- require 'digest'
6
- require 'active_support/concern'
7
-
8
- module HashyDB
9
- module DataModel
10
- extend ActiveSupport::Concern
11
-
12
- included do
13
- attr_reader :id, :model
14
- end
15
-
16
- module ClassMethods
17
- def data_fields(*fields)
18
- create_data_fields(*fields) if fields.any?
19
- @data_fields
20
- end
21
-
22
- def data_collection(collection_name = nil)
23
- set_data_collection(collection_name) if collection_name
24
- @data_collection
25
- end
26
-
27
- def store(model)
28
- new(model).tap do |p|
29
- p.add_to_data_store
30
- end.id
31
- end
32
-
33
- def update(model)
34
- new(model).tap do |p|
35
- p.replace_in_data_store
36
- end
37
- end
38
-
39
- def remove_from_array(id, field, value)
40
- HashyDB::DataStore.instance.remove_from_array(data_collection, :id, id, field, value)
41
- end
42
-
43
- def push_to_array(id, field, value)
44
- HashyDB::DataStore.instance.push_to_array(data_collection, :id, id, field, value)
45
- end
46
-
47
- def get_one(id)
48
- HashyDB::DataStore.instance.get_one(data_collection, :id, id)
49
- end
50
-
51
- def by_field(field, value)
52
- DataStore.instance.get_for_key_with_value(data_collection, field, value)
53
- end
54
-
55
- def all
56
- HashyDB::DataStore.instance.get(data_collection)
57
- end
58
-
59
- def all_by_field(field, value)
60
- HashyDB::DataStore.instance.get_all_for_key_with_value(data_collection, field, value)
61
- end
62
-
63
- def all_by_fields(hash)
64
- HashyDB::DataStore.instance.get_by_params(data_collection, hash)
65
- end
66
-
67
- def find_by_fields(hash)
68
- all_by_fields(hash).first
69
- end
70
-
71
- def containing_any(field, values)
72
- HashyDB::DataStore.instance.containing_any(data_collection, field, values)
73
- end
74
-
75
- def array_contains(field, value)
76
- HashyDB::DataStore.instance.array_contains(data_collection, field, value)
77
- end
78
-
79
- private
80
-
81
- def set_data_collection(collection_name)
82
- @data_collection = collection_name
83
- end
84
-
85
- def create_data_fields(*fields)
86
- attr_accessor *fields
87
- @data_fields = fields
88
- end
89
- end
90
-
91
- def initialize(model)
92
- @model = model
93
- set_data_field_values
94
- set_id
95
- end
96
-
97
- def data_fields
98
- self.class.data_fields
99
- end
100
-
101
- def data_collection
102
- self.class.data_collection
103
- end
104
-
105
- def add_to_data_store
106
- HashyDB::DataStore.instance.add(data_collection, attributes)
107
- end
108
-
109
- def replace_in_data_store
110
- HashyDB::DataStore.instance.replace(data_collection, attributes)
111
- end
112
-
113
- private
114
-
115
- def attributes
116
- { id: id }.merge(data_field_attributes)
117
- end
118
-
119
- def data_field_attributes
120
- {}.tap do |hash|
121
- data_fields.each do |field|
122
- hash[field] = self.send(field)
123
- end
124
- end
125
- end
126
-
127
- def set_id
128
- @id = if(model.respond_to?(:id) && model.id)
129
- model.id
130
- else
131
- new_hash(Time.current)
132
- end
133
- end
134
-
135
- def new_hash(salt)
136
- Digest::SHA256.hexdigest("#{salt}#{model}")[0..6]
137
- end
138
-
139
- def set_data_field_values
140
- data_fields.each { |field| set_data_field_value(field) }
141
- end
142
-
143
- def set_data_field_value(field)
144
- self.send("#{field}=", model.send(field)) if field_exists?(field)
145
- end
146
-
147
- def field_exists?(field)
148
- model.respond_to?(field) && !model.send(field).nil?
149
- end
150
- end
151
- end
@@ -1,17 +0,0 @@
1
- require_relative '../../examples/guitar_document'
2
-
3
- require_relative '../support/examples/data_model_examples'
4
-
5
- describe GuitarDocument do
6
- let(:collection_name) { :guitars }
7
- let(:data_field_attributes) do
8
- {
9
- brand: 'a brand everyone knows',
10
- price: 'a price you save up for',
11
- type: 'the kind you want',
12
- color: 'should be your favorite'
13
- }
14
- end
15
-
16
- it_behaves_like 'a data model'
17
- end
@@ -1,32 +0,0 @@
1
- require_relative '../../examples/guitar'
2
-
3
- describe Guitar do
4
- let(:attrs) { { brand: 'Gibson', price: 3399.99, type: 'Les Paul', color: 'Mahogany' } }
5
-
6
- subject { described_class.new(attrs)}
7
-
8
- its(:brand){ should == attrs[:brand] }
9
- its(:price){ should == attrs[:price] }
10
- its(:type){ should == attrs[:type] }
11
- its(:color){ should == attrs[:color] }
12
-
13
- describe "storing the guitar" do
14
- let(:mock_id) { mock 'unique id for the guitar' }
15
-
16
- before do
17
- GuitarDocument.stub(store: mock_id)
18
- end
19
-
20
- it 'uses the guitar document to store it' do
21
- GuitarDocument.should_receive(:store).with(subject).and_return(mock_id)
22
-
23
- subject.store
24
- end
25
-
26
- it 'sets the id received from the document to the guitar so we can find it later' do
27
- subject.store
28
-
29
- subject.id.should == mock_id
30
- end
31
- end
32
- end
@@ -1,7 +0,0 @@
1
- require 'rspec'
2
- require 'hashy_db'
3
-
4
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
5
-
6
- RSpec.configure do |config|
7
- end
@@ -1,157 +0,0 @@
1
- require 'digest'
2
- require_relative '../../../lib/hashy_db/data_store'
3
-
4
- shared_examples_for 'a data model' do
5
- let(:mock_data_store) { mock 'data store data model' }
6
-
7
- before do
8
- HashyDB::DataStore.stub(:instance => mock_data_store)
9
- end
10
-
11
- describe "storing a data model" do
12
- let(:current_time) { mock 'current time' }
13
- let(:mock_data_model_string_rep) { mock 'data model string representation used for generating an id' }
14
- let(:data_model_id) { full_data_model_id[0..6] }
15
- let(:full_data_model_id) { '1234567891423456789' }
16
- let(:mock_data_model) { mock 'a data model', data_field_attributes }
17
-
18
- before do
19
- Time.stub(:current => current_time)
20
- Digest::SHA256.stub(:hexdigest => full_data_model_id)
21
- mock_data_store.stub(:add)
22
- end
23
-
24
- it 'creates a unique hash for the id' do
25
- Digest::SHA256.should_receive(:hexdigest).with("#{current_time}#{mock_data_model}").and_return(full_data_model_id)
26
-
27
- described_class.store(mock_data_model)
28
- end
29
-
30
- it 'adds the data model to the db store' do
31
- mock_data_store.should_receive(:add).with(collection_name, {id: data_model_id}.merge(data_field_attributes))
32
-
33
- described_class.store(mock_data_model)
34
- end
35
- end
36
-
37
- describe "updating a data model" do
38
- let(:data_model_id) { '1234567' }
39
- let(:mock_model) { mock 'a model', {:id => data_model_id}.merge(data_field_attributes) }
40
-
41
- before do
42
- mock_data_store.stub(:replace)
43
- end
44
-
45
- it 'replaces the data model in the db store' do
46
- mock_data_store.should_receive(:replace).with(collection_name, {id: data_model_id}.merge(data_field_attributes))
47
-
48
- described_class.update(mock_model)
49
- end
50
- end
51
-
52
- describe "pushing a value to an array for a data model" do
53
- let(:data_model_id) { '1234567' }
54
-
55
- it 'replaces the data model in the db store' do
56
- mock_data_store.should_receive(:push_to_array).with(collection_name, :id, data_model_id, :array_field, 'some value')
57
-
58
- described_class.push_to_array(data_model_id, :array_field, 'some value')
59
- end
60
- end
61
-
62
- describe "getting all data models with a specific value for a field" do
63
- let(:mock_data_models) { mock 'data models in the data store' }
64
- subject { described_class.array_contains(:some_field, 'some value') }
65
-
66
- it 'returns the stored data models with the requested field / value' do
67
- mock_data_store.should_receive(:array_contains).with(collection_name, :some_field, 'some value').and_return(mock_data_models)
68
-
69
- subject.should == mock_data_models
70
- end
71
- end
72
-
73
- describe "removing a value from an array for a data model" do
74
- let(:data_model_id) { '1234567' }
75
-
76
- it 'removes the value from the array' do
77
- mock_data_store.should_receive(:remove_from_array).with(collection_name, :id, data_model_id, :array_field, 'some value')
78
-
79
- described_class.remove_from_array(data_model_id, :array_field, 'some value')
80
- end
81
- end
82
-
83
- describe 'getting all of the data models' do
84
- let(:mock_data_models) { mock 'data models in the data store' }
85
-
86
- it 'returns the stored data models' do
87
- mock_data_store.should_receive(:get).with(collection_name).and_return(mock_data_models)
88
-
89
- described_class.all.should == mock_data_models
90
- end
91
- end
92
-
93
- describe "getting a data model by a specific field" do
94
- let(:mock_data_model) { mock 'data model in the data store' }
95
-
96
- it 'returns the stored data models' do
97
- mock_data_store.should_receive(:get_for_key_with_value).with(collection_name, :field, 'some value').and_return(mock_data_model)
98
-
99
- described_class.by_field(:field, 'some value').should == mock_data_model
100
- end
101
- end
102
-
103
- describe "getting all the data fields by a parameter hash" do
104
- let(:mock_data_models) { mock 'some data model'}
105
- let(:sample_hash) { {field1: nil, field2: 'not nil' }}
106
-
107
- it 'passes the hash to the DataStore' do
108
- mock_data_store.should_receive(:get_by_params).with(collection_name, sample_hash).and_return(mock_data_models)
109
-
110
- described_class.all_by_fields(sample_hash).should == mock_data_models
111
- end
112
- end
113
-
114
- describe "getting a record by a set of key values" do
115
- let(:mock_data_model) { mock 'some data model' }
116
- let(:mock_data_models) { [mock_data_model]}
117
- let(:sample_hash) { {field1: nil, field2: 'not nil' }}
118
-
119
- it 'passes the hash to the DataStore' do
120
- mock_data_store.should_receive(:get_by_params).with(collection_name, sample_hash).and_return(mock_data_models)
121
-
122
- described_class.find_by_fields(sample_hash).should == mock_data_model
123
- end
124
- end
125
-
126
- describe "getting all of the data models for a where a field contains any value of a given array of values" do
127
- let(:mock_data_models) { mock 'data models in the data store' }
128
- subject { described_class.containing_any(:some_field, ['value 1', 'value 2']) }
129
-
130
- it 'returns the stored data models' do
131
- mock_data_store.should_receive(:containing_any).with(collection_name, :some_field, ['value 1', 'value 2']).and_return(mock_data_models)
132
-
133
- subject.should == mock_data_models
134
- end
135
- end
136
-
137
- describe "getting all data models with a specific value for a field" do
138
- let(:mock_data_models) { mock 'data models in the data store' }
139
- subject { described_class.all_by_field(:some_field, 'some value') }
140
-
141
- it 'returns the stored data models with the requested field / value' do
142
- mock_data_store.should_receive(:get_all_for_key_with_value).with(collection_name, :some_field, 'some value').and_return(mock_data_models)
143
-
144
- subject.should == mock_data_models
145
- end
146
- end
147
-
148
- describe 'getting a specific data model' do
149
- let(:mock_data_model) { mock 'data_model', :id => 'id' }
150
-
151
- it 'returns the data model from the data store' do
152
- mock_data_store.should_receive(:get_one).with(collection_name, :id, mock_data_model.id).and_return(mock_data_model)
153
-
154
- described_class.get_one(mock_data_model.id).should == mock_data_model
155
- end
156
- end
157
- end