representable 1.1.6 → 1.1.7

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.7
2
+
3
+ * Added support for coercion using virtus. Note that this presently works on class level with inline representers, only.
4
+
1
5
  h2. 1.1.6
2
6
 
3
7
  * Added @:if@ option to @property@.
@@ -13,8 +13,9 @@ This keeps your representation knowledge in one place when implementing REST ser
13
13
  == Features
14
14
 
15
15
  * Bidirectional - rendering and parsing
16
- * OOP documents
17
- * Support for JSON, XML and MessagePack
16
+ * OOP access to documents
17
+ * Support for JSON and XML
18
+ * Coercion support with virtus[https://github.com/solnic/virtus.]
18
19
 
19
20
 
20
21
  == Example
@@ -52,7 +53,7 @@ Many people dislike including representers on class layer. You might also extend
52
53
 
53
54
  Hero.new.extend(HeroRepresenter)
54
55
 
55
- Alternatively, if you don't like modules (which you shouldn't), declarations can be put into classes directly.
56
+ Alternatively, if you don't like modules (which you shouldn't), declarations can be put into classes directly. We call that inline representers.
56
57
 
57
58
  class Hero
58
59
  attr_accessor :forename, :surename
@@ -307,6 +308,25 @@ You can also map properties to tag attributes in representable.
307
308
  Naturally, this works for both ways.
308
309
 
309
310
 
311
+ == Coercion
312
+
313
+ If you fancy coercion when parsing a document you can use the Coercion module which uses virtus[https://github.com/solnic/virtus] for type conversion. Due to some virtus' nature this can be used on class layer with inline representers, only for now.
314
+
315
+ Include virtus in your Gemfile, first.
316
+
317
+ gem 'virtus'
318
+
319
+ Use the +:type+ option to specify the conversion target. Note that +:default+ still works.
320
+
321
+ class Hero
322
+ include Representable::JSON
323
+ include Virtus
324
+ extend Representable::Coercion::ClassMethods
325
+
326
+ property :born_at, :type => DateTime, :default => "May 12th, 2012"
327
+ end
328
+
329
+
310
330
  == More
311
331
 
312
332
  Instead of spreading knowledge about your representations about the entire framework, Representable keeps rendering and parsing representations in one single, testable asset. It is a new abstraction layer missing in many "RESTful" frameworks.
@@ -199,17 +199,4 @@ private
199
199
  downcase
200
200
  end
201
201
  end
202
-
203
-
204
- # Allows mapping formats to representer classes.
205
- # DISCUSS: this module might be removed soon.
206
- module Represents
207
- def represents(format, options)
208
- representer[format] = options[:with]
209
- end
210
-
211
- def representer
212
- @represents_map ||= {}
213
- end
214
- end
215
202
  end
@@ -0,0 +1,18 @@
1
+ require "virtus"
2
+
3
+ module Representable::Coercion
4
+ def self.included(base)
5
+ base.class_eval do
6
+ include Virtus
7
+ extend ClassMethods
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def property(name, args={})
13
+ attribute(name, args[:type]) if args[:type] # FIXME (in virtus): undefined method `superclass' for VirtusCoercionTest::SongRepresenter:Module
14
+ super(name, args)
15
+ end
16
+ end
17
+
18
+ end
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "1.1.6"
2
+ VERSION = "1.1.7"
3
3
  end
@@ -27,4 +27,5 @@ Gem::Specification.new do |s|
27
27
  s.add_development_dependency "minitest", ">= 2.8.1"
28
28
  s.add_development_dependency "mocha"
29
29
  s.add_development_dependency "mongoid"
30
+ s.add_development_dependency "virtus"
30
31
  end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+ require 'representable/coercion'
3
+
4
+ class VirtusCoercionTest < MiniTest::Spec
5
+ class Song # note that we don't define accessors for the properties here.
6
+ end
7
+
8
+ describe "Coercion with Virtus" do
9
+ #before do
10
+ # module SongRepresenter
11
+ # include Representable::JSON
12
+ # include Representable::Coercion
13
+ # property :composed_at, :type => DateTime
14
+ # end
15
+ #end
16
+ #
17
+ # it "coerces properties in #from_json" do
18
+ # song = Song.new.extend(SongRepresenter).from_json("{\"composed_at\":\"November 18th, 1983\"}")
19
+ # assert_kind_of DateTime, song.composed_at
20
+ # assert_equal "expected", song.composed_at
21
+ # end
22
+ class ImmigrantSong
23
+ include Representable::JSON
24
+ include Virtus
25
+ extend Representable::Coercion::ClassMethods
26
+
27
+ property :composed_at, :type => DateTime, :default => "May 12th, 2012"
28
+ end
29
+
30
+ it "coerces into the provided type" do
31
+ song = ImmigrantSong.new.from_json("{\"composed_at\":\"November 18th, 1983\"}")
32
+ assert_equal DateTime.parse("Fri, 18 Nov 1983 00:00:00 +0000"), song.composed_at
33
+ end
34
+
35
+ it "respects the :default options" do
36
+ song = ImmigrantSong.new.from_json("{}")
37
+ assert_kind_of DateTime, song.composed_at
38
+ assert_equal DateTime.parse("Mon, 12 May 2012 00:00:00 +0000"), song.composed_at
39
+ end
40
+
41
+ end
42
+ end
@@ -340,19 +340,4 @@ class RepresentableTest < MiniTest::Spec
340
340
  end
341
341
  end
342
342
  end
343
-
344
- describe "Represents" do
345
- before do
346
- @class = Class.new do
347
- extend Representable::Represents
348
- represents :json, :with => :whatever
349
- represents "application/order-json", :with => :special
350
- end
351
- end
352
-
353
- it "allows mapping formats to representers" do
354
- assert_equal :whatever, @class.representer[:json]
355
- assert_equal :special, @class.representer["application/order-json"]
356
- end
357
- end
358
343
  end
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.6
4
+ version: 1.1.7
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-20 00:00:00.000000000 Z
12
+ date: 2012-05-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &80957240 !ruby/object:Gem::Requirement
16
+ requirement: &80366830 !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: *80957240
24
+ version_requirements: *80366830
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &80956630 !ruby/object:Gem::Requirement
27
+ requirement: &80366420 !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: *80956630
35
+ version_requirements: *80366420
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &80956310 !ruby/object:Gem::Requirement
38
+ requirement: &80365840 !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: *80956310
46
+ version_requirements: *80365840
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: test_xml
49
- requirement: &80956090 !ruby/object:Gem::Requirement
49
+ requirement: &80365210 !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: *80956090
57
+ version_requirements: *80365210
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: minitest
60
- requirement: &80955690 !ruby/object:Gem::Requirement
60
+ requirement: &80364570 !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: *80955690
68
+ version_requirements: *80364570
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: mocha
71
- requirement: &80955390 !ruby/object:Gem::Requirement
71
+ requirement: &80364280 !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: *80955390
79
+ version_requirements: *80364280
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: mongoid
82
- requirement: &80955040 !ruby/object:Gem::Requirement
82
+ requirement: &80363910 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,18 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *80955040
90
+ version_requirements: *80363910
91
+ - !ruby/object:Gem::Dependency
92
+ name: virtus
93
+ requirement: &80363490 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *80363490
91
102
  description: Maps representation documents from and to Ruby objects. Includes XML
92
103
  and JSON support, plain properties, collections and compositions.
93
104
  email:
@@ -108,6 +119,7 @@ files:
108
119
  - lib/representable/binding.rb
109
120
  - lib/representable/bindings/json_bindings.rb
110
121
  - lib/representable/bindings/xml_bindings.rb
122
+ - lib/representable/coercion.rb
111
123
  - lib/representable/definition.rb
112
124
  - lib/representable/json.rb
113
125
  - lib/representable/json/collection.rb
@@ -117,6 +129,7 @@ files:
117
129
  - lib/representable/xml/collection.rb
118
130
  - lib/representable/xml/hash.rb
119
131
  - representable.gemspec
132
+ - test/coercion_test.rb
120
133
  - test/definition_test.rb
121
134
  - test/json_bindings_test.rb
122
135
  - test/json_test.rb