representable 3.0.1 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -2
- data/CHANGES.md +6 -0
- data/Gemfile +0 -8
- data/README.md +0 -2
- data/lib/representable/autoload.rb +4 -0
- data/lib/representable/config.rb +5 -0
- data/lib/representable/declarative.rb +3 -3
- data/lib/representable/hash.rb +3 -0
- data/lib/representable/json.rb +3 -0
- data/lib/representable/json/hash.rb +0 -1
- data/lib/representable/populator.rb +1 -1
- data/lib/representable/serializer.rb +1 -1
- data/lib/representable/version.rb +1 -1
- data/lib/representable/xml.rb +3 -0
- data/lib/representable/yaml.rb +3 -0
- data/representable.gemspec +0 -3
- data/test/decorator_test.rb +13 -0
- data/test/example.rb +0 -1
- data/test/hash_test.rb +23 -1
- data/test/json_test.rb +23 -0
- data/test/mongoid_test.rb +28 -28
- data/test/representable_test.rb +0 -2
- data/test/test_helper.rb +7 -5
- data/test/xml_bindings_test.rb +0 -4
- data/test/xml_test.rb +54 -13
- data/test/yaml_test.rb +26 -1
- metadata +2 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ac8f58fd73f1e9aa9e35f3298490086dfb93d54
|
4
|
+
data.tar.gz: 054dcc8fdcf9cc7ce6f223747af5914457d19e90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af903f251a75b3d57899b4a0b428a84ff5436885939a8b59b6fb42b9fa4f517efca96ccb9b01bee969d9602a542b2b58a81171be8f4175204090a0e6c8f292c2
|
7
|
+
data.tar.gz: 58b9716576c0d790d1b4584ac480760c9539ca61cea4618574fec0e7cb9f415fe2b35d156839f1b2d1c531433fa652160faea4bb14b6d2299341e62c094ec426
|
data/.travis.yml
CHANGED
data/CHANGES.md
CHANGED
data/Gemfile
CHANGED
@@ -2,19 +2,11 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
-
platform :rbx do
|
6
|
-
gem "psych"
|
7
|
-
gem "rubysl-irb"
|
8
|
-
gem "json_pure"
|
9
|
-
end
|
10
|
-
|
11
5
|
group :test do
|
12
6
|
gem "nokogiri", require: false
|
13
7
|
gem "multi_json", require: false
|
14
8
|
gem "minitest-line"
|
15
|
-
gem "pry"
|
16
9
|
end
|
17
10
|
|
18
11
|
# gem "declarative", path: "../declarative"
|
19
12
|
# gem "declarative", github: "apotonick/declarative"
|
20
|
-
gem "uber", path: "../uber"
|
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
module Representable
|
2
2
|
autoload :Hash, 'representable/hash'
|
3
|
+
autoload :JSON, 'representable/json'
|
4
|
+
autoload :Object, 'representable/object'
|
5
|
+
autoload :YAML, 'representable/yaml'
|
6
|
+
autoload :XML, 'representable/xml'
|
3
7
|
|
4
8
|
module Hash
|
5
9
|
autoload :AllowSymbols, 'representable/hash/allow_symbols'
|
data/lib/representable/config.rb
CHANGED
@@ -5,6 +5,11 @@ module Representable
|
|
5
5
|
# Overwrite definition_class if you need a custom Definition object (helpful when using
|
6
6
|
# representable in other gems).
|
7
7
|
class Config < ::Declarative::Definitions
|
8
|
+
def initialize(*)
|
9
|
+
super
|
10
|
+
@wrap = nil
|
11
|
+
end
|
12
|
+
|
8
13
|
def remove(name)
|
9
14
|
delete(name.to_s)
|
10
15
|
end
|
@@ -21,9 +21,9 @@ module Representable
|
|
21
21
|
# them to the original object.
|
22
22
|
def nested(name, options={}, &block)
|
23
23
|
options = options.merge(
|
24
|
-
getter: ->(
|
25
|
-
setter: ->(
|
26
|
-
instance: ->(
|
24
|
+
getter: ->(opts) { self },
|
25
|
+
setter: ->(opts) { },
|
26
|
+
instance: ->(opts) { self },
|
27
27
|
)
|
28
28
|
|
29
29
|
if block
|
data/lib/representable/hash.rb
CHANGED
data/lib/representable/json.rb
CHANGED
@@ -21,7 +21,7 @@ module Representable
|
|
21
21
|
def self.apply!(options)
|
22
22
|
return unless populator = options[:populator]
|
23
23
|
|
24
|
-
options[:parse_pipeline] = ->(input,
|
24
|
+
options[:parse_pipeline] = ->(input, opts) do
|
25
25
|
pipeline = Pipeline[*parse_functions] # TODO: AssignFragment
|
26
26
|
pipeline = Pipeline::Insert.(pipeline, SetValue, delete: true) # remove the setter function.
|
27
27
|
pipeline = Pipeline::Insert.(pipeline, populator, replace: CreateObject::Populator) # let the actual populator do the job.
|
@@ -3,7 +3,7 @@ module Representable
|
|
3
3
|
options[:binding].evaluate_option(:getter, input, options)
|
4
4
|
end
|
5
5
|
|
6
|
-
GetValue = ->(input, options) { options[:binding].send(:exec_context, options).
|
6
|
+
GetValue = ->(input, options) { options[:binding].send(:exec_context, options).public_send(options[:binding].getter) }
|
7
7
|
|
8
8
|
Writer = ->(input, options) do
|
9
9
|
options[:binding].evaluate_option(:writer, input, options)
|
data/lib/representable/xml.rb
CHANGED
data/lib/representable/yaml.rb
CHANGED
data/representable.gemspec
CHANGED
@@ -27,9 +27,6 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_development_dependency "rake"
|
28
28
|
spec.add_development_dependency "test_xml", "0.1.6"
|
29
29
|
spec.add_development_dependency "minitest"
|
30
|
-
spec.add_development_dependency "mongoid"
|
31
30
|
spec.add_development_dependency "virtus"
|
32
|
-
spec.add_development_dependency "json", '>= 1.7.7'
|
33
|
-
|
34
31
|
spec.add_development_dependency "ruby-prof"
|
35
32
|
end
|
data/test/decorator_test.rb
CHANGED
@@ -12,9 +12,18 @@ class DecoratorTest < MiniTest::Spec
|
|
12
12
|
collection :songs, :class => Song, :extend => SongRepresentation
|
13
13
|
end
|
14
14
|
|
15
|
+
class RatingRepresentation < Representable::Decorator
|
16
|
+
include Representable::JSON
|
17
|
+
|
18
|
+
property :system
|
19
|
+
property :value
|
20
|
+
end
|
21
|
+
|
15
22
|
let (:song) { Song.new("Mama, I'm Coming Home") }
|
16
23
|
let (:album) { Album.new([song]) }
|
17
24
|
|
25
|
+
let (:rating) { OpenStruct.new(system: 'MPAA', value: 'R') }
|
26
|
+
|
18
27
|
describe "inheritance" do
|
19
28
|
let (:inherited_decorator) do
|
20
29
|
Class.new(AlbumRepresentation) do
|
@@ -27,12 +36,16 @@ class DecoratorTest < MiniTest::Spec
|
|
27
36
|
|
28
37
|
let (:decorator) { AlbumRepresentation.new(album) }
|
29
38
|
|
39
|
+
let(:rating_decorator) { RatingRepresentation.new(rating) }
|
40
|
+
|
30
41
|
it "renders" do
|
31
42
|
decorator.to_hash.must_equal({"songs"=>[{"name"=>"Mama, I'm Coming Home"}]})
|
32
43
|
album.wont_respond_to :to_hash
|
33
44
|
song.wont_respond_to :to_hash # DISCUSS: weak test, how to assert blank slate?
|
34
45
|
# no @representable_attrs in decorated objects
|
35
46
|
song.instance_variable_get(:@representable_attrs).must_equal nil
|
47
|
+
|
48
|
+
rating_decorator.to_hash.must_equal({"system" => "MPAA", "value" => "R"})
|
36
49
|
end
|
37
50
|
|
38
51
|
describe "#from_hash" do
|
data/test/example.rb
CHANGED
data/test/hash_test.rb
CHANGED
@@ -1,5 +1,27 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
+
class HashPublicMethodsTest < Minitest::Spec
|
4
|
+
#---
|
5
|
+
# from_hash
|
6
|
+
class BandRepresenter < Representable::Decorator
|
7
|
+
include Representable::Hash
|
8
|
+
property :id
|
9
|
+
property :name
|
10
|
+
end
|
11
|
+
|
12
|
+
let (:data) { {"id"=>1,"name"=>"Rancid"} }
|
13
|
+
|
14
|
+
it { BandRepresenter.new(Band.new).from_hash(data)[:id, :name].must_equal [1, "Rancid"] }
|
15
|
+
it { BandRepresenter.new(Band.new).parse(data)[:id, :name].must_equal [1, "Rancid"] }
|
16
|
+
|
17
|
+
#---
|
18
|
+
# to_hash
|
19
|
+
let (:band) { Band.new(1, "Rancid") }
|
20
|
+
|
21
|
+
it { BandRepresenter.new(band).to_hash.must_equal data }
|
22
|
+
it { BandRepresenter.new(band).render.must_equal data }
|
23
|
+
end
|
24
|
+
|
3
25
|
class HashWithScalarPropertyTest < MiniTest::Spec
|
4
26
|
Album = Struct.new(:title)
|
5
27
|
|
@@ -157,4 +179,4 @@ class HashWithScalarCollectionTest < MiniTest::Spec
|
|
157
179
|
album.extend(representer).from_hash("songs" => ["Off Key Melody", "Sinking"]).must_equal Album.new(["Off Key Melody", "Sinking"])
|
158
180
|
end
|
159
181
|
end
|
160
|
-
end
|
182
|
+
end
|
data/test/json_test.rb
CHANGED
@@ -1,10 +1,33 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
module JsonTest
|
4
|
+
class JSONPublicMethodsTest < Minitest::Spec
|
5
|
+
#---
|
6
|
+
# from_json
|
7
|
+
class BandRepresenter < Representable::Decorator
|
8
|
+
include Representable::JSON
|
9
|
+
property :id
|
10
|
+
property :name
|
11
|
+
end
|
12
|
+
|
13
|
+
let (:json) { '{"id":1,"name":"Rancid"}' }
|
14
|
+
|
15
|
+
it { BandRepresenter.new(Band.new).from_json(json)[:id, :name].must_equal [1, "Rancid"] }
|
16
|
+
it { BandRepresenter.new(Band.new).parse(json)[:id, :name].must_equal [1, "Rancid"] }
|
17
|
+
|
18
|
+
#---
|
19
|
+
# to_json
|
20
|
+
let (:band) { Band.new(1, "Rancid") }
|
21
|
+
|
22
|
+
it { BandRepresenter.new(band).to_json.must_equal json }
|
23
|
+
it { BandRepresenter.new(band).render.must_equal json }
|
24
|
+
end
|
25
|
+
|
4
26
|
class APITest < MiniTest::Spec
|
5
27
|
Json = Representable::JSON
|
6
28
|
Def = Representable::Definition
|
7
29
|
|
30
|
+
|
8
31
|
describe "JSON module" do
|
9
32
|
before do
|
10
33
|
@Band = Class.new do
|
data/test/mongoid_test.rb
CHANGED
@@ -1,31 +1,31 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'mongoid'
|
3
|
-
require 'mongoid/document'
|
1
|
+
# require 'test_helper'
|
2
|
+
# require 'mongoid'
|
3
|
+
# require 'mongoid/document'
|
4
4
|
|
5
|
-
class MongoidTest < MiniTest::Spec
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
class Dude
|
14
|
-
include Mongoid::Document
|
15
|
-
embeds_one :profile, :class_name => "MongoidTest::Profile"
|
16
|
-
end
|
17
|
-
|
18
|
-
module ProfileRepresenter
|
19
|
-
include Representable::JSON
|
5
|
+
# class MongoidTest < MiniTest::Spec
|
6
|
+
# describe "Mongoid compatibility" do
|
7
|
+
# it "allows #to_json" do
|
8
|
+
# class Profile
|
9
|
+
# include Mongoid::Document
|
10
|
+
# field :name
|
11
|
+
# end
|
20
12
|
|
21
|
-
|
22
|
-
|
13
|
+
# class Dude
|
14
|
+
# include Mongoid::Document
|
15
|
+
# embeds_one :profile, :class_name => "MongoidTest::Profile"
|
16
|
+
# end
|
23
17
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
18
|
+
# module ProfileRepresenter
|
19
|
+
# include Representable::JSON
|
20
|
+
|
21
|
+
# property :name
|
22
|
+
# end
|
23
|
+
|
24
|
+
# dude = Dude.new
|
25
|
+
# dude.profile = Profile.new
|
26
|
+
# dude.profile.name = "Kofi"
|
27
|
+
|
28
|
+
# assert_equal "{\"name\":\"Kofi\"}", dude.profile.extend(ProfileRepresenter).to_json
|
29
|
+
# end
|
30
|
+
# end
|
31
|
+
# end
|
data/test/representable_test.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,11 +1,7 @@
|
|
1
1
|
require 'representable'
|
2
2
|
|
3
|
-
require "json"
|
4
3
|
require "psych"
|
5
4
|
|
6
|
-
require 'representable/json'
|
7
|
-
require 'representable/xml'
|
8
|
-
require 'representable/yaml'
|
9
5
|
require 'minitest/autorun'
|
10
6
|
require 'test_xml/mini_test'
|
11
7
|
|
@@ -132,4 +128,10 @@ class BaseTest < MiniTest::Spec
|
|
132
128
|
let (:song) { OpenStruct.new(:title => "Resist Stance") }
|
133
129
|
let (:song_representer) { Module.new do include Representable::Hash; property :title end }
|
134
130
|
|
135
|
-
end
|
131
|
+
end
|
132
|
+
|
133
|
+
Band = Struct.new(:id, :name) do
|
134
|
+
def [](*attrs)
|
135
|
+
attrs.collect { |attr| send(attr) }
|
136
|
+
end
|
137
|
+
end
|
data/test/xml_bindings_test.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
require 'representable/json' # FIXME.
|
3
|
-
require 'representable/xml/collection'
|
4
2
|
require 'representable/xml/hash'
|
5
3
|
|
6
|
-
require 'representable/xml'
|
7
|
-
|
8
4
|
class XMLBindingTest < MiniTest::Spec
|
9
5
|
module SongRepresenter
|
10
6
|
include Representable::XML
|
data/test/xml_test.rb
CHANGED
@@ -1,22 +1,41 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
require 'representable/xml'
|
3
2
|
|
4
|
-
class Band
|
5
|
-
include Representable::XML
|
6
|
-
property :name
|
7
|
-
attr_accessor :name
|
8
3
|
|
9
|
-
|
10
|
-
|
4
|
+
class XmlPublicMethodsTest < Minitest::Spec
|
5
|
+
#---
|
6
|
+
# from_hash
|
7
|
+
class BandRepresenter < Representable::Decorator
|
8
|
+
include Representable::XML
|
9
|
+
property :id
|
10
|
+
property :name
|
11
11
|
end
|
12
|
-
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
let (:data) { %{<data><id>1</id><name>Rancid</name></data>} }
|
14
|
+
|
15
|
+
it { BandRepresenter.new(Band.new).from_xml(data)[:id, :name].must_equal ["1", "Rancid"] }
|
16
|
+
it { BandRepresenter.new(Band.new).parse(data)[:id, :name].must_equal ["1", "Rancid"] }
|
17
17
|
|
18
|
+
#---
|
19
|
+
# to_hash
|
20
|
+
let (:band) { Band.new("1", "Rancid") }
|
21
|
+
|
22
|
+
it { BandRepresenter.new(band).to_xml.must_equal_xml data }
|
23
|
+
it { BandRepresenter.new(band).render.must_equal_xml data }
|
24
|
+
end
|
18
25
|
|
19
26
|
class XmlTest < MiniTest::Spec
|
27
|
+
|
28
|
+
class Band
|
29
|
+
include Representable::XML
|
30
|
+
property :name
|
31
|
+
attr_accessor :name
|
32
|
+
|
33
|
+
def initialize(name=nil)
|
34
|
+
name and self.name = name
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
20
39
|
XML = Representable::XML
|
21
40
|
Def = Representable::Definition
|
22
41
|
|
@@ -156,7 +175,8 @@ class XmlTest < MiniTest::Spec
|
|
156
175
|
end
|
157
176
|
|
158
177
|
it "extends contained models when serializing" do
|
159
|
-
@album = Album.new(
|
178
|
+
@album = Album.new(
|
179
|
+
[Song.new("I Hate My Brain"), mr=Song.new("Mr. Charisma")], mr)
|
160
180
|
@album.extend(AlbumRepresenter)
|
161
181
|
|
162
182
|
assert_xml_equal "<album>
|
@@ -222,6 +242,16 @@ class CDataBand
|
|
222
242
|
end
|
223
243
|
|
224
244
|
class TypedPropertyTest < MiniTest::Spec
|
245
|
+
class Band
|
246
|
+
include Representable::XML
|
247
|
+
property :name
|
248
|
+
attr_accessor :name
|
249
|
+
|
250
|
+
def initialize(name=nil)
|
251
|
+
name and self.name = name
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
225
255
|
module AlbumRepresenter
|
226
256
|
include Representable::XML
|
227
257
|
property :band, :class => Band
|
@@ -288,6 +318,17 @@ end
|
|
288
318
|
|
289
319
|
|
290
320
|
class CollectionTest < MiniTest::Spec
|
321
|
+
class Band
|
322
|
+
include Representable::XML
|
323
|
+
property :name
|
324
|
+
attr_accessor :name
|
325
|
+
|
326
|
+
def initialize(name=nil)
|
327
|
+
name and self.name = name
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
|
291
332
|
describe ":class => Band, :as => :band, :collection => true" do
|
292
333
|
class Compilation
|
293
334
|
include Representable::XML
|
@@ -500,4 +541,4 @@ class XmlHashTest < MiniTest::Spec
|
|
500
541
|
# FIXME: this NEVER worked!
|
501
542
|
# it { OpenStruct.new.extend(representer).from_xml(doc).songs.must_equal({"first" => "The Gargoyle", "second" => "Bronx"}) }
|
502
543
|
end
|
503
|
-
end
|
544
|
+
end
|
data/test/yaml_test.rb
CHANGED
@@ -1,5 +1,30 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
+
class YamlPublicMethodsTest < Minitest::Spec
|
4
|
+
#---
|
5
|
+
# from_yaml
|
6
|
+
class BandRepresenter < Representable::Decorator
|
7
|
+
include Representable::YAML
|
8
|
+
property :id
|
9
|
+
property :name
|
10
|
+
end
|
11
|
+
|
12
|
+
let (:data) { %{---
|
13
|
+
id: 1
|
14
|
+
name: Rancid
|
15
|
+
} }
|
16
|
+
|
17
|
+
it { BandRepresenter.new(Band.new).from_yaml(data)[:id, :name].must_equal [1, "Rancid"] }
|
18
|
+
it { BandRepresenter.new(Band.new).parse(data)[:id, :name].must_equal [1, "Rancid"] }
|
19
|
+
|
20
|
+
#---
|
21
|
+
# to_yaml
|
22
|
+
let (:band) { Band.new("1", "Rancid") }
|
23
|
+
|
24
|
+
it { BandRepresenter.new(band).to_yaml.must_equal data }
|
25
|
+
it { BandRepresenter.new(band).render.must_equal data }
|
26
|
+
end
|
27
|
+
|
3
28
|
class YamlTest < MiniTest::Spec
|
4
29
|
def self.yaml_representer(&block)
|
5
30
|
Module.new do
|
@@ -159,4 +184,4 @@ songs:
|
|
159
184
|
end
|
160
185
|
end
|
161
186
|
end
|
162
|
-
end
|
187
|
+
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: 3.0.
|
4
|
+
version: 3.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: uber
|
@@ -86,20 +86,6 @@ dependencies:
|
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '0'
|
89
|
-
- !ruby/object:Gem::Dependency
|
90
|
-
name: mongoid
|
91
|
-
requirement: !ruby/object:Gem::Requirement
|
92
|
-
requirements:
|
93
|
-
- - ">="
|
94
|
-
- !ruby/object:Gem::Version
|
95
|
-
version: '0'
|
96
|
-
type: :development
|
97
|
-
prerelease: false
|
98
|
-
version_requirements: !ruby/object:Gem::Requirement
|
99
|
-
requirements:
|
100
|
-
- - ">="
|
101
|
-
- !ruby/object:Gem::Version
|
102
|
-
version: '0'
|
103
89
|
- !ruby/object:Gem::Dependency
|
104
90
|
name: virtus
|
105
91
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,20 +100,6 @@ dependencies:
|
|
114
100
|
- - ">="
|
115
101
|
- !ruby/object:Gem::Version
|
116
102
|
version: '0'
|
117
|
-
- !ruby/object:Gem::Dependency
|
118
|
-
name: json
|
119
|
-
requirement: !ruby/object:Gem::Requirement
|
120
|
-
requirements:
|
121
|
-
- - ">="
|
122
|
-
- !ruby/object:Gem::Version
|
123
|
-
version: 1.7.7
|
124
|
-
type: :development
|
125
|
-
prerelease: false
|
126
|
-
version_requirements: !ruby/object:Gem::Requirement
|
127
|
-
requirements:
|
128
|
-
- - ">="
|
129
|
-
- !ruby/object:Gem::Version
|
130
|
-
version: 1.7.7
|
131
103
|
- !ruby/object:Gem::Dependency
|
132
104
|
name: ruby-prof
|
133
105
|
requirement: !ruby/object:Gem::Requirement
|