representable 2.1.5 → 2.1.6

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: 351c7071b33faa1215070024868b2614288915f4
4
- data.tar.gz: 9960a2f4c850764bc48ad3db09c84581fb53049a
3
+ metadata.gz: 8c49b50c08553f95fcf4125c6fecd1a951f9303f
4
+ data.tar.gz: 1f1236f5bc0b0736f4a042b8cd85b928ab8cec04
5
5
  SHA512:
6
- metadata.gz: ae7afd058a763a369761633d3375d865d583e7761988665580c6765980b254dc0443cdc3c3b641a5de3f94476f2cecaa23550dc3bc72c0f10e9b2d215fb6c269
7
- data.tar.gz: f44ba7ec3298e2d264be42f491561a00dbb1375a3ff347848abdb4ab5a87d603ad86a1f513721752b48864204be89e2fd5ce512b068d5fc0c183c7c1b0aef106
6
+ metadata.gz: 04b4160bcc81156d193f141167c3f954f30429fd96afdfab08502db8537806937329370794f7067aa207297a85ad70b7fe3e6139f312c76f60b24c515e6b7a5e
7
+ data.tar.gz: 96ff73a1056aacdffb00a0742dc218c525ab596325042142878a0de6894c5ce459a4e2a5012270eca554f9dae9265b3aa3e6f97c1784f4cc6390465a23cf2fe0
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 2.1.6
2
+
3
+ * 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.
4
+ * Fixed loading issues when requiring `representable/decorator`, only.
5
+
1
6
  # 2.1.5
2
7
 
3
8
  * Using `inherit: true` now works even if the parent property hasn't been defined before. It will simply create a new property. This used to crash with `undefined method `merge!' for nil:NilClass`.
data/README.md CHANGED
@@ -9,7 +9,7 @@ Representable is helpful for all kind of mappings, rendering and parsing workflo
9
9
 
10
10
  ## Installation
11
11
 
12
- The representable gem runs with all Ruby versions >= 1.8.7.
12
+ The representable gem runs with all Ruby versions >= 1.9.3.
13
13
 
14
14
  ```ruby
15
15
  gem 'representable'
@@ -950,7 +950,7 @@ end
950
950
  The `#items` method lets you configure the contained entity representing here.
951
951
 
952
952
  ```ruby
953
- [Song.new(title: "Hyper Music"), Song.new(title: "Screenager")].extend(SongsRepresenter).to_json
953
+ [Song.new(title: "Hyper Music"), Song.new(title: "Screenager")].extend(SongsRepresenter.for_collection).to_json
954
954
  #=> [{"title":"Hyper Music"},{"title":"Screenager"}]
955
955
  ```
956
956
 
@@ -1202,7 +1202,7 @@ end
1202
1202
  In a decorator it works alike.
1203
1203
 
