entity_store 0.2.8 → 0.2.9
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.
- checksums.yaml +4 -4
- data/lib/entity_store/attributes.rb +18 -0
- data/lib/entity_store/version.rb +1 -1
- data/spec/entity_store/entity_spec.rb +91 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d20e8cbde446a0c8079689f003aae7b5cb06a113
|
4
|
+
data.tar.gz: 7059c4b37172bb89699c5f9566fad552e31f345e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dafa949cc2b77783ce442d14a2f2c28f96d19476c66ba15407e7208cfe469317c2e4b3bb8e9dcbf1fec86ac7cd5d602dcfaa143a453c0bff9c42d10f7ecbeabc
|
7
|
+
data.tar.gz: 9bb78b4637fac541c3ebf0a7b0b7d7d5ef265e285936e1b0b8eaa6f8ab6d478cd83cdb9ac30f710552c03209920ee1a720e248f8c11169dfbb5e67909966552f
|
@@ -43,6 +43,24 @@ module EntityStore
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
46
|
+
|
47
|
+
def entity_value_dictionary_attribute name, klass
|
48
|
+
define_method("#{name}_dictionary") {
|
49
|
+
instance_variable_get("@_#{name}_dictionary") || instance_variable_set("@_#{name}_dictionary", {})
|
50
|
+
}
|
51
|
+
define_method("#{name}_dictionary=") do |value|
|
52
|
+
value.each_pair do |key, item|
|
53
|
+
case item
|
54
|
+
when Hash
|
55
|
+
send("#{name}_dictionary")[key] = klass.new(item)
|
56
|
+
when klass
|
57
|
+
send("#{name}_dictionary")[key] = item
|
58
|
+
else
|
59
|
+
raise ArgumentError.new("#{item.class.name} not supported. Expecting #{klass.name}")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
46
64
|
end
|
47
65
|
end
|
48
66
|
end
|
data/lib/entity_store/version.rb
CHANGED
@@ -13,6 +13,7 @@ class DummyEntity
|
|
13
13
|
|
14
14
|
attr_accessor :name, :description, :members
|
15
15
|
entity_value_array_attribute :things, ThingEntityValue
|
16
|
+
entity_value_dictionary_attribute :other_things, ThingEntityValue
|
16
17
|
end
|
17
18
|
|
18
19
|
describe Entity do
|
@@ -79,7 +80,8 @@ describe Entity do
|
|
79
80
|
before(:each) do
|
80
81
|
@entity = DummyEntity.new(:id => @id = random_object_id, :club_id => @club_id = random_string,
|
81
82
|
:user_id => @user_id = random_string, :name => @name = random_string, :version => @version = random_integer,
|
82
|
-
:members => [])
|
83
|
+
:members => [], things: [ ThingEntityValue.new(name: random_string), ThingEntityValue.new(name: random_string) ],
|
84
|
+
other_things_dictionary: { random_string => ThingEntityValue.new(name: random_string) } )
|
83
85
|
end
|
84
86
|
|
85
87
|
subject { @entity.attributes }
|
@@ -87,9 +89,33 @@ describe Entity do
|
|
87
89
|
it "returns a hash of the attributes" do
|
88
90
|
subject.should eq({
|
89
91
|
:id => @id, :version => @version, :name => @name, :club_id => @club_id,
|
90
|
-
:user_id => @user_id, :description => nil, :members => [],
|
92
|
+
:user_id => @user_id, :description => nil, :members => [],
|
93
|
+
:things => @entity.things.map { |t| { name: t.name } },
|
94
|
+
:other_things_dictionary => { @entity.other_things_dictionary.keys.first => { name: @entity.other_things_dictionary.values.first.name }}
|
91
95
|
})
|
92
96
|
end
|
97
|
+
context "when initialise with attributes" do
|
98
|
+
|
99
|
+
subject { DummyEntity.new(@entity.attributes) }
|
100
|
+
|
101
|
+
it "should set simple attributes" do
|
102
|
+
subject.id.should eq(@entity.id)
|
103
|
+
end
|
104
|
+
it "should set entity value array attributes" do
|
105
|
+
actual = subject
|
106
|
+
actual.things.count.should eq(@entity.things.count)
|
107
|
+
actual.things.each_with_index do |item, i|
|
108
|
+
item.should eq(@entity.things[i])
|
109
|
+
end
|
110
|
+
end
|
111
|
+
it "should set entity value dictionary attributes" do
|
112
|
+
actual = subject
|
113
|
+
actual.other_things_dictionary.keys.count.should eq(@entity.other_things_dictionary.keys.count)
|
114
|
+
actual.other_things_dictionary.each_pair do |k,v|
|
115
|
+
v.should eq(@entity.other_things_dictionary[k])
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
93
119
|
end
|
94
120
|
|
95
121
|
describe ".entity_value_array_attribute" do
|
@@ -162,4 +188,67 @@ describe Entity do
|
|
162
188
|
end
|
163
189
|
end
|
164
190
|
|
191
|
+
describe ".entity_value_dictionary_attribute" do
|
192
|
+
let(:ids) { [ random_string, random_string ] }
|
193
|
+
let(:entity) { DummyEntity.new }
|
194
|
+
|
195
|
+
describe "setter" do
|
196
|
+
context "with hashes" do
|
197
|
+
let(:items) { { ids[0] => { name: random_string }, ids[1] => { name: random_string } } }
|
198
|
+
|
199
|
+
before(:each) do
|
200
|
+
entity.other_things_dictionary = items
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should create an items of the correct type" do
|
204
|
+
ids.each do |id| entity.other_things_dictionary[id].should be_an_instance_of(ThingEntityValue) end
|
205
|
+
end
|
206
|
+
it "should set the value" do
|
207
|
+
ids.each do |id| entity.other_things_dictionary[id].name.should eq(items[id][:name]) end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
context "with a hash of typed " do
|
211
|
+
let(:items) { { ids[0] => ThingEntityValue.new(name: random_string), ids[1] => ThingEntityValue.new(name: random_string) } }
|
212
|
+
|
213
|
+
before(:each) do
|
214
|
+
entity.other_things_dictionary = items
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should set items" do
|
218
|
+
ids.each do |id| entity.other_things_dictionary[id].name.should eq(items[id].name) end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
context "when something else in array" do
|
222
|
+
let(:items) { { ids[0] => random_string, ids[1] => random_string } }
|
223
|
+
|
224
|
+
it "should raise and argument error" do
|
225
|
+
expect { entity.other_things_dictionary = items }.to raise_error(ArgumentError)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
describe "getter" do
|
231
|
+
context "when nothing set" do
|
232
|
+
it "should return and empty array" do
|
233
|
+
entity.other_things_dictionary[random_string].should be_nil
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
describe "hash initialisation, ie from snapshot" do
|
239
|
+
let(:attributes) { { other_things_dictionary: { ids[0] => { name: random_string }, ids[1] => { name: random_string } } } }
|
240
|
+
|
241
|
+
subject { DummyEntity.new(attributes) }
|
242
|
+
|
243
|
+
it "should create an array of the correct type" do
|
244
|
+
entity = subject
|
245
|
+
ids.each do |id| entity.other_things_dictionary[id].should be_an_instance_of(ThingEntityValue) end
|
246
|
+
end
|
247
|
+
it "should set the value" do
|
248
|
+
entity = subject
|
249
|
+
ids.each do |id| entity.other_things_dictionary[id].name.should eq(attributes[:other_things_dictionary][id][:name]) end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
165
254
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: entity_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Bird
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongo
|