model_view 0.5.0 → 0.6.0

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: d309f17a7cace28365d69fd6a09a9fbea7834459
4
- data.tar.gz: 12dc29109bc78c788c26aaf4983abf1ee5bc68d1
3
+ metadata.gz: 7f452a7c8518cd7317744d92c073b69ecda1a8ea
4
+ data.tar.gz: d2f23452702e9503b9d6528318d3c3633626e67d
5
5
  SHA512:
6
- metadata.gz: 737cf93c2fb26e0d8f57765cedbe584bdee8407e17f9dc3242094b195288025afd1b9f846f292faae49ca1259bc11352ee90aee4c75ccc1d9550f27472d81266
7
- data.tar.gz: 71d927e979f1975b9d9aa813b8136f8457fd4e713bb1732a20c94de88e84d37123c9d96cd3fae6b790fbf73bcb001eab00b938347e84ee5de45eebe40932f228
6
+ metadata.gz: fb18484060aa0268040989d9442fa181bea59161c6eb1bccce476cdb6b31a1e168b6a37a0302155615b4d1371fbbfc16facc26e4d96132c3478d193159382d49
7
+ data.tar.gz: e4f1038e682911d1a77a7576f262ff845ae0d343851ccd2162cd7dcc3e24324b62dc3fbb0399392401ce961ea731a537569e96ec442315edc4f318d4181690d5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ # 0.6.0
4
+ * One level of inheritance from the parent class
5
+
3
6
  # 0.5.0
4
7
  * Transfer field args to setters
5
8
 
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/hash'
2
+
1
3
  module ModelView
2
4
  class Updater
3
5
 
@@ -1,3 +1,3 @@
1
1
  module ModelView
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/lib/model_view.rb CHANGED
@@ -74,18 +74,18 @@ module ModelView
74
74
  end
75
75
 
76
76
  def add_after_update(scope, block)
77
- @scopes ||= {ROOT => new_opts}
77
+ create_scopes
78
78
  @scopes[scope] ||= new_opts
79
79
  @scopes[scope][:after_update] = block
80
80
  end
81
81
 
82
82
  def add_scope(scope_name)
83
- @scopes ||= {ROOT => new_opts}
84
- @scopes[scope_name] = new_opts
83
+ create_scopes
84
+ @scopes[scope_name] = new_opts unless @scopes.key?(scope_name)
85
85
  end
86
86
 
87
87
  def add_field(scope, field_name, args, block)
88
- @scopes ||= {ROOT => new_opts}
88
+ create_scopes
89
89
  @scopes[scope] ||= new_opts
90
90
  cleaned_args = args.select{ |k| k != :setter}
91
91
  if args[:setter]
@@ -95,9 +95,17 @@ module ModelView
95
95
  end
96
96
 
97
97
  def add_setter(scope, field_name, args, block)
98
- @scopes ||= {ROOT => new_opts}
98
+ create_scopes
99
99
  @scopes[scope] ||= new_opts
100
100
  @scopes[scope][:setters][field_name] = {args: args, block: block}
101
101
  end
102
102
 
103
+ def create_scopes
104
+ if self.superclass.respond_to?(:scopes) && @scopes.nil?
105
+ @scopes = self.superclass.scopes
106
+ elsif @scopes.nil?
107
+ @scopes = { ROOT => new_opts }
108
+ end
109
+ end
110
+
103
111
  end
data/model_view.gemspec CHANGED
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "rspec", "~> 3.0"
25
25
  spec.add_development_dependency "pry", "~> 0.10"
26
+ spec.add_development_dependency "rails", "~> 5.1.4"
26
27
  end
@@ -39,7 +39,7 @@ describe ModelView do
39
39
 
40
40
  context "with a block" do
41
41
  it "adds the field to the root scope" do
42
- dummy_class.field :a_field { 1 + 1 }
42
+ dummy_class.field(:a_field) { 1 + 1 }
43
43
 
44
44
  scope_fields = dummy_class.scopes[root_scope][:fields]
45
45
 
@@ -47,6 +47,30 @@ describe ModelView do
47
47
  expect(scope_fields[:a_field][:block].call).to eq(2)
48
48
  end
49
49
  end
50
+
51
+ context "when a parent class has been defined" do
52
+ it "shares parent class fields" do
53
+ dummy_class.field(:a_field) { 1 + 1 }
54
+
55
+ child_class = Class.new(dummy_class)
56
+ child_class.field(:b_field) { 1 + 2 }
57
+
58
+ scope_fields = child_class.scopes[root_scope][:fields]
59
+ expect(scope_fields[:a_field][:block].call).to eq(2)
60
+ expect(scope_fields[:b_field][:block].call).to eq(3)
61
+ end
62
+
63
+ it "overwrites parent class field with child field correctly" do
64
+ dummy_class.field(:a_field) { 1 + 1 }
65
+
66
+ child_class = Class.new(dummy_class)
67
+ child_class.field(:a_field) { 1 + 2 }
68
+
69
+ scope_fields = child_class.scopes[root_scope][:fields]
70
+ expect(scope_fields[:a_field][:args]).to eq({})
71
+ expect(scope_fields[:a_field][:block].call).to eq(3)
72
+ end
73
+ end
50
74
  end
51
75
 
52
76
  describe :fields do
@@ -82,7 +106,7 @@ describe ModelView do
82
106
 
83
107
  context "with a block" do
84
108
  it "adds the seter to the root scope" do
85
- dummy_class.setter :a_field { 1 + 1 }
109
+ dummy_class.setter(:a_field) { 1 + 1 }
86
110
 
87
111
  scope_setters = dummy_class.scopes[root_scope][:setters]
88
112
 
@@ -90,6 +114,32 @@ describe ModelView do
90
114
  expect(scope_setters[:a_field][:block].call).to eq(2)
91
115
  end
92
116
  end
117
+
118
+ context "when a parent class has been defined" do
119
+ it "shares parent class setter" do
120
+ dummy_class.field :a_field, {setter: true, arg1: "foo1"}
121
+
122
+ child_class = Class.new(dummy_class)
123
+ child_class.field :b_field, {setter: true, arg1: "foo2"}
124
+
125
+ scope_setters = dummy_class.scopes[root_scope][:setters]
126
+ expect(scope_setters[:a_field]).to eq({args: {arg1: "foo1"}, block: nil})
127
+ expect(scope_setters[:b_field]).to eq({args: {arg1: "foo2"}, block: nil})
128
+ end
129
+
130
+ it "overwrites parent class setter correctly" do
131
+ dummy_class.field :a_field, {setter: true, arg1: "foo"}
132
+
133
+ child_class = Class.new(dummy_class)
134
+ child_class.field :a_field, {setter: true, arg1: "foo2"}
135
+
136
+ scope_fields = child_class.scopes[root_scope][:fields]
137
+ expect(scope_fields[:a_field]).to eq({args: {arg1: "foo2"}, block: nil})
138
+
139
+ scope_setters = dummy_class.scopes[root_scope][:setters]
140
+ expect(scope_setters[:a_field]).to eq({args: {arg1: "foo2"}, block: nil})
141
+ end
142
+ end
93
143
  end
94
144
 
95
145
  describe :after_update do
@@ -99,13 +149,35 @@ describe ModelView do
99
149
  after_update = dummy_class.scopes[root_scope][:after_update]
100
150
  expect(after_update).to be_a(Proc)
101
151
  end
152
+
153
+ context "when a parent class has been defined" do
154
+ it "child shares the parent after_update block" do
155
+ dummy_class.after_update { |obj| obj.save }
156
+
157
+ child_class = Class.new(dummy_class)
158
+ child_class.field(:a_field) { 1 + 2 }
159
+
160
+ after_update = child_class.scopes[root_scope][:after_update]
161
+ expect(after_update).to be_a(Proc)
162
+ end
163
+
164
+ it "overwrites the parent class after_update block correctly" do
165
+ dummy_class.after_update { |obj| obj.save }
166
+
167
+ child_class = Class.new(dummy_class)
168
+ child_class.after_update { 1 + 1 }
169
+
170
+ after_update = child_class.scopes[root_scope][:after_update]
171
+ expect(after_update.call('test')).to eq(2)
172
+ end
173
+ end
102
174
  end
103
175
  end
104
176
 
105
177
  context "within a scope" do
106
178
  describe :scope do
107
179
  it "creates a scope" do
108
- dummy_class.scope :a_scope { }
180
+ dummy_class.scope(:a_scope) { }
109
181
 
110
182
  expect(dummy_class.scopes.keys).to include(:a_scope)
111
183
  end
@@ -113,14 +185,14 @@ describe ModelView do
113
185
 
114
186
  describe :field do
115
187
  it "adds a field inside the scope" do
116
- dummy_class.scope :a_scope { field :foo }
188
+ dummy_class.scope(:a_scope) { field :foo }
117
189
 
118
190
  scope_fields = dummy_class.scopes[:a_scope][:fields]
119
191
  expect(scope_fields.keys).to include(:foo)
120
192
  end
121
193
 
122
194
  it "changes the current scope back to root" do
123
- dummy_class.scope :a_scope { field :foo }
195
+ dummy_class.scope(:a_scope) { field :foo }
124
196
  dummy_class.field :bar
125
197
 
126
198
  scope_fields = dummy_class.scopes[:a_scope][:fields]
@@ -133,31 +205,43 @@ describe ModelView do
133
205
 
134
206
  describe :fields do
135
207
  it "adds all the fields to the current scope" do
136
- dummy_class.scope :foo_scope { fields :field_1, :field_2, :field_3 }
208
+ dummy_class.scope(:foo_scope) { fields :field_1, :field_2, :field_3 }
137
209
 
138
210
  scope_fields = dummy_class.scopes[:foo_scope][:fields]
139
211
  expect(scope_fields.keys).to eq([:field_1, :field_2, :field_3])
140
212
  end
213
+
214
+ context "when a parent class has been defined" do
215
+ it "merges parent class fields and child fields to the current scope" do
216
+ dummy_class.scope(:foo_scope) { fields :field_1, :field_2, :field_3 }
217
+
218
+ child_class = Class.new(dummy_class)
219
+ child_class.scope(:foo_scope) { fields :field_4 }
220
+
221
+ scope_fields = child_class.scopes[:foo_scope][:fields]
222
+ expect(scope_fields.keys).to eq([:field_1, :field_2, :field_3, :field_4])
223
+ end
224
+ end
141
225
  end
142
226
 
143
227
  describe :include_scope do
144
228
  context "given a single scope" do
145
229
  it "adds the scope to the current scope's includes" do
146
- dummy_class.scope :my_scope { include_scope :foo }
230
+ dummy_class.scope(:my_scope) { include_scope :foo }
147
231
  expect(dummy_class.scopes[:my_scope][:includes]).to eq([:foo])
148
232
  end
149
233
  end
150
234
 
151
235
  context "given an array" do
152
236
  it "adds all the scopes to the current scope's includes" do
153
- dummy_class.scope :my_scope { include_scope [:foo, :bar] }
237
+ dummy_class.scope(:my_scope) { include_scope [:foo, :bar] }
154
238
  expect(dummy_class.scopes[:my_scope][:includes]).to eq([:foo, :bar])
155
239
  end
156
240
  end
157
241
 
158
242
  context "given more than one scope" do
159
243
  it "adds all the scopes to the current scope's includes" do
160
- dummy_class.scope :my_scope { include_scope :foo, :bar }
244
+ dummy_class.scope(:my_scope) { include_scope :foo, :bar }
161
245
  expect(dummy_class.scopes[:my_scope][:includes]).to eq([:foo, :bar])
162
246
  end
163
247
  end
@@ -166,21 +250,21 @@ describe ModelView do
166
250
  describe :extend_scope do
167
251
  context "given a single scope" do
168
252
  it "adds the scope to the current scope's extends" do
169
- dummy_class.scope :my_scope { extend_scope :foo }
253
+ dummy_class.scope(:my_scope) { extend_scope :foo }
170
254
  expect(dummy_class.scopes[:my_scope][:extends]).to eq([:foo])
171
255
  end
172
256
  end
173
257
 
174
258
  context "given an array" do
175
259
  it "adds all the scopes to the current scope's extends" do
176
- dummy_class.scope :my_scope { extend_scope [:foo, :bar] }
260
+ dummy_class.scope(:my_scope) { extend_scope [:foo, :bar] }
177
261
  expect(dummy_class.scopes[:my_scope][:extends]).to eq([:foo, :bar])
178
262
  end
179
263
  end
180
264
 
181
265
  context "given more than one scope" do
182
266
  it "adds all the scopes to the current scope's extends" do
183
- dummy_class.scope :my_scope { extend_scope :foo, :bar }
267
+ dummy_class.scope(:my_scope) { extend_scope :foo, :bar }
184
268
  expect(dummy_class.scopes[:my_scope][:extends]).to eq([:foo, :bar])
185
269
  end
186
270
  end
@@ -189,7 +273,7 @@ describe ModelView do
189
273
  describe :setter do
190
274
  context "without a block" do
191
275
  it "adds the setter to the root scope" do
192
- dummy_class.scope :my_scope { setter :a_field }
276
+ dummy_class.scope(:my_scope) { setter :a_field }
193
277
 
194
278
  scope_setters = dummy_class.scopes[:my_scope][:setters]
195
279
  expect(scope_setters[:a_field]).to eq({args: {}, block: nil})
@@ -198,7 +282,7 @@ describe ModelView do
198
282
 
199
283
  context "with a block" do
200
284
  it "adds the seter to the root scope" do
201
- dummy_class.scope :my_scope { setter :a_field { 1 + 1 } }
285
+ dummy_class.scope(:my_scope) { setter(:a_field) { 1 + 1 } }
202
286
 
203
287
  scope_setters = dummy_class.scopes[:my_scope][:setters]
204
288
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Pretorius
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 5.1.4
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 5.1.4
83
97
  description: Composable serialisation for models
84
98
  email:
85
99
  - martin@offerzen.com
@@ -123,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
137
  version: '0'
124
138
  requirements: []
125
139
  rubyforge_project:
126
- rubygems_version: 2.6.10
140
+ rubygems_version: 2.6.13
127
141
  signing_key:
128
142
  specification_version: 4
129
143
  summary: Composable serialisation for models