representable 1.2.2 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.textile +4 -0
- data/Gemfile +2 -0
- data/README.rdoc +5 -5
- data/lib/representable.rb +31 -24
- data/lib/representable/coercion.rb +0 -1
- data/lib/representable/version.rb +1 -1
- data/representable.gemspec +1 -1
- data/test/coercion_test.rb +16 -14
- data/test/representable_test.rb +1 -1
- metadata +6 -6
data/CHANGES.textile
CHANGED
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -320,18 +320,18 @@ Naturally, this works for both ways.
|
|
320
320
|
|
321
321
|
== Coercion
|
322
322
|
|
323
|
-
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.
|
323
|
+
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.
|
324
324
|
|
325
|
-
Include virtus in your Gemfile, first.
|
325
|
+
Include virtus in your Gemfile, first. Be sure to include virtus 0.5.0 or greater.
|
326
326
|
|
327
327
|
gem 'virtus'
|
328
|
-
|
328
|
+
|
329
329
|
Use the +:type+ option to specify the conversion target. Note that +:default+ still works.
|
330
330
|
|
331
|
-
|
331
|
+
module HeroRepresenter
|
332
332
|
include Representable::JSON
|
333
333
|
include Virtus
|
334
|
-
|
334
|
+
include Representable::Coercion
|
335
335
|
|
336
336
|
property :born_at, :type => DateTime, :default => "May 12th, 2012"
|
337
337
|
end
|
data/lib/representable.rb
CHANGED
@@ -16,7 +16,7 @@ require 'representable/definition'
|
|
16
16
|
#
|
17
17
|
# == On module level
|
18
18
|
#
|
19
|
-
# Modules give you much more flexibility since you can mix them into objects at runtime,
|
19
|
+
# Modules give you much more flexibility since you can mix them into objects at runtime, following the DCI
|
20
20
|
# pattern.
|
21
21
|
#
|
22
22
|
# module HeroRepresenter
|
@@ -30,19 +30,11 @@ module Representable
|
|
30
30
|
|
31
31
|
def self.included(base)
|
32
32
|
base.class_eval do
|
33
|
-
|
33
|
+
extend ClassInclusions, ModuleExtensions
|
34
34
|
extend ClassMethods
|
35
35
|
extend ClassMethods::Declarations
|
36
|
-
extend ClassMethods::Accessors
|
37
|
-
|
38
|
-
def self.included(base)
|
39
|
-
base.representable_attrs.push(*representable_attrs.clone) # "inherit".
|
40
|
-
end
|
41
36
|
|
42
|
-
|
43
|
-
def self.extended(object)
|
44
|
-
object.representable_attrs=(representable_attrs)
|
45
|
-
end
|
37
|
+
include Deprecations
|
46
38
|
end
|
47
39
|
end
|
48
40
|
|
@@ -128,7 +120,23 @@ private
|
|
128
120
|
end
|
129
121
|
|
130
122
|
|
131
|
-
module
|
123
|
+
module ClassInclusions
|
124
|
+
def included(base)
|
125
|
+
super
|
126
|
+
base.representable_attrs.push(*representable_attrs.clone) # "inherit".
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
module ModuleExtensions
|
131
|
+
# Copies the representable_attrs to the extended object.
|
132
|
+
def extended(object)
|
133
|
+
super
|
134
|
+
object.representable_attrs=(representable_attrs)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
module ClassMethods
|
132
140
|
# Create and yield object and options. Called in .from_json and friends.
|
133
141
|
def create_represented(document, *args)
|
134
142
|
new.tap do |represented|
|
@@ -136,9 +144,14 @@ private
|
|
136
144
|
end
|
137
145
|
end
|
138
146
|
|
147
|
+
|
139
148
|
module Declarations
|
140
|
-
def
|
141
|
-
|
149
|
+
def representable_attrs
|
150
|
+
@representable_attrs ||= Config.new
|
151
|
+
end
|
152
|
+
|
153
|
+
def representation_wrap=(name)
|
154
|
+
representable_attrs.wrap = name
|
142
155
|
end
|
143
156
|
|
144
157
|
# Declares a represented document node, which is usually a XML tag or a JSON key.
|
@@ -149,7 +162,7 @@ private
|
|
149
162
|
# property :name, :from => :title
|
150
163
|
# property :name, :class => Name
|
151
164
|
# property :name, :default => "Mike"
|
152
|
-
# property :name, :
|
165
|
+
# property :name, :render_nil => true
|
153
166
|
def property(name, options={})
|
154
167
|
representable_attrs << definition_class.new(name, options)
|
155
168
|
end
|
@@ -172,16 +185,10 @@ private
|
|
172
185
|
options[:hash] = true
|
173
186
|
property(name, options)
|
174
187
|
end
|
175
|
-
end
|
176
|
-
|
177
|
-
|
178
|
-
module Accessors
|
179
|
-
def representable_attrs
|
180
|
-
@representable_attrs ||= Config.new
|
181
|
-
end
|
182
188
|
|
183
|
-
|
184
|
-
|
189
|
+
private
|
190
|
+
def definition_class
|
191
|
+
Definition
|
185
192
|
end
|
186
193
|
end
|
187
194
|
end
|
data/representable.gemspec
CHANGED
@@ -27,5 +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
|
+
s.add_development_dependency "virtus", "~> 0.5.0"
|
31
31
|
end
|
data/test/coercion_test.rb
CHANGED
@@ -6,23 +6,25 @@ class VirtusCoercionTest < MiniTest::Spec
|
|
6
6
|
end
|
7
7
|
|
8
8
|
describe "Coercion with Virtus" do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
9
|
+
describe "on object level" do
|
10
|
+
module SongRepresenter
|
11
|
+
include Representable::JSON
|
12
|
+
include Representable::Coercion
|
13
|
+
property :composed_at, :type => DateTime
|
14
|
+
end
|
15
|
+
|
16
|
+
it "coerces properties in #from_json" do
|
17
|
+
song = Song.new.extend(SongRepresenter).from_json("{\"composed_at\":\"November 18th, 1983\"}")
|
18
|
+
assert_kind_of DateTime, song.composed_at
|
19
|
+
assert_equal DateTime.parse("Fri, 18 Nov 1983 00:00:00 +0000"), song.composed_at
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
22
24
|
class ImmigrantSong
|
23
25
|
include Representable::JSON
|
24
26
|
include Virtus
|
25
|
-
|
27
|
+
include Representable::Coercion
|
26
28
|
|
27
29
|
property :composed_at, :type => DateTime, :default => "May 12th, 2012"
|
28
30
|
end
|
data/test/representable_test.rb
CHANGED
@@ -205,7 +205,7 @@ class RepresentableTest < MiniTest::Spec
|
|
205
205
|
|
206
206
|
describe "#definition_class" do
|
207
207
|
it "returns Definition class" do
|
208
|
-
assert_equal Representable::Definition, Band.definition_class
|
208
|
+
assert_equal Representable::Definition, Band.send(:definition_class)
|
209
209
|
end
|
210
210
|
end
|
211
211
|
|
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.2.
|
4
|
+
version: 1.2.3
|
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-06-
|
12
|
+
date: 2012-06-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -128,17 +128,17 @@ dependencies:
|
|
128
128
|
requirement: !ruby/object:Gem::Requirement
|
129
129
|
none: false
|
130
130
|
requirements:
|
131
|
-
- -
|
131
|
+
- - ~>
|
132
132
|
- !ruby/object:Gem::Version
|
133
|
-
version:
|
133
|
+
version: 0.5.0
|
134
134
|
type: :development
|
135
135
|
prerelease: false
|
136
136
|
version_requirements: !ruby/object:Gem::Requirement
|
137
137
|
none: false
|
138
138
|
requirements:
|
139
|
-
- -
|
139
|
+
- - ~>
|
140
140
|
- !ruby/object:Gem::Version
|
141
|
-
version:
|
141
|
+
version: 0.5.0
|
142
142
|
description: Maps representation documents from and to Ruby objects. Includes XML
|
143
143
|
and JSON support, plain properties, collections and compositions.
|
144
144
|
email:
|