representable 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ h2. 1.1.6
2
+
3
+ * Added @:if@ option to @property@.
4
+
1
5
  h2. 1.1.5
2
6
 
3
7
  * Definitions are now properly cloned when @Config@ is cloned.
@@ -231,7 +231,7 @@ If your accessor name doesn't match the attribute name in the document, use the
231
231
  peter.to_json #=> {"i_am_called":"Peter","surename":"Pan"}
232
232
 
233
233
 
234
- === Filtering
234
+ === Filtering and Conditions
235
235
 
236
236
  Representable allows you to skip and include properties when rendering or parsing.
237
237
 
@@ -240,6 +240,13 @@ Representable allows you to skip and include properties when rendering or parsin
240
240
 
241
241
  It gives you convenient +:exclude+ and +:include+ options.
242
242
 
243
+ You can also define conditions on properties on the class layer.
244
+
245
+ module HeroRepresenter
246
+ property :friends, :if => lambda { forename == "Peter" }
247
+ end
248
+
249
+ When rendering or parsing, the +friends+ property is considered only if the condition block evals to true. Note that the block is executed in instance context, giving you access to instance methods.
243
250
 
244
251
  == DCI
245
252
 
@@ -67,11 +67,23 @@ private
67
67
 
68
68
  # Checks and returns if the property should be included.
69
69
  def skip_property?(binding, options)
70
+ return true if skip_excluded_property?(binding, options) # no need for further evaluation when :except'ed
71
+
72
+ skip_conditional_property?(binding)
73
+ end
74
+
75
+ def skip_excluded_property?(binding, options)
70
76
  return unless props = options[:except] || options[:include]
71
- res = props.include?(binding.definition.name.to_sym)
77
+ props = options[:except] || options[:include]
78
+ res = props.include?(binding.definition.name.to_sym)
72
79
  options[:include] ? !res : res
73
80
  end
74
81
 
82
+ def skip_conditional_property?(binding)
83
+ return unless condition = binding.definition.options[:if]
84
+ not instance_exec(&condition)
85
+ end
86
+
75
87
  # Retrieve value and write fragment to the doc.
76
88
  def compile_fragment(bin, doc)
77
89
  value = send(bin.definition.getter) || bin.definition.default # DISCUSS: eventually move back to Ref.
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "1.1.5"
2
+ VERSION = "1.1.6"
3
3
  end
@@ -154,15 +154,6 @@ class RepresentableTest < MiniTest::Spec
154
154
  assert_equal "friends", band.representable_attrs.last.from
155
155
  end
156
156
  end
157
-
158
- describe ":accessor" do
159
- it "doesn't add methods when false" do
160
- klass = Class.new(Band) { property :friends, :accessors => false }
161
- band = klass.new
162
- assert ! band.respond_to?(:friends)
163
- assert ! band.respond_to?("friends=")
164
- end
165
- end
166
157
  end
167
158
 
168
159
  describe "#collection" do
@@ -253,6 +244,48 @@ class RepresentableTest < MiniTest::Spec
253
244
  it "always returns self" do
254
245
  assert_equal @band, @band.update_properties_from({"name"=>"Nofx"}, {}, Representable::JSON)
255
246
  end
247
+
248
+ describe ":if" do
249
+ before do
250
+ @pop = Class.new(PopBand) { attr_accessor :fame }
251
+ end
252
+
253
+ it "respects property when condition true" do
254
+ @pop.class_eval { property :fame, :if => lambda { true } }
255
+ band = @pop.new
256
+ band.update_properties_from({"fame"=>"oh yes"}, {}, Representable::JSON)
257
+ assert_equal "oh yes", band.fame
258
+ end
259
+
260
+ it "ignores property when condition false" do
261
+ @pop.class_eval { property :fame, :if => lambda { false } }
262
+ band = @pop.new
263
+ band.update_properties_from({"fame"=>"oh yes"}, {}, Representable::JSON)
264
+ assert_equal nil, band.fame
265
+ end
266
+
267
+ it "ignores property when :except'ed even when condition is true" do
268
+ @pop.class_eval { property :fame, :if => lambda { true } }
269
+ band = @pop.new
270
+ band.update_properties_from({"fame"=>"oh yes"}, {:except => [:fame]}, Representable::JSON)
271
+ assert_equal nil, band.fame
272
+ end
273
+
274
+
275
+ it "executes block in instance context" do
276
+ @pop.class_eval { property :fame, :if => lambda { groupies } }
277
+ band = @pop.new
278
+ band.groupies = true
279
+ band.update_properties_from({"fame"=>"oh yes"}, {}, Representable::JSON)
280
+ assert_equal "oh yes", band.fame
281
+ end
282
+
283
+ end
284
+
285
+ it "what" do
286
+
287
+ end
288
+
256
289
  end
257
290
 
258
291
  describe "#create_representation_with" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: representable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-03 00:00:00.000000000 Z
12
+ date: 2012-04-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &72230170 !ruby/object:Gem::Requirement
16
+ requirement: &80957240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *72230170
24
+ version_requirements: *80957240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &72229010 !ruby/object:Gem::Requirement
27
+ requirement: &80956630 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *72229010
35
+ version_requirements: *80956630
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &72228350 !ruby/object:Gem::Requirement
38
+ requirement: &80956310 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *72228350
46
+ version_requirements: *80956310
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: test_xml
49
- requirement: &72227790 !ruby/object:Gem::Requirement
49
+ requirement: &80956090 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *72227790
57
+ version_requirements: *80956090
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: minitest
60
- requirement: &72227360 !ruby/object:Gem::Requirement
60
+ requirement: &80955690 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 2.8.1
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *72227360
68
+ version_requirements: *80955690
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: mocha
71
- requirement: &72226980 !ruby/object:Gem::Requirement
71
+ requirement: &80955390 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *72226980
79
+ version_requirements: *80955390
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: mongoid
82
- requirement: &72226580 !ruby/object:Gem::Requirement
82
+ requirement: &80955040 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *72226580
90
+ version_requirements: *80955040
91
91
  description: Maps representation documents from and to Ruby objects. Includes XML
92
92
  and JSON support, plain properties, collections and compositions.
93
93
  email: