mediator 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,6 +15,11 @@ class Mediator
15
15
  (options && options[:value]) || mediator.get(selector)
16
16
  end
17
17
 
18
+ def has? name, options = nil
19
+ selector = (options && options[:from]) || name
20
+ (options && options.has_key?(:value)) || mediator.subject.respond_to?(selector) || mediator.subject.respond_to?(selector.to_s)
21
+ end
22
+
18
23
  def id name, options = {}
19
24
  key name, options.merge(from: "#{name}_id")
20
25
  end
@@ -37,6 +42,8 @@ class Mediator
37
42
  name = name[0..-2].intern
38
43
  end
39
44
 
45
+ return unless has? name, options
46
+
40
47
  value = get name, options
41
48
  return if empty? value, options
42
49
 
data/lib/mediator.rb CHANGED
@@ -141,7 +141,10 @@ class Mediator
141
141
  # implementation calls the `name` method if it exists.
142
142
 
143
143
  def get name
144
- value = subject.send name if subject.respond_to? name
144
+ value = subject.send name if subject.respond_to? name
145
+
146
+ return value unless value.nil?
147
+
145
148
  value ||= construct name
146
149
 
147
150
  getting name, value if value
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.1.0"
12
+ gem.version = "0.1.1"
13
13
 
14
14
  gem.required_ruby_version = ">= 1.9.2"
15
15
  end
@@ -179,6 +179,20 @@ describe Mediator::Renderer do
179
179
  assert d[:foo]
180
180
  end
181
181
 
182
+ it "Works with false boolean" do
183
+ c = Class.new Mediator do
184
+ def render! r
185
+ r.key :foo
186
+ end
187
+ end
188
+
189
+ s = OpenStruct.new foo: false
190
+ m = c.new s
191
+ d = m.render
192
+
193
+ assert_equal false, d[:foo]
194
+ end
195
+
182
196
  it "ignores nil or empty values" do
183
197
  c = Class.new Mediator do
184
198
  def render! r
@@ -207,7 +221,7 @@ describe Mediator::Renderer do
207
221
  assert_equal Hash.new, d
208
222
  end
209
223
 
210
- it "optionally allows empty values" do
224
+ it "optionally allows empty values when defined" do
211
225
  c = Class.new Mediator do
212
226
  def render! r
213
227
  r.key :foo, empty: true
@@ -215,6 +229,7 @@ describe Mediator::Renderer do
215
229
  end
216
230
 
217
231
  s = OpenStruct.new
232
+ s.foo = nil
218
233
  m = c.new s
219
234
  d = m.render
220
235
  e = { foo: nil }
@@ -222,6 +237,21 @@ describe Mediator::Renderer do
222
237
  assert_equal e, d
223
238
  end
224
239
 
240
+ it "ditches empty values when not defined in any case" do
241
+ c = Class.new Mediator do
242
+ def render! r
243
+ r.key :foo, empty: true
244
+ end
245
+ end
246
+
247
+ s = OpenStruct.new
248
+ m = c.new s
249
+ d = m.render
250
+ e = { }
251
+
252
+ assert_equal e, d
253
+ end
254
+
225
255
  it "optionally maps names" do
226
256
  c = Class.new Mediator do
227
257
  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.1.0
4
+ version: 0.1.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: 2012-05-17 00:00:00.000000000Z
12
+ date: 2012-06-12 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A go-between for models.
15
15
  email: