mediator 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,14 +10,14 @@ class Mediator
10
10
  @mediator = mediator
11
11
  end
12
12
 
13
- def get name, options = nil
14
- selector = (options && options[:from]) || name
15
- (options && options[:value]) || data[selector] || data[selector.to_s]
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 = nil
19
- selector = (options && options[:from]) || name
20
- (options && options.has_key?(:value)) || mediator.data_has?(data,selector)
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 = nil, &block
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 = nil, &block
55
- data = get name, options
56
- subj = (options && options[:subject]) || mediator.get(name)
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.map { |d| sub subj[data.index d], d, options, &block }.
59
- reject { |v| empty? v, options }
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 = nil, &block
69
+ def one name, options = {}, &block
70
+ options = {construct: true}.merge options
71
+
63
72
  data = get name, options
64
- subj = (options && options[:subject]) || mediator.get(name)
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 = nil, &block
70
- (options ||= {}).merge! value: self.data
78
+ def union name, options = {}, &block
79
+ options = {value: self.data}.merge options
71
80
  one name, options, &block
72
81
  end
73
82
 
@@ -10,14 +10,15 @@ class Mediator
10
10
  @mediator = mediator
11
11
  end
12
12
 
13
- def get name, options = nil
14
- selector = (options && options[:from]) || name
15
- (options && options[:value]) || mediator.get(selector)
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 = nil
19
- selector = (options && options[:from]) || name
20
- (options && options.has_key?(:value)) || mediator.subject_has?(selector)
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 = nil, &block
40
+ def key name, options = {}, &block
40
41
  if name[-1] == "?"
41
- (options ||= {})[:from] = name
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 = nil, &block
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 = nil, &block
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 && options[:merge] ? data.merge!(value) : data[name] = value
67
+ options[:merge] ? data.merge!(value) : data[name] = value
67
68
  end
68
69
 
69
- def union name, options = nil, &block
70
- (options ||= {}).merge! merge: true
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
@@ -9,7 +9,7 @@ Gem::Specification.new do |gem|
9
9
  gem.test_files = `git ls-files -- test/*`.split "\n"
10
10
  gem.name = "mediator"
11
11
  gem.require_paths = ["lib"]
12
- gem.version = "0.3.1"
12
+ gem.version = "0.4.0"
13
13
 
14
14
  gem.required_ruby_version = ">= 1.9.2"
15
15
  end
@@ -198,19 +198,32 @@ describe Mediator::Parser do
198
198
 
199
199
  describe "many" do
200
200
  before do
201
- Many = Class.new OpenStruct
202
- Nested = Class.new OpenStruct
201
+ Bar ||= Class.new OpenStruct
202
+ Foo ||= Class.new OpenStruct
203
203
 
204
204
  Class.new Mediator do
205
- accept Many
205
+ accept Bar
206
206
 
207
207
  def parse! p
208
- p.many :nest
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 Nested
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 = Many.new
223
- s.nest = [ Nested.new, Nested.new ]
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 = { nest: [ { baz: "baz!" }, { baz: "blup?" } ] }
262
+ d = { merge_foos: [ { baz: "blup?" } ] }
227
263
 
228
264
  m.parse d
229
265
 
230
- assert_equal d[:nest].map { |v| v[:baz] }, s.nest.map { |v| v.baz }
266
+ assert_equal ["bar", "blup?"], s.merge_foos.map(&:baz)
231
267
  end
232
268
  end
233
269
 
@@ -319,7 +319,7 @@ describe Mediator::Renderer do
319
319
  end
320
320
 
321
321
  def render! r
322
- r.key :foo
322
+ r.key :foo, construct: true
323
323
  end
324
324
  end
325
325
 
@@ -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.3.1
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-01-23 00:00:00.000000000 Z
12
+ date: 2013-03-05 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A go-between for models.
15
15
  email: