representable 2.1.6 → 2.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +4 -0
- data/lib/representable/object.rb +4 -0
- data/lib/representable/object/binding.rb +9 -0
- data/lib/representable/version.rb +1 -1
- data/test/examples/object.rb +31 -0
- data/test/object_test.rb +25 -4
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89001d90401ddb1d037e061a528599b5a5766a9a
|
4
|
+
data.tar.gz: e7035bf1405dbb411f4daee3aa8bdfb499d8bb3f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 619b6f1015ba1af6a6047864512d39b7f2d0d1b3e237e6124f2967c8fa0dadecef7f0ccd10ce065c1a9b18455fcb09c2ce91e0ab3da5e1417a527f9de0bf4f6b
|
7
|
+
data.tar.gz: 4bcf5e4dae36a937aa1dcba0908ac7dcbd575438c6c8ad0bd92aa011eaa49cb8aa36d4b33f99039713aed14b07b5fa2a564ec06aea6546fa5695f6056c2e77a8
|
data/CHANGES.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
# 2.1.7
|
2
|
+
|
3
|
+
* Adding `Object#to_object`. This is even faster than using `#from_object` for simple transformations.
|
4
|
+
|
1
5
|
# 2.1.6
|
2
6
|
|
3
7
|
* Introducing `Representable::Object` that allows mapping objects to objects. This is way faster than rendering a hash from the source and then writing the hash to the target object.
|
data/lib/representable/object.rb
CHANGED
@@ -21,5 +21,9 @@ module Representable
|
|
21
21
|
def from_object(data, options={}, binding_builder=Binding)
|
22
22
|
update_properties_from(data, options, binding_builder)
|
23
23
|
end
|
24
|
+
|
25
|
+
def to_object(options={}, binding_builder=Binding)
|
26
|
+
create_representation_with(nil, options, binding_builder)
|
27
|
+
end
|
24
28
|
end
|
25
29
|
end
|
@@ -13,10 +13,19 @@ module Representable
|
|
13
13
|
fragment
|
14
14
|
end
|
15
15
|
|
16
|
+
def write(hash, fragment)
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
16
20
|
def deserialize_method
|
17
21
|
:from_object
|
18
22
|
end
|
19
23
|
|
24
|
+
def serialize_method
|
25
|
+
:to_object
|
26
|
+
end
|
27
|
+
|
28
|
+
|
20
29
|
class Collection < self
|
21
30
|
include Representable::Binding::Collection
|
22
31
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
require "ostruct"
|
4
|
+
require "pp"
|
5
|
+
|
6
|
+
|
7
|
+
source = OpenStruct.new(name: "30 Years Live", songs: [
|
8
|
+
OpenStruct.new(id: 1, title: "Dear Beloved"), OpenStruct.new(id: 2, title: "Fuck Armageddon")])
|
9
|
+
|
10
|
+
pp source
|
11
|
+
|
12
|
+
require "representable/object"
|
13
|
+
|
14
|
+
class AlbumRepresenter < Representable::Decorator
|
15
|
+
include Representable::Object
|
16
|
+
|
17
|
+
property :name
|
18
|
+
collection :songs, instance: ->(fragment, *) { Song.new } do
|
19
|
+
property :title
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
Album = Struct.new(:name, :songs)
|
25
|
+
Song = Struct.new(:title)
|
26
|
+
|
27
|
+
target = Album.new
|
28
|
+
|
29
|
+
AlbumRepresenter.new(target).from_object(source)
|
30
|
+
|
31
|
+
pp target
|
data/test/object_test.rb
CHANGED
@@ -8,10 +8,10 @@ class ObjectTest < MiniTest::Spec
|
|
8
8
|
representer!(module: Representable::Object) do
|
9
9
|
property :title
|
10
10
|
|
11
|
-
property :album, instance: lambda { |fragment, *| fragment } do
|
11
|
+
property :album, instance: lambda { |fragment, *| fragment.name.upcase!; fragment } do
|
12
12
|
property :name
|
13
13
|
|
14
|
-
collection :songs, instance: lambda { |fragment, *|fragment } do
|
14
|
+
collection :songs, instance: lambda { |fragment, *| fragment.title.upcase!; fragment } do
|
15
15
|
property :title
|
16
16
|
end
|
17
17
|
end
|
@@ -25,8 +25,8 @@ class ObjectTest < MiniTest::Spec
|
|
25
25
|
representer.prepare(target).from_object(source)
|
26
26
|
|
27
27
|
target.title.must_equal "The King Is Dead"
|
28
|
-
target.album.name.must_equal "
|
29
|
-
target.album.songs[0].title.must_equal "
|
28
|
+
target.album.name.must_equal "RUINER"
|
29
|
+
target.album.songs[0].title.must_equal "IN VINO VERITAS II"
|
30
30
|
end
|
31
31
|
|
32
32
|
# ignore nested object when nil
|
@@ -36,4 +36,25 @@ class ObjectTest < MiniTest::Spec
|
|
36
36
|
target.title.must_equal nil # scalar property gets overridden when nil.
|
37
37
|
target.album.must_equal nil # nested property stays nil.
|
38
38
|
end
|
39
|
+
|
40
|
+
# to_object
|
41
|
+
describe "#to_object" do
|
42
|
+
representer!(module: Representable::Object) do
|
43
|
+
property :title
|
44
|
+
|
45
|
+
property :album, render_filter: lambda { |object, *| object.name = "Live"; object } do
|
46
|
+
property :name
|
47
|
+
|
48
|
+
collection :songs, render_filter: lambda { |object, *| object[0].title = 1; object } do
|
49
|
+
property :title
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it do
|
55
|
+
representer.prepare(source).to_object
|
56
|
+
source.album.name.must_equal "Live"
|
57
|
+
source.album.songs[0].title.must_equal 1
|
58
|
+
end
|
59
|
+
end
|
39
60
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: representable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -215,6 +215,7 @@ files:
|
|
215
215
|
- test/decorator_test.rb
|
216
216
|
- test/definition_test.rb
|
217
217
|
- test/example.rb
|
218
|
+
- test/examples/object.rb
|
218
219
|
- test/exec_context_test.rb
|
219
220
|
- test/features_test.rb
|
220
221
|
- test/filter_test.rb
|
@@ -288,6 +289,7 @@ test_files:
|
|
288
289
|
- test/decorator_test.rb
|
289
290
|
- test/definition_test.rb
|
290
291
|
- test/example.rb
|
292
|
+
- test/examples/object.rb
|
291
293
|
- test/exec_context_test.rb
|
292
294
|
- test/features_test.rb
|
293
295
|
- test/filter_test.rb
|