representable 2.0.0.rc1 → 2.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c06c246f8a877ca22b35b7e5f409be0f5b1d6922
4
- data.tar.gz: 57183b1d4597321d6ed84278407032b04ac3eb8a
3
+ metadata.gz: c2acf392fadc7e81f961fa72ccee559b56bbac22
4
+ data.tar.gz: 8c27f1a11cb756c4d906d751231a26574e06bc87
5
5
  SHA512:
6
- metadata.gz: ed0821431bd7bee01cf48dc470211a78cdaa80a6de27df4961bf9028f6824a5914bf16bf86ffd59ca4698f60c56362226421aa2ea011933de82aa3072c28d081
7
- data.tar.gz: df87389a3a4b6e7841915dbf9b5ae999942f2640e70c27d199cc1ded97f1d2d1f9d6df3c400a3c76110176b5cdc55acdb5c73bb97da6b0a6986321e41a8dfb0a
6
+ metadata.gz: 482ed2034b6a2a09783be9b0aae1ee39ddb70f33cbdf5c60a459e90c6074c93ebf54f419535129f79804a7d524b690028ac308e8edcefc5eccb132032e6920a3
7
+ data.tar.gz: 1e3b78b78639a4c1c3955cb191f98a8b5e63002c76b274b6fa44b2d2179a76e26f685193f1d23e7dfc1bba6108e46510052e89e10667149eea8611ce36b67320
data/CHANGES.md CHANGED
@@ -5,10 +5,10 @@
5
5
  * Removed class methods `::from_json`, `::from_hash`, `::from_yaml` and `::from_xml`. Please build the instance yourself and use something along `Song.new.from_json`.
6
6
  * Inline representers in `Decorator` do *no longer inherit from `self`*. When defining an inline representer, they are always derived from `Representable::Decorator`. The base class can be changed by overriding `Decorator::default_inline_class` in the decorator class that defines an inline representer block.
7
7
  If you need to inherit common methods to all inline decorators, include the module using `::feature`: `Representer.feature(BetterProperty)`.
8
+ * You can now define methods in inline representers! The block is now `module_eval`ed and not `instance_exec`ed anymore. Same goes for Decorators, note that you need to `exec_context: :decorator`, though. Here, the block is `class_eval`ed.
8
9
  * Removed behaviour for `instance: lambda { |*| nil }` which used to return `binding.get`. Simply do it yourself: `instance: lambda { |fragment, options| options.binding.get }` if you need this behaviour. If you use `:instance` and it returns `nil` it throws a `DeserializeError` now, which is way more understandable than `NoMethodError: undefined method `title=' for {"title"=>"Perpetual"}:Hash`.
9
10
  * Remove behaviour for `class: lambda { nil }` which used to return the fragment. This now throws a `DeserializeError`. Do it yourself with class: lambda { |fragment,*| fragment }.
10
- * Coercion now happens inside `:render_filter` and `:parse_filter` (new!) and doesn't block `:getter` and `:setter` anymore.
11
- We require virtus >=1.0 now.
11
+ * Coercion now happens inside `:render_filter` and `:parse_filter` (new!) and doesn't block `:getter` and `:setter` anymore. Also, we require virtus >=1.0 now.
12
12
  * `::representation_wrap=` in now properly inherited.
13
13
  * Including modules with representable `property .., inherit: true` into a `Decorator` crashed. This works fine now.
14
14
 
data/README.md CHANGED
@@ -405,9 +405,9 @@ class AlbumRepresentation < Representable::Decorator
405
405
  end
406
406
  ```
407
407
 
408
- ### Helpers In Decorators
408
+ ### Methods In Modules
409
409
 
410
- In module representers you can add methods for properties.
410
+ You can define methods in representers in case they aren't defined on the represented object.
411
411
 
412
412
  ```ruby
413
413
  module SongRepresenter
@@ -418,7 +418,23 @@ module SongRepresenter
418
418
  end
