model_view 0.4.0 → 0.6.2

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
- SHA1:
3
- metadata.gz: d6f1228c9decb1c8c548d0641e35dc935ee4473b
4
- data.tar.gz: db0e1c74ad97f5ffb60de645f7407cf80f79cadc
2
+ SHA256:
3
+ metadata.gz: df48774fd9e789552ad5e1bc03ef4e60a9958c81a9eb51220fbbb3746ecf7aa9
4
+ data.tar.gz: bd816c3e95d4c059633b07263065b1539a89a343e2968f2bbbe3e9632bf2affa
5
5
  SHA512:
6
- metadata.gz: 3be2f2b46b4a7a9302716a8f917688c7b5f2d1a01d2a09c4096f62074a9f4336b34850d037623465b2fc96a6045044a9782923b4a3e901d2c544596586105cda
7
- data.tar.gz: 0ba01965454e3aa695bb7068902bdb51ef4b4b4c576c6e6756f492f1cd4d7f109424fe3511e7ee6ae46a23715214f34f8b6eaf519fd115068d691c48e2bae703
6
+ metadata.gz: 071f5d440a4aa831e3a988bb172a9b511d004050050f57ad56f8e094d85b9d8c50fbd90be41c43be4c27d54ff11fcad2b2776343f6b80119d508bcd9d1a33a4c
7
+ data.tar.gz: 599284803a78b72b3a6c6b18d32a4f2316a4ad266eaa50ff74317e529f909a4b55bc438a3c3b90e9f8950f23e77e4cf44006b76d08191e0e04740bf845f65fa4
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ # 0.6.2
4
+ * Upgrade rake and bundler version
5
+
6
+ # 0.6.1
7
+ * Fix inheritance bug overwriting parent scope
8
+
9
+ # 0.6.0
10
+ * One level of inheritance from the parent class
11
+
12
+ # 0.5.0
13
+ * Transfer field args to setters
14
+
15
+
16
+ # 0.4.1
17
+ * Make setters more robust
18
+
19
+
3
20
  # 0.4.0
4
21
  * Added constant and alias_for helpers
5
22
 
@@ -1,5 +1,6 @@
1
1
  require 'model_view/resolver'
2
2
  require 'model_view/updater'
3
+ require 'active_support/core_ext/object'
3
4
 
4
5
  module ModelView
5
6
 
@@ -74,29 +75,38 @@ module ModelView
74
75
  end
75
76
 
76
77
  def add_after_update(scope, block)
77
- @scopes ||= {ROOT => new_opts}
78
+ create_scopes
78
79
  @scopes[scope] ||= new_opts
79
80
  @scopes[scope][:after_update] = block
80
81
  end
81
82
 
82
83
  def add_scope(scope_name)
83
- @scopes ||= {ROOT => new_opts}
84
- @scopes[scope_name] = new_opts
84
+ create_scopes
85
+ @scopes[scope_name] = new_opts unless @scopes.key?(scope_name)
85
86
  end
86
87
 
87
88
  def add_field(scope, field_name, args, block)
88
- @scopes ||= {ROOT => new_opts}
89
+ create_scopes
89
90
  @scopes[scope] ||= new_opts
91
+ cleaned_args = args.select{ |k| k != :setter}
90
92
  if args[:setter]
91
- add_setter(scope, field_name, {}, nil)
93
+ add_setter(scope, field_name, cleaned_args, nil)
92
94
  end
93
- @scopes[scope][:fields][field_name] = {args: args.select{ |k| k != :setter}, block: block}
95
+ @scopes[scope][:fields][field_name] = {args: cleaned_args, block: block}
94
96
  end
95
97
 
96
98
  def add_setter(scope, field_name, args, block)
97
- @scopes ||= {ROOT => new_opts}
99
+ create_scopes
98
100
  @scopes[scope] ||= new_opts
99
101
  @scopes[scope][:setters][field_name] = {args: args, block: block}
100
102
  end
101
103
 
104
+ def create_scopes
105
+ if self.superclass.respond_to?(:scopes) && @scopes.nil?
106
+ @scopes = self.superclass.scopes.deep_dup
107
+ elsif @scopes.nil?
108
+ @scopes = { ROOT => new_opts }
109
+ end
110
+ end
111
+
102
112
  end
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/hash'
2
+
1
3
  module ModelView
2
4
  class Updater
3
5
 
@@ -7,7 +9,7 @@ module ModelView
7
9
 
8
10
  setters = setters_for_scope(scope, scopes)
9
11
 
10
- data.each do |key, value|
12
+ data.symbolize_keys.each do |key, value|
11
13
  setter = if !setters[key].nil?
12
14
  setter_for_key(setters, key)
13
15
  else
@@ -61,6 +63,7 @@ module ModelView
61
63
  .merge(scope_setters)
62
64
  .merge(extended_setters)
63
65
  .merge(included_setters)
66
+ .with_indifferent_access
64
67
  end
65
68
 
66
69
  def extract_setters(scope, scope_data)
@@ -1,3 +1,3 @@
1
1
  module ModelView
2
- VERSION = "0.4.0"
3
- end
2
+ VERSION = "0.6.2"
3
+ end
@@ -19,8 +19,9 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_development_dependency "semvergen", "~> 1.9"
22
- spec.add_development_dependency "bundler", "~> 1.3"
23
- spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "bundler", "~> 2.1.4"
23
+ spec.add_development_dependency "rake", "~> 12.3.3"
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
@@ -26,20 +26,20 @@ describe ModelView do
26
26
 
27
27
  context "with setter set to true" do
28
28
  it "adds the field and setter to the root scope" do
29
- dummy_class.field :a_field, {setter: true}
29
+ dummy_class.field :a_field, {setter: true, arg1: "foo"}
30
30
 
31
31
  scope_fields = dummy_class.scopes[root_scope][:fields]
32
- expect(scope_fields[:a_field]).to eq({args: {}, block: nil})
32
+ expect(scope_fields[:a_field]).to eq({args: {arg1: "foo"}, block: nil})
33
33
 
34
34
  scope_setters = dummy_class.scopes[root_scope][:setters]
35
- expect(scope_setters[:a_field]).to eq({args: {}, block: nil})
35
+ expect(scope_setters[:a_field]).to eq({args: {arg1: "foo"}, block: nil})
36
36
  end
37
37
  end
38
38
  end
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 = child_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 = child_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,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Pretorius
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2017-06-13 00:00:00.000000000 Z
@@ -30,28 +30,28 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.3'
33
+ version: 2.1.4
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.3'
40
+ version: 2.1.4
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: 12.3.3
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: 12.3.3
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -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
@@ -107,7 +121,7 @@ homepage: https://github.com/Offerzen/model_view
107
121
  licenses:
108
122
  - MIT
109
123
  metadata: {}
110
- post_install_message:
124
+ post_install_message:
111
125
  rdoc_options: []
112
126
  require_paths:
113
127
  - lib
@@ -122,9 +136,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
136
  - !ruby/object:Gem::Version
123
137
  version: '0'
124
138
  requirements: []
125
- rubyforge_project:
126
- rubygems_version: 2.6.10
127
- signing_key:
139
+ rubygems_version: 3.0.6
140
+ signing_key:
128
141
  specification_version: 4
129
142
  summary: Composable serialisation for models
130
143
  test_files: