representable 2.1.3 → 2.1.4
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.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -0
- data/README.md +1 -1
- data/lib/representable/hash/collection.rb +1 -1
- data/lib/representable/object.rb +25 -0
- data/lib/representable/object/binding.rb +22 -0
- data/lib/representable/parse_strategies.rb +1 -1
- data/lib/representable/version.rb +1 -1
- data/lib/representable/xml/collection.rb +2 -0
- data/lib/representable/yaml.rb +1 -0
- data/test/lonely_test.rb +27 -0
- data/test/object_test.rb +31 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19f89d36352fe54ab21a3759d6c85392950dc564
|
4
|
+
data.tar.gz: c3e581d0e9fb29d0a47f608a8b9b960b48f83fe4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4cdc220373cb17d0f9934ddc8224925938c274a9489f8da931bcdac9c9782ecde3f1b5204bf7b9b7643b28105b52ba80b786e150d480a8893549e264f3612ce
|
7
|
+
data.tar.gz: 545f594e8786f27d2fca07dcd41241b9b754aacc9298fc02f24f8ff8916381046e24d03bb6907062a323448bc85c4403713e43aa04aaf2bd0f8032cfe519dd74
|
data/CHANGES.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
# 2.1.4
|
2
|
+
|
3
|
+
* Allow lonely collection representers without configuration, with inline representer, only. This is for render-only collection representers and very handy.
|
4
|
+
* Now runs with MagLev.
|
5
|
+
|
1
6
|
# 2.1.3
|
2
7
|
|
3
8
|
* Like 2.1.2 (got yanked) because I thought it's buggy but it's not. What has changed is that `Serializer::Collection#serialize` no longer does `collection.collect` but `collection.each` since this allows filtering out unwanted elements.
|
data/README.md
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'representable'
|
2
|
+
require 'representable/object/binding'
|
3
|
+
|
4
|
+
module Representable
|
5
|
+
module Object
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
include Representable
|
9
|
+
extend ClassMethods
|
10
|
+
register_feature Representable::Object
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
def collection_representer_class
|
17
|
+
Collection
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def from_object(data, options={}, binding_builder=Binding)
|
22
|
+
update_properties_from(data, options, binding_builder)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Representable
|
2
|
+
module Object
|
3
|
+
class Binding < Representable::Binding
|
4
|
+
def self.build_for(definition, *args) # TODO: remove default arg.
|
5
|
+
return Collection.new(definition, *args) if definition.array?
|
6
|
+
new(definition, *args)
|
7
|
+
end
|
8
|
+
|
9
|
+
def read(hash)
|
10
|
+
hash.send(as) # :getter? no, that's for parsing!
|
11
|
+
end
|
12
|
+
|
13
|
+
def deserialize_method
|
14
|
+
:from_object
|
15
|
+
end
|
16
|
+
|
17
|
+
class Collection < self
|
18
|
+
include Representable::Binding::Collection
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -50,7 +50,7 @@ module Representable
|
|
50
50
|
args = args.last # TODO: don't pass i as separate block parameter but in Options.
|
51
51
|
object_class = args.binding[:class].evaluate(self, fragment, args)
|
52
52
|
|
53
|
-
object_class.find_by(id: fragment["id"]) or object_class.new
|
53
|
+
object_class.find_by({id: fragment["id"]}) or object_class.new
|
54
54
|
}
|
55
55
|
end
|
56
56
|
end
|
@@ -3,10 +3,12 @@ module Representable::XML
|
|
3
3
|
include Representable::XML
|
4
4
|
|
5
5
|
def self.included(base)
|
6
|
+
base.send :include, Representable::XML
|
6
7
|
base.send :include, Representable::Hash::Collection
|
7
8
|
base.send :include, Methods
|
8
9
|
end
|
9
10
|
|
11
|
+
|
10
12
|
module Methods
|
11
13
|
def update_properties_from(doc, *args)
|
12
14
|
super(doc.search("./*"), *args) # pass the list of collection items to Hash::Collection#update_properties_from.
|
data/lib/representable/yaml.rb
CHANGED
data/test/lonely_test.rb
CHANGED
@@ -3,6 +3,33 @@ require 'test_helper'
|
|
3
3
|
require 'representable/json/hash'
|
4
4
|
|
5
5
|
class LonelyRepresenterTest < MiniTest::Spec
|
6
|
+
|
7
|
+
# test ::items without arguments, render-only.
|
8
|
+
for_formats(
|
9
|
+
:hash => [Representable::Hash::Collection, [{"name"=>"Resist Stance"}, {"name"=>"Suffer"}]],
|
10
|
+
:json => [Representable::JSON::Collection, "[{\"name\":\"Resist Stance\"},{\"name\":\"Suffer\"}]"],
|
11
|
+
:xml => [Representable::XML::Collection, "<array><song><name>Resist Stance</name></song><song><name>Suffer</name></song></array>"],
|
12
|
+
) do |format, mod, output, input|
|
13
|
+
|
14
|
+
describe "[#{format}] lonely collection, render-only" do # TODO: introduce :representable option?
|
15
|
+
let (:format) { format }
|
16
|
+
|
17
|
+
representer!(module: mod) do
|
18
|
+
items do
|
19
|
+
property :name
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
let (:album) { [Song.new("Resist Stance"), Song.new("Suffer")].extend(representer) }
|
24
|
+
|
25
|
+
it "calls #to_hash on song instances, nothing else" do
|
26
|
+
render(album).must_equal_document(output)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
|
6
33
|
module SongRepresenter
|
7
34
|
include Representable::JSON
|
8
35
|
|
data/test/object_test.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "representable/object"
|
3
|
+
|
4
|
+
class ObjectTest < MiniTest::Spec
|
5
|
+
Song = Struct.new(:title, :album)
|
6
|
+
Album = Struct.new(:name, :songs)
|
7
|
+
|
8
|
+
representer!(module: Representable::Object) do
|
9
|
+
property :title
|
10
|
+
|
11
|
+
property :album, instance: lambda { |fragment, *| fragment } do
|
12
|
+
property :name
|
13
|
+
|
14
|
+
collection :songs, instance: lambda { |fragment, *|fragment } do
|
15
|
+
property :title
|
16
|
+
end
|
17
|
+
end
|
18
|
+
# TODO: collection
|
19
|
+
end
|
20
|
+
|
21
|
+
let (:source) { Song.new("The King Is Dead", Album.new("Ruiner", [Song.new("In Vino Veritas II")])) }
|
22
|
+
let (:target) { Song.new }
|
23
|
+
|
24
|
+
it do
|
25
|
+
representer.prepare(target).from_object(source)
|
26
|
+
|
27
|
+
target.title.must_equal "The King Is Dead"
|
28
|
+
target.album.name.must_equal "Ruiner"
|
29
|
+
target.album.songs[0].title.must_equal "In Vino Veritas II"
|
30
|
+
end
|
31
|
+
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -189,6 +189,8 @@ files:
|
|
189
189
|
- lib/representable/json/collection.rb
|
190
190
|
- lib/representable/json/hash.rb
|
191
191
|
- lib/representable/mapper.rb
|
192
|
+
- lib/representable/object.rb
|
193
|
+
- lib/representable/object/binding.rb
|
192
194
|
- lib/representable/parse_strategies.rb
|
193
195
|
- lib/representable/pipeline.rb
|
194
196
|
- lib/representable/populator.rb
|
@@ -231,6 +233,7 @@ files:
|
|
231
233
|
- test/lonely_test.rb
|
232
234
|
- test/mongoid_test.rb
|
233
235
|
- test/nested_test.rb
|
236
|
+
- test/object_test.rb
|
234
237
|
- test/parse_strategy_test.rb
|
235
238
|
- test/pass_options_test.rb
|
236
239
|
- test/prepare_test.rb
|
@@ -302,6 +305,7 @@ test_files:
|
|
302
305
|
- test/lonely_test.rb
|
303
306
|
- test/mongoid_test.rb
|
304
307
|
- test/nested_test.rb
|
308
|
+
- test/object_test.rb
|
305
309
|
- test/parse_strategy_test.rb
|
306
310
|
- test/pass_options_test.rb
|
307
311
|
- test/prepare_test.rb
|