419
419
  ```
420
420
 
421
- That works as the method is mixed into the represented object. When adding a helper method to a decorator, representable will still invoke accessors on the represented instance - unless you tell it the scope.
421
+ That works as the method is mixed into the represented object.
422
+
423
+ Note that this also goes for inline representers.
424
+
425
+ ```ruby
426
+ property :song do
427
+ property :title
428
+
429
+ def title
430
+ "Static titles are better"
431
+ end
432
+ end
433
+ ```
434
+
435
+ ### Methods In Decorators
436
+
437
+ When adding a method to a decorator, representable will still invoke accessors on the represented instance - unless you tell it the scope.
422
438
 
423
439
  ```ruby
424
440
  class SongRepresenter < Representable::Decorator
@@ -3,10 +3,14 @@ require "virtus"
3
3
  module Representable
4
4
  module Coercion
5
5
  class Coercer
6
+ def initialize(type)
7
+ @type = type
8
+ end
9
+
6
10
  # This gets called when the :render_filter or :parse_filter option is evaluated.
7
11
  # Usually the Coercer instance is an element in a Pipeline to allow >1 filters per property.
8
12
  def call(value, doc, options)
9
- Virtus::Attribute.build(options.binding[:type]).coerce(value)
13
+ Virtus::Attribute.build(@type).coerce(value)
10
14
  end
11
15
  end
12
16
 
@@ -23,9 +27,7 @@ module Representable
23
27
  def build_definition(name, options, &block) # Representable::Declarative
24
28
  return super unless type = options[:type]
25
29
 
26
- options[:pass_options] = true # TODO: remove, standard.
27
-
28
- options[:render_filter] << coercer = Coercer.new
30
+ options[:render_filter] << coercer = Coercer.new(type)
29
31
  options[:parse_filter] << coercer
30
32
 
31
33
  super
@@ -44,7 +44,7 @@ module Representable
44
44
  feature *features # Representable::JSON or similar.
45
45
  include base if base # base when :inherit, or in decorator.
46
46
 
47
- instance_exec &block
47
+ module_eval &block
48
48
  end
49
49
  end
50
50
 
@@ -51,7 +51,7 @@ module Representable
51
51
  Class.new(base || default_inline_class).tap do |decorator|
52
52
  decorator.class_eval do # Ruby 1.8.7 wouldn't properly execute the block passed to Class.new!
53
53
  feature *features
54
- instance_exec &block
54
+ class_eval &block
55
55
  end
56
56
  end
57
57
  end
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "2.0.0.rc1"
2
+ VERSION = "2.0.0.rc2"
3
3
  end
data/test/inline_test.rb CHANGED
@@ -226,4 +226,47 @@ class InlineTest < MiniTest::Spec
226
226
  must_equal({"song"=>{"artist"=>"Strung Out", "title"=>"The Fever And The Sound"}})
227
227
  end
228
228
  end
229
+
230
+
231
+ # define method in inline representer
232
+ describe "define method in inline representer" do
233
+ Mod = Module.new do
234
+ include Representable::Hash
235
+
236
+ def song
237
+ "Object.new"
238
+ end
239
+
240
+ property :song do
241
+ property :duration
242
+
243
+ def duration
244
+ "6:53"
245
+ end
246
+ end
247
+ end
248
+
249
+ it { Object.new.extend(Mod).to_hash.must_equal("song"=>{"duration"=>"6:53"}) }
250
+ end
251
+
252
+ # define method inline with Decorator
253
+ describe "define method inline with Decorator" do
254
+ dec = Class.new(Representable::Decorator) do
255
+ include Representable::Hash
256
+
257
+ def song
258
+ "Object.new"
259
+ end
260
+
261
+ property :song, :exec_context => :decorator do
262
+ property :duration, :exec_context => :decorator
263
+
264
+ def duration
265
+ "6:53"
266
+ end
267
+ end
268
+ end
269
+
270
+ it { dec.new(Object.new).to_hash.must_equal("song"=>{"duration"=>"6:53"}) }
271
+ end
229
272
  end
metadata CHANGED
@@ -1,153 +1,153 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: representable
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.rc1
4
+ version: 2.0.0.rc2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-28 00:00:00.000000000 Z
11
+ date: 2014-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: multi_json
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: uber
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: 0.0.7
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.0.7
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: test_xml
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: 0.1.6
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.1.6
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: minitest
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ~>
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
89
  version: 5.0.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ~>
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: 5.0.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: mocha
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: 0.13.0
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: 0.13.0
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: mongoid
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - '>='
115
+ - - ">="
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - '>='
122
+ - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: virtus
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - '>='
129
+ - - ">="
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - '>='
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: json
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
- - - ~>
143
+ - - "~>"
144
144
  - !ruby/object:Gem::Version
145
145
  version: 1.7.7
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
- - - ~>
150
+ - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: 1.7.7
153
153
  description: Renders and parses JSON/XML/YAML documents from and to Ruby objects.
@@ -158,8 +158,8 @@ executables: []
158
158
  extensions: []
159
159
  extra_rdoc_files: []
160
160
  files:
161
- - .gitignore
162
- - .travis.yml
161
+ - ".gitignore"
162
+ - ".travis.yml"
163
163
  - CHANGES.md
164
164
  - Gemfile
165
165
  - LICENSE
@@ -250,12 +250,12 @@ require_paths:
250
250
  - lib
251
251
  required_ruby_version: !ruby/object:Gem::Requirement
252
252
  requirements:
253
- - - '>='
253
+ - - ">="
254
254
  - !ruby/object:Gem::Version
255
255
  version: '0'
256
256
  required_rubygems_version: !ruby/object:Gem::Requirement
257
257
  requirements:
258
- - - '>'
258
+ - - ">"
259
259
  - !ruby/object:Gem::Version
260
260
  version: 1.3.1
261
261
  requirements: []
@@ -265,4 +265,46 @@ signing_key:
265
265
  specification_version: 4
266
266
  summary: Renders and parses JSON/XML/YAML documents from and to Ruby objects. Includes
267
267
  plain properties, collections, nesting, coercion and more.
268
- test_files: []
268
+ test_files:
269
+ - test/as_test.rb
270
+ - test/class_test.rb
271
+ - test/coercion_test.rb
272
+ - test/config/inherit_test.rb
273
+ - test/config_test.rb
274
+ - test/decorator_scope_test.rb
275
+ - test/decorator_test.rb
276
+ - test/definition_test.rb
277
+ - test/example.rb
278
+ - test/exec_context_test.rb
279
+ - test/features_test.rb
280
+ - test/filter_test.rb
281
+ - test/for_collection_test.rb
282
+ - test/generic_test.rb
283
+ - test/getter_setter_test.rb
284
+ - test/hash_bindings_test.rb
285
+ - test/hash_test.rb
286
+ - test/if_test.rb
287
+ - test/inherit_test.rb
288
+ - test/inheritable_test.rb
289
+ - test/inline_test.rb
290
+ - test/instance_test.rb
291
+ - test/is_representable_test.rb
292
+ - test/json_test.rb
293
+ - test/lonely_test.rb
294
+ - test/mongoid_test.rb
295
+ - test/nested_test.rb
296
+ - test/parse_strategy_test.rb
297
+ - test/pass_options_test.rb
298
+ - test/prepare_test.rb
299
+ - test/reader_writer_test.rb
300
+ - test/represent_test.rb
301
+ - test/representable_test.rb
302
+ - test/schema_test.rb
303
+ - test/serialize_deserialize_test.rb
304
+ - test/stringify_hash_test.rb
305
+ - test/test_helper.rb
306
+ - test/test_helper_test.rb
307
+ - test/wrap_test.rb
308
+ - test/xml_bindings_test.rb
309
+ - test/xml_test.rb
310
+ - test/yaml_test.rb