entity_store 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed21397eb12f394675c2a3ac4aab4ebc79abf7ee
4
- data.tar.gz: 2ecf790f3ccf4d23479b7802335fbfea11c70991
3
+ metadata.gz: 0296c4214324ed1c555792e750280e78aeeb4ed3
4
+ data.tar.gz: beb1a9d0ec5ba5c7f59e8f01a5e0870f02d242b3
5
5
  SHA512:
6
- metadata.gz: b59e19df9f215be126f02b00a4930bb3b60609eab77fbe2c01549a0f7dc2b891abd1a24e15dbd626c1573206258ef82534c3b95840424388ca0c426fdaa82a56
7
- data.tar.gz: 69091354b3f787542230c721898930de5f5328a76d601aca8fbfab77deca1762d18a490725aabe797d39c024130a8dda43ea000430b43af9a19b7eb3bcd16303
6
+ metadata.gz: 72529749b3e8647cc5b71aff136c12e4997c2eebfb2b19879e95624e9cdf94adbbf6eeeedfeded8d469017e867e1582d5ee9cf136c1fec5ba5fa9d920fcd447e
7
+ data.tar.gz: 934cba74d24007f7d32241485b89f3c66c82b624cb92b5d399cb6e60181dc6403796df8b629a95bf2931abba42539172f654f3a51370185e3f61759b3577b023
@@ -1,20 +1,20 @@
1
1
  module EntityStore
2
2
  module Attributes
3
- def self.included(klass)
3
+ def self.included klass
4
4
  klass.class_eval do
5
5
  extend ClassMethods
6
6
  end
7
7
  end
8
8
 
9
9
  module ClassMethods
10
- def entity_value_attribute(name, klass)
10
+ def entity_value_attribute name, klass
11
11
  define_method(name) { instance_variable_get("@#{name}") }
12
12
  define_method("#{name}=") do |value|
13
13
  instance_variable_set("@#{name}", self.class._eval_entity_value_setter(value, klass))
14
14
  end
15
15
  end
16
16
 
17
- def _eval_entity_value_setter(value, klass)
17
+ def _eval_entity_value_setter value, klass
18
18
  case value
19
19
  when Array
20
20
  klass.new(Hash[*value.flatten])
@@ -25,6 +25,24 @@ module EntityStore
25
25
  end
26
26
  end
27
27
 
28
+ def entity_value_array_attribute name, klass
29
+ define_method(name) {
30
+ instance_variable_get("@_#{name}") || instance_variable_set("@_#{name}", [])
31
+ }
32
+
33
+ define_method("#{name}=") do |value|
34
+ value.each do |item|
35
+ case item
36
+ when Hash
37
+ send(name) << klass.new(item)
38
+ when klass
39
+ send(name) << item
40
+ else
41
+ raise ArgumentError.new("#{item.class.name} not supported. Expecting #{klass.name}")
42
+ end
43
+ end
44
+ end
45
+ end
28
46
  end
29
47
  end
30
48
  end
@@ -65,6 +65,16 @@ module EntityStore
65
65
  entities.update({'_id' => BSON::ObjectId.from_string(id)}, { '$unset' => { 'snapshot' => 1}})
66
66
  end
67
67
 
68
+ # Public - remove all snapshots
69
+ #
70
+ # type - String optional class name for the entity
71
+ #
72
+ def remove_snapshots type=nil
73
+ query = {}
74
+ query['_type'] = type if type
75
+ entities.update(query, { '$unset' => { 'snapshot' => 1 } }, { multi: true })
76
+ end
77
+
68
78
  def add_event(event)
69
79
  events.insert({'_type' => event.class.name, '_entity_id' => BSON::ObjectId.from_string(event.entity_id) }.merge(event.attributes) ).to_s
70
80
  end
@@ -48,6 +48,10 @@ module EntityStore
48
48
  storage_client.remove_entity_snapshot(id)
49
49
  end
50
50
 
51
+ def remove_snapshots type=nil
52
+ storage_client.remove_snapshots type
53
+ end
54
+
51
55
  def add_events(entity)
