mediator 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mediator/parser.rb +25 -16
- data/lib/mediator/renderer.rb +14 -13
- data/lib/mediator.rb +2 -2
- data/mediator.gemspec +1 -1
- data/test/mediator_parser_test.rb +45 -9
- data/test/mediator_renderer_test.rb +1 -1
- data/test/mediator_test.rb +2 -2
- metadata +2 -2
data/lib/mediator/parser.rb
CHANGED
@@ -10,14 +10,14 @@ class Mediator
|
|
10
10
|
@mediator = mediator
|
11
11
|
end
|
12
12
|
|
13
|
-
def get name, options =
|
14
|
-
selector =
|
15
|
-
|
13
|
+
def get name, options = {}
|
14
|
+
selector = options[:from] || name
|
15
|
+
options[:value] || data[selector] || data[selector.to_s]
|
16
16
|
end
|
17
17
|
|
18
|
-
def has? name, options =
|
19
|
-
selector =
|
20
|
-
|
18
|
+
def has? name, options = {}
|
19
|
+
selector = options[:from] || name
|
20
|
+
options.has_key?(:value) || mediator.data_has?(data,selector)
|
21
21
|
end
|
22
22
|
|
23
23
|
def id name, options = {}
|
@@ -36,7 +36,7 @@ class Mediator
|
|
36
36
|
key id_name, options
|
37
37
|
end
|
38
38
|
|
39
|
-
def key name, options =
|
39
|
+
def key name, options = {}, &block
|
40
40
|
if name[-1] == "?"
|
41
41
|
name = name[0..-2].intern
|
42
42
|
end
|
@@ -51,23 +51,32 @@ class Mediator
|
|
51
51
|
mediator.set name, value unless empty? value, options
|
52
52
|
end
|
53
53
|
|
54
|
-
def many name, options =
|
55
|
-
|
56
|
-
|
54
|
+
def many name, options = {}, &block
|
55
|
+
options = {construct: true}.merge options
|
56
|
+
|
57
|
+
mediator.set name, [] unless options[:merge]
|
58
|
+
|
59
|
+
data = get(name, options) || []
|
60
|
+
subj = (options && options[:subject]) || mediator.get(name, options)
|
57
61
|
|
58
|
-
data.
|
59
|
-
|
62
|
+
data.each do |d|
|
63
|
+
name = name[0..-2] if name[-1] == "s"
|
64
|
+
s = mediator.construct name
|
65
|
+
sub s, d, options, &block
|
66
|
+
end
|
60
67
|
end
|
61
68
|
|
62
|
-
def one name, options =
|
69
|
+
def one name, options = {}, &block
|
70
|
+
options = {construct: true}.merge options
|
71
|
+
|
63
72
|
data = get name, options
|
64
|
-
subj =
|
73
|
+
subj = options[:subject] || mediator.get(name, options)
|
65
74
|
|
66
75
|
sub subj, data, options, &block
|
67
76
|
end
|
68
77
|
|
69
|
-
def union name, options =
|
70
|
-
|
78
|
+
def union name, options = {}, &block
|
79
|
+
options = {value: self.data}.merge options
|
71
80
|
one name, options, &block
|
72
81
|
end
|
73
82
|
|
data/lib/mediator/renderer.rb
CHANGED
@@ -10,14 +10,15 @@ class Mediator
|
|
10
10
|
@mediator = mediator
|
11
11
|
end
|
12
12
|
|
13
|
-
def get name, options =
|
14
|
-
|
15
|
-
|
13
|
+
def get name, options = {}
|
14
|
+
options = {construct: false}.merge options
|
15
|
+
selector = options[:from] || name
|
16
|
+
options[:value] || mediator.get(selector, options)
|
16
17
|
end
|
17
18
|
|
18
|
-
def has? name, options =
|
19
|
-
selector =
|
20
|
-
|
19
|
+
def has? name, options = {}
|
20
|
+
selector = options[:from] || name
|
21
|
+
options.has_key?(:value) || mediator.subject_has?(selector)
|
21
22
|
end
|
22
23
|
|
23
24
|
def id name, options = {}, &block
|
@@ -36,9 +37,9 @@ class Mediator
|
|
36
37
|
key name, options, &block
|
37
38
|
end
|
38
39
|
|
39
|
-
def key name, options =
|
40
|
+
def key name, options = {}, &block
|
40
41
|
if name[-1] == "?"
|
41
|
-
|
42
|
+
options[:from] = name
|
42
43
|
name = name[0..-2].intern
|
43
44
|
end
|
44
45
|
|
@@ -50,24 +51,24 @@ class Mediator
|
|
50
51
|
data[name] = value unless empty? value, options
|
51
52
|
end
|
52
53
|
|
53
|
-
def many name, options =
|
54
|
+
def many name, options = {}, &block
|
54
55
|
value = get name, options
|
55
56
|
data[name] = value.map { |v| sub v, options, &block }.
|
56
57
|
reject { |v| empty? v, options }
|
57
58
|
end
|
58
59
|
|
59
|
-
def one name, options =
|
60
|
+
def one name, options = {}, &block
|
60
61
|
value = get name, options
|
61
62
|
return if empty? value, options
|
62
63
|
|
63
64
|
value = sub value, options, &block
|
64
65
|
return if empty? value, options
|
65
66
|
|
66
|
-
options
|
67
|
+
options[:merge] ? data.merge!(value) : data[name] = value
|
67
68
|
end
|
68
69
|
|
69
|
-
def union name, options =
|
70
|
-
|
70
|
+
def union name, options = {}, &block
|
71
|
+
options.merge! merge: true
|
71
72
|
one name, options, &block
|
72
73
|
end
|
73
74
|
|
data/lib/mediator.rb
CHANGED
@@ -152,9 +152,9 @@ class Mediator
|
|
152
152
|
# Gets the `name` property from `subject`. The default
|
153
153
|
# implementation calls the `name` method if it exists.
|
154
154
|
|
155
|
-
def get name
|
155
|
+
def get name, options = {}
|
156
156
|
value = subject.send name if subject.respond_to? name
|
157
|
-
value = construct name if value.nil?
|
157
|
+
value = construct name if value.nil? and options[:construct]
|
158
158
|
|
159
159
|
getting name, value unless value.nil?
|
160
160
|
end
|
data/mediator.gemspec
CHANGED
@@ -198,19 +198,32 @@ describe Mediator::Parser do
|
|
198
198
|
|
199
199
|
describe "many" do
|
200
200
|
before do
|
201
|
-
|
202
|
-
|
201
|
+
Bar ||= Class.new OpenStruct
|
202
|
+
Foo ||= Class.new OpenStruct
|
203
203
|
|
204
204
|
Class.new Mediator do
|
205
|
-
accept
|
205
|
+
accept Bar
|
206
206
|
|
207
207
|
def parse! p
|
208
|
-
p.many :
|
208
|
+
p.many :foos
|
209
|
+
p.many :merge_foos, merge: true
|
210
|
+
end
|
211
|
+
|
212
|
+
def construct name
|
213
|
+
# name is either foo, replace_foo, replace_foos or foos..
|
214
|
+
subject.foos ||= []
|
215
|
+
subject.merge_foos ||= []
|
216
|
+
|
217
|
+
return subject.send(name) if subject.respond_to?(name)
|
218
|
+
|
219
|
+
foo = Foo.new
|
220
|
+
subject.send("#{name}s") << foo
|
221
|
+
foo
|
209
222
|
end
|
210
223
|
end
|
211
224
|
|
212
225
|
Class.new Mediator do
|
213
|
-
accept
|
226
|
+
accept Foo
|
214
227
|
|
215
228
|
def parse! p
|
216
229
|
p.key :baz
|
@@ -219,15 +232,38 @@ describe Mediator::Parser do
|
|
219
232
|
end
|
220
233
|
|
221
234
|
it "delegates to an array of nested mediator" do
|
222
|
-
s
|
223
|
-
|
235
|
+
s = Bar.new
|
236
|
+
|
237
|
+
m = Mediator[s]
|
238
|
+
d = { foos: [ { baz: "baz!" }, { baz: "blup?" } ] }
|
239
|
+
|
240
|
+
m.parse d
|
241
|
+
|
242
|
+
assert_equal d[:foos].map { |v| v[:baz] }, s.foos.map { |v| v.baz }
|
243
|
+
end
|
244
|
+
|
245
|
+
it "replaces all models by default" do
|
246
|
+
s = Bar.new foos: [ Foo.new(baz: "bar") ]
|
247
|
+
|
248
|
+
assert_equal ["bar"], s.foos.map(&:baz)
|
249
|
+
|
250
|
+
m = Mediator[s]
|
251
|
+
d = { foos: [ { baz: "blup?" } ] }
|
252
|
+
|
253
|
+
m.parse d
|
254
|
+
|
255
|
+
assert_equal ["blup?"], s.foos.map(&:baz)
|
256
|
+
end
|
257
|
+
|
258
|
+
it "adds new models if told to" do
|
259
|
+
s = Bar.new merge_foos: [ Foo.new(baz: "bar") ]
|
224
260
|
|
225
261
|
m = Mediator[s]
|
226
|
-
d = {
|
262
|
+
d = { merge_foos: [ { baz: "blup?" } ] }
|
227
263
|
|
228
264
|
m.parse d
|
229
265
|
|
230
|
-
assert_equal
|
266
|
+
assert_equal ["bar", "blup?"], s.merge_foos.map(&:baz)
|
231
267
|
end
|
232
268
|
end
|
233
269
|
|
data/test/mediator_test.rb
CHANGED
@@ -136,7 +136,7 @@ describe Mediator do
|
|
136
136
|
assert_equal :bar, m.get(:foo)
|
137
137
|
end
|
138
138
|
|
139
|
-
it "can construct missing values" do
|
139
|
+
it "can construct missing values if told to" do
|
140
140
|
c = Class.new Mediator do
|
141
141
|
def construct name
|
142
142
|
"HELLO" if :foo == name
|
@@ -144,7 +144,7 @@ describe Mediator do
|
|
144
144
|
end
|
145
145
|
|
146
146
|
m = c.new OpenStruct.new
|
147
|
-
assert_equal "HELLO", m.get(:foo)
|
147
|
+
assert_equal "HELLO", m.get(:foo, construct: true)
|
148
148
|
end
|
149
149
|
end
|
150
150
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mediator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-05 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A go-between for models.
|
15
15
|
email:
|