1204
1204
  ```ruby
1205
- module SongRepresenter < Representable::Decorator
1205
+ class SongRepresenter < Representable::Decorator
1206
1206
  include Representable::JSON
1207
1207
  include Representable::Coercion
1208
1208
 
@@ -1334,6 +1334,28 @@ end
1334
1334
 
1335
1335
  This will give you a behavior close to Rails' `HashWithIndifferentAccess` by stringifying the incoming hash internally.
1336
1336
 
1337
+ ## Object to Object Mapping
1338
+
1339
+ Since Representable 2.1.6 we also allow mapping objects to objects directly. This is extremely fast as the source object is queried for its properties and then values are directly written to the target object.
1340
+
1341
+ `````ruby
1342
+ require "representable/object"
1343
+
1344
+ module SongRepresenter
1345
+ include Representable::Object
1346
+
1347
+ property :title
1348
+ end
1349
+ ```
1350
+
1351
+ As always, the source object needs to have readers for all properties and the target writers.
1352
+
1353
+ ```ruby
1354
+ song_copy = OpenStruct.new.extend(SongRepresenter).from_object(song)
1355
+ song_copy.title = "Roxanne"
1356
+ ```
1357
+
1358
+ All kind of transformation options can be used as well as nesting.
1337
1359
 
1338
1360
  ## Debugging
1339
1361
 
@@ -1,3 +1,5 @@
1
+ require "representable"
2
+
1
3
  module Representable
2
4
  class Decorator
3
5
  attr_reader :represented
@@ -7,7 +7,10 @@ module Representable
7
7
  end
8
8
 
9
9
  def read(hash)
10
- hash.send(as) # :getter? no, that's for parsing!
10
+ fragment = hash.send(as) # :getter? no, that's for parsing!
11
+
12
+ return FragmentNotFound if fragment.nil? and typed?
13
+ fragment
11
14
  end
12
15
 
13
16
  def deserialize_method
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "2.1.5"
2
+ VERSION = "2.1.6"
3
3
  end
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
+ s.license = "MIT"
21
22
 
22
23
  s.add_dependency "nokogiri"
23
24
  s.add_dependency "multi_json"
@@ -15,7 +15,6 @@ class DecoratorTest < MiniTest::Spec
15
15
 
16
16
  let (:song) { Song.new("Mama, I'm Coming Home") }
17
17
  let (:album) { Album.new([song]) }
18
- let (:decorator) { AlbumRepresentation.new(album) }
19
18
 
20
19
  describe "inheritance" do
21
20
  let (:inherited_decorator) do
@@ -27,10 +26,14 @@ class DecoratorTest < MiniTest::Spec
27
26
  it { inherited_decorator.to_hash.must_equal({"songs"=>[{"name"=>"Mama, I'm Coming Home"}], "best_song"=>"Stand Up"}) }
28
27
  end
29
28
 
29
+ let (:decorator) { AlbumRepresentation.new(album) }
30
+
30
31
  it "renders" do
31
32
  decorator.to_hash.must_equal({"songs"=>[{"name"=>"Mama, I'm Coming Home"}]})
32
33
  album.wont_respond_to :to_hash
33
34
  song.wont_respond_to :to_hash # DISCUSS: weak test, how to assert blank slate?
35
+ # no @representable_attrs in decorated objects
36
+ song.instance_variable_get(:@representable_attrs).must_equal nil
34
37
  end
35
38
 
36
39
  describe "#from_hash" do
@@ -52,4 +55,21 @@ class DecoratorTest < MiniTest::Spec
52
55
  AlbumRepresentation.prepare(album).decorated.must_equal album
53
56
  end
54
57
  end
58
+
59
+
60
+ describe "inline decorators" do
61
+ representer!(decorator: true) do
62
+ collection :songs, :class => Song do
63
+ property :name
64
+ end
65
+ end
66
+
67
+ it "does not pollute represented" do
68
+ representer.new(album).from_hash({"songs"=>[{"name"=>"Atomic Garden"}]})
69
+
70
+ # no @representable_attrs in decorated objects
71
+ song.instance_variable_get(:@representable_attrs).must_equal nil
72
+ album.instance_variable_get(:@representable_attrs).must_equal nil
73
+ end
74
+ end
55
75
  end
@@ -28,4 +28,12 @@ class ObjectTest < MiniTest::Spec
28
28
  target.album.name.must_equal "Ruiner"
29
29
  target.album.songs[0].title.must_equal "In Vino Veritas II"
30
30
  end
31
+
32
+ # ignore nested object when nil
33
+ it do
34
+ representer.prepare(Song.new("The King Is Dead")).from_object(Song.new)
35
+
36
+ target.title.must_equal nil # scalar property gets overridden when nil.
37
+ target.album.must_equal nil # nested property stays nil.
38
+ end
31
39
  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.5
4
+ version: 2.1.6
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-02-04 00:00:00.000000000 Z
11
+ date: 2015-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -252,7 +252,8 @@ files:
252
252
  - test/xml_test.rb
253
253
  - test/yaml_test.rb
254
254
  homepage: https://github.com/apotonick/representable/
255
- licenses: []
255
+ licenses:
256
+ - MIT
256
257
  metadata: {}
257
258
  post_install_message:
258
259
  rdoc_options: []