edr 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/edr.gemspec +1 -1
- data/lib/edr/model.rb +22 -13
- data/lib/edr/repository.rb +2 -1
- data/lib/edr/version.rb +1 -1
- data/spec/edr/data_validator_spec.rb +3 -3
- data/spec/edr/model_spec.rb +3 -10
- data/spec/edr/repository_spec.rb +6 -16
- data/spec/test_data.rb +2 -6
- metadata +3 -3
data/.gitignore
CHANGED
data/edr.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |gem|
|
|
7
7
|
gem.name = "edr"
|
8
8
|
gem.version = Edr::VERSION
|
9
9
|
gem.authors = ["Victor Savkin", "Matt Briggs", "Clemens Park", "Justin Fitzsimmons"]
|
10
|
-
gem.email = ["vsavkin@nulogy.com", "
|
10
|
+
gem.email = ["vsavkin@nulogy.com", "matt@mattbriggs.net", "clemensp@nulogy.com", "justinf@nulogy.com"]
|
11
11
|
gem.description = %q{Entity Data-object Repository framework}
|
12
12
|
gem.summary = %q{Separate persistence from the domain model.}
|
13
13
|
gem.homepage = ""
|
data/lib/edr/model.rb
CHANGED
@@ -7,15 +7,7 @@ module Edr
|
|
7
7
|
base.extend ::Forwardable
|
8
8
|
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
def initialize _data = _new_instance
|
13
|
-
if _data.kind_of?(Hash)
|
14
|
-
@_data = _new_instance _data
|
15
|
-
else
|
16
|
-
@_data = _data
|
17
|
-
end
|
18
|
-
end
|
10
|
+
attr_writer :_data
|
19
11
|
|
20
12
|
def mass_assign(attributes)
|
21
13
|
attributes.each do |k, v|
|
@@ -27,6 +19,10 @@ module Edr
|
|
27
19
|
_data.as_json(options)
|
28
20
|
end
|
29
21
|
|
22
|
+
def _data
|
23
|
+
@_data ||= self.class._new_instance
|
24
|
+
end
|
25
|
+
|
30
26
|
protected
|
31
27
|
|
32
28
|
def repository
|
@@ -49,15 +45,24 @@ module Edr
|
|
49
45
|
else
|
50
46
|
return nil if association.nil?
|
51
47
|
model_class = Registry.model_class_for(association.class)
|
52
|
-
model_class.
|
48
|
+
model_class.build association
|
53
49
|
end
|
54
50
|
end
|
55
51
|
|
56
|
-
def _new_instance hash = {}
|
57
|
-
Registry.data_class_for(self.class).new hash
|
58
|
-
end
|
59
52
|
|
60
53
|
module ClassMethods
|
54
|
+
def build(data = _new_instance)
|
55
|
+
instance = new
|
56
|
+
|
57
|
+
if data.kind_of?(Hash)
|
58
|
+
instance._data = _new_instance(data)
|
59
|
+
else
|
60
|
+
instance._data = data
|
61
|
+
end
|
62
|
+
|
63
|
+
return instance
|
64
|
+
end
|
65
|
+
|
61
66
|
def fields *field_names
|
62
67
|
field_names.each do |field_name|
|
63
68
|
def_delegators :_data, field_name
|
@@ -72,6 +77,10 @@ module Edr
|
|
72
77
|
end
|
73
78
|
end
|
74
79
|
end
|
80
|
+
|
81
|
+
def _new_instance hash = {}
|
82
|
+
Registry.data_class_for(self).new hash
|
83
|
+
end
|
75
84
|
end
|
76
85
|
end
|
77
86
|
end
|
data/lib/edr/repository.rb
CHANGED
data/lib/edr/version.rb
CHANGED
@@ -6,7 +6,7 @@ describe Edr::AR::DataValidator do
|
|
6
6
|
describe "Using data validation for a saved model" do
|
7
7
|
example do
|
8
8
|
order_data = OrderData.create! amount: 10, deliver_at: Date.today
|
9
|
-
order = Order.
|
9
|
+
order = Order.build(order_data)
|
10
10
|
order.amount = "blah"
|
11
11
|
Edr::AR::DataValidator.validate(order).should be_present
|
12
12
|
end
|
@@ -14,8 +14,8 @@ describe Edr::AR::DataValidator do
|
|
14
14
|
|
15
15
|
describe "Using data validation for a new model" do
|
16
16
|
example do
|
17
|
-
order = Order.
|
17
|
+
order = Order.build(amount: "blah")
|
18
18
|
Edr::AR::DataValidator.validate(order).should be_present
|
19
19
|
end
|
20
20
|
end
|
21
|
-
end
|
21
|
+
end
|
data/spec/edr/model_spec.rb
CHANGED
@@ -23,24 +23,17 @@ describe Edr::Model do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "uses hash to initialize fields" do
|
26
|
-
order = Order.
|
26
|
+
order = Order.build amount: 15
|
27
27
|
|
28
28
|
order.amount.should == 15
|
29
29
|
end
|
30
|
-
|
31
|
-
it "creating an aggregate with children" do
|
32
|
-
order = Order.new
|
33
|
-
item = order.add_item name: 'item1', amount: 10
|
34
|
-
|
35
|
-
item.name.should == 'item1'
|
36
|
-
end
|
37
30
|
end
|
38
31
|
|
39
32
|
describe "Using data structure instead of a data object" do
|
40
33
|
example do
|
41
|
-
order = Order.
|
34
|
+
order = Order.build OpenStruct.new
|
42
35
|
order.amount = 99
|
43
36
|
order.amount.should == 99
|
44
37
|
end
|
45
38
|
end
|
46
|
-
end
|
39
|
+
end
|
data/spec/edr/repository_spec.rb
CHANGED
@@ -4,7 +4,7 @@ require_relative '../test_data'
|
|
4
4
|
describe Edr::Repository do
|
5
5
|
describe "Persisting objects" do
|
6
6
|
example do
|
7
|
-
order = Order.
|
7
|
+
order = Order.build amount: 10
|
8
8
|
|
9
9
|
OrderRepository.persist order
|
10
10
|
|
@@ -12,18 +12,8 @@ describe Edr::Repository do
|
|
12
12
|
order.amount.should == 10
|
13
13
|
end
|
14
14
|
|
15
|
-
it "persists an aggregate with children" do
|
16
|
-
order = Order.new amount: 10
|
17
|
-
order.add_item name: 'item1', amount: 5
|
18
|
-
|
19
|
-
OrderRepository.persist order
|
20
|
-
|
21
|
-
from_db = OrderRepository.find(order.id)
|
22
|
-
from_db.items.first.amount.should == 5
|
23
|
-
end
|
24
|
-
|
25
15
|
it "raises an exception when invalid data" do
|
26
|
-
order = Order.
|
16
|
+
order = Order.build amount: "invalid"
|
27
17
|
|
28
18
|
->{OrderRepository.persist order}.should raise_error
|
29
19
|
end
|
@@ -31,13 +21,13 @@ describe Edr::Repository do
|
|
31
21
|
|
32
22
|
describe "Creating objects through the repository variable" do
|
33
23
|
let(:order) do
|
34
|
-
order = Order.
|
24
|
+
order = Order.build(amount: 10)
|
35
25
|
OrderRepository.persist order
|
36
26
|
order
|
37
27
|
end
|
38
28
|
|
39
29
|
example do
|
40
|
-
order.
|
30
|
+
order.add_item name: 'item', amount: 10
|
41
31
|
order.items.first.name.should == 'item'
|
42
32
|
end
|
43
33
|
|
@@ -71,7 +61,7 @@ describe Edr::Repository do
|
|
71
61
|
|
72
62
|
describe "Deleting models" do
|
73
63
|
let!(:order) do
|
74
|
-
order = Order.
|
64
|
+
order = Order.build amount: 10, deliver_at: Date.today
|
75
65
|
OrderRepository.persist order
|
76
66
|
order
|
77
67
|
end
|
@@ -90,4 +80,4 @@ describe Edr::Repository do
|
|
90
80
|
->{OrderRepository.delete_by_id 999}.should raise_error
|
91
81
|
end
|
92
82
|
end
|
93
|
-
end
|
83
|
+
end
|
data/spec/test_data.rb
CHANGED
@@ -52,10 +52,6 @@ class Order
|
|
52
52
|
wrap_associations :items
|
53
53
|
|
54
54
|
def add_item attrs
|
55
|
-
wrap association(:items).new(attrs)
|
56
|
-
end
|
57
|
-
|
58
|
-
def add_item_through_repository attrs
|
59
55
|
repository.create_item self, attrs
|
60
56
|
end
|
61
57
|
end
|
@@ -103,7 +99,7 @@ module OrderRepository
|
|
103
99
|
set_model_class Item
|
104
100
|
|
105
101
|
def self.create_item order, attrs
|
106
|
-
item = Item.
|
102
|
+
item = Item.build(attrs)
|
107
103
|
item.order_id = order.id
|
108
104
|
persist item
|
109
105
|
end
|
@@ -113,4 +109,4 @@ end
|
|
113
109
|
|
114
110
|
|
115
111
|
# STEP5: Profit
|
116
|
-
# --------------------------------------------------
|
112
|
+
# --------------------------------------------------
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: edr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2012-11-
|
15
|
+
date: 2012-11-21 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rspec
|
@@ -113,7 +113,7 @@ dependencies:
|
|
113
113
|
description: Entity Data-object Repository framework
|
114
114
|
email:
|
115
115
|
- vsavkin@nulogy.com
|
116
|
-
-
|
116
|
+
- matt@mattbriggs.net
|
117
117
|
- clemensp@nulogy.com
|
118
118
|
- justinf@nulogy.com
|
119
119
|
executables: []
|