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 CHANGED
@@ -18,3 +18,4 @@ Gemfile.lock
18
18
  .yardoc
19
19
  _yardoc
20
20
  doc/
21
+ TAGS
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", "mbriggs@nulocy.com", "clemensp@nulogy.com", "justinf@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
- attr_accessor :_data
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.new association
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
@@ -23,7 +23,8 @@ module Edr
23
23
  protected
24
24
 
25
25
  def wrap data
26
- model_class.new(data).tap do |m|
26
+ model_class.new.tap do |m|
27
+ m.send(:_data=, data)
27
28
  m.send(:repository=, self)
28
29
  end
29
30
  end
data/lib/edr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Edr
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -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.new(order_data)
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.new amount: "blah"
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
@@ -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.new amount: 15
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.new OpenStruct.new
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
@@ -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.new amount: 10
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.new amount: "invalid"
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.new(amount: 10)
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.add_item_through_repository name: 'item', amount: 10
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.new amount: 10, deliver_at: Date.today
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.new(attrs)
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
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-20 00:00:00.000000000 Z
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
- - mbriggs@nulocy.com
116
+ - matt@mattbriggs.net
117
117
  - clemensp@nulogy.com
118
118
  - justinf@nulogy.com
119
119
  executables: []