52
56
  entity.pending_events.each do |e|
53
57
  e.entity_id = entity.id.to_s
@@ -1,3 +1,3 @@
1
1
  module EntityStore
2
- VERSION = "0.2.7".freeze
2
+ VERSION = "0.2.8".freeze
3
3
  end
@@ -1,12 +1,18 @@
1
1
  require "spec_helper"
2
2
 
3
+ class ThingEntityValue
4
+ include EntityValue
5
+
6
+ attr_accessor :name
7
+ end
8
+
3
9
  class DummyEntity
4
10
  include Entity
5
11
 
6
12
  related_entities :club, :user
7
13
 
8
14
  attr_accessor :name, :description, :members
9
-
15
+ entity_value_array_attribute :things, ThingEntityValue
10
16
  end
11
17
 
12
18
  describe Entity do
@@ -79,7 +85,81 @@ describe Entity do
79
85
  subject { @entity.attributes }
80
86
 
81
87
  it "returns a hash of the attributes" do
82
- subject.should eq({:id => @id, :version => @version, :name => @name, :club_id => @club_id, :user_id => @user_id, :description => nil, :members => []})
88
+ subject.should eq({
89
+ :id => @id, :version => @version, :name => @name, :club_id => @club_id,
90
+ :user_id => @user_id, :description => nil, :members => [], :things => []
91
+ })
92
+ end
93
+ end
94
+
95
+ describe ".entity_value_array_attribute" do
96
+ let(:entity) { DummyEntity.new }
97
+
98
+ describe "setter" do
99
+ context "with array of hashes" do
100
+ let(:items) { [{ name: random_string }, { name: random_string }] }
101
+
102
+ before(:each) do
103
+ entity.things = items
104
+ end
105
+
106
+ it "should create the number of items" do
107
+ entity.things.count.should eq(items.count)
108
+ end
109
+ it "should create an array of the correct type" do
110
+ entity.things.each do |item| item.should be_an_instance_of(ThingEntityValue) end
111
+ end
112
+ it "should set the value" do
113
+ entity.things.each_with_index do |item, i| item.name.should eq(items[i][:name]) end
114
+ end
115
+ end
116
+ context "with an array of matching items" do
117
+ let(:items) { [ ThingEntityValue.new(name: random_string), ThingEntityValue.new(name: random_string)] }
118
+
119
+ before(:each) do
120
+ entity.things = items
121
+ end
122
+
123
+ it "should create the number of items" do
124
+ entity.things.count.should eq(items.count)
125
+ end
126
+ it "should set items" do
127
+ entity.things.each_with_index do |item, i| item.should be(items[i]) end
128
+ end
129
+ end
130
+ context "when something else in array" do
131
+ let(:items) { [ random_string, random_string ] }
132
+
133
+ it "should raise and argument error" do
134
+ expect { entity.things = items }.to raise_error(ArgumentError)
135
+ end
136
+ end
137
+ end
138
+
139
+ describe "getter" do
140
+ context "when nothing set" do
141
+ it "should return and empty array" do
142
+ entity.things.count.should eq(0)
143
+ end
144
+ end
145
+ end
146
+
147
+ describe "hash initialisation, ie from snapshot" do
148
+ let(:attributes) { { things: [ { name: random_string }, { name: random_string } ] } }
149
+
150
+ subject { DummyEntity.new(attributes) }
151
+
152
+ it "should create the number of items" do
153
+ subject.things.count.should eq(attributes[:things].count)
154
+ end
155
+ it "should create an array of the correct type" do
156
+ subject.things.each do |item| item.should be_an_instance_of(ThingEntityValue) end
157
+ end
158
+ it "should set the value" do
159
+ subject.things.each_with_index do |item, i| item.name.should eq(attributes[:things][i][:name]) end
160
+ end
161
+
83
162
  end
84
163
  end
164
+
85
165
  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.7
4
+ version: 0.2.8
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-10 00:00:00.000000000 Z
11
+ date: 2013-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo