mediator 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/lib/mediator/renderer.rb +4 -4
- data/mediator.gemspec +1 -1
- data/test/mediator_parser_test.rb +15 -0
- data/test/mediator_renderer_test.rb +29 -0
- metadata +3 -3
data/.gitignore
CHANGED
data/lib/mediator/renderer.rb
CHANGED
@@ -20,11 +20,11 @@ class Mediator
|
|
20
20
|
(options && options.has_key?(:value)) || mediator.subject_has?(selector)
|
21
21
|
end
|
22
22
|
|
23
|
-
def id name, options = {}
|
24
|
-
key name, options.merge(from: "#{name}_id")
|
23
|
+
def id name, options = {}, &block
|
24
|
+
key name, options.merge(from: "#{name}_id"), &block
|
25
25
|
end
|
26
26
|
|
27
|
-
def ids name, options = {}
|
27
|
+
def ids name, options = {}, &block
|
28
28
|
unless options[:from]
|
29
29
|
if name[-1] == "s"
|
30
30
|
options = options.merge(from: "#{name[0..-2]}_ids")
|
@@ -33,7 +33,7 @@ class Mediator
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
key name, options
|
36
|
+
key name, options, &block
|
37
37
|
end
|
38
38
|
|
39
39
|
def key name, options = nil, &block
|
data/mediator.gemspec
CHANGED
@@ -27,6 +27,21 @@ describe Mediator::Parser do
|
|
27
27
|
p.id :foo
|
28
28
|
assert_equal 42, @subject.foo_id
|
29
29
|
end
|
30
|
+
|
31
|
+
it "Does nothing with empty values by default" do
|
32
|
+
p = Mediator::Parser.new @mediator, foo: ""
|
33
|
+
@subject.foo_id = 42
|
34
|
+
|
35
|
+
p.id :foo
|
36
|
+
assert_equal 42, @subject.foo_id
|
37
|
+
end
|
38
|
+
|
39
|
+
it "Accept empty values if told to" do
|
40
|
+
p = Mediator::Parser.new @mediator, foo: ""
|
41
|
+
|
42
|
+
p.id :foo, empty: true
|
43
|
+
assert_equal "", @subject.foo_id
|
44
|
+
end
|
30
45
|
end
|
31
46
|
|
32
47
|
describe "ids" do
|
@@ -26,6 +26,20 @@ describe Mediator::Renderer do
|
|
26
26
|
|
27
27
|
assert_equal r, m.render
|
28
28
|
end
|
29
|
+
|
30
|
+
it "apply a block to id value, if given" do
|
31
|
+
c = Class.new Mediator do
|
32
|
+
def render! r
|
33
|
+
r.id(:foo) { |v| 27 }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
x = OpenStruct.new foo_id: 5
|
38
|
+
m = c.new x
|
39
|
+
r = { foo: 27 }
|
40
|
+
|
41
|
+
assert_equal r, m.render
|
42
|
+
end
|
29
43
|
end
|
30
44
|
|
31
45
|
describe "ids" do
|
@@ -207,6 +221,21 @@ describe Mediator::Renderer do
|
|
207
221
|
assert_equal Hash.new, d
|
208
222
|
end
|
209
223
|
|
224
|
+
it "accepts custom blocks for ids" do
|
225
|
+
c = Class.new Mediator do
|
226
|
+
def render! r
|
227
|
+
r.ids(:foos) { |p| [12,34,45] }
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
s = OpenStruct.new foo_ids: [56,78,90]
|
232
|
+
m = c.new s
|
233
|
+
d = m.render
|
234
|
+
e = {foos: [12,34,45]}
|
235
|
+
|
236
|
+
assert_equal e, d
|
237
|
+
end
|
238
|
+
|
210
239
|
it "ignores nil or empty values returned by blocks" do
|
211
240
|
c = Class.new Mediator do
|
212
241
|
def render! r
|
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.3.
|
4
|
+
version: 0.3.1
|
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:
|
12
|
+
date: 2013-01-23 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A go-between for models.
|
15
15
|
email:
|
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
53
|
version: '0'
|
54
54
|
requirements: []
|
55
55
|
rubyforge_project:
|
56
|
-
rubygems_version: 1.8.
|
56
|
+
rubygems_version: 1.8.24
|
57
57
|
signing_key:
|
58
58
|
specification_version: 3
|
59
59
|
summary: Translates models to and from primitive representations.
|