representable 3.0.1 → 3.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 81be665d8491efad3044c31114917a6b42e4dc36
4
- data.tar.gz: dd49b35a842ac30a57f40d2b2a63784712c846ba
3
+ metadata.gz: 3ac8f58fd73f1e9aa9e35f3298490086dfb93d54
4
+ data.tar.gz: 054dcc8fdcf9cc7ce6f223747af5914457d19e90
5
5
  SHA512:
6
- metadata.gz: a9dffbcdc7dcac66ee881278b55944ecc3ceda2b8ed5ddabae71bfc748bc59251e954e23bd2508e046a96ecaa448bc4895a19f8908e3bada0f4fef5315336eac
7
- data.tar.gz: 273e167fe08c19f9b069ee9bcf4d9f81165fb64c892cb662e90dbe58e2b714ccfde39cfb2555f2e5520a0ea4e9865e3e8595542133816ef5f6f9a8e37d02c935
6
+ metadata.gz: af903f251a75b3d57899b4a0b428a84ff5436885939a8b59b6fb42b9fa4f517efca96ccb9b01bee969d9602a542b2b58a81171be8f4175204090a0e6c8f292c2
7
+ data.tar.gz: 58b9716576c0d790d1b4584ac480760c9539ca61cea4618574fec0e7cb9f415fe2b35d156839f1b2d1c531433fa652160faea4bb14b6d2299341e62c094ec426
@@ -1,7 +1,10 @@
1
+ language: ruby
2
+ cache: bundler
3
+ sudo: false
1
4
  rvm:
2
5
  - 1.9.3
3
6
  - 2.0.0
4
7
  - 2.1.1
5
- - 2.2.3
8
+ - 2.3.1
6
9
  before_install:
7
- - gem install bundler
10
+ - gem install bundler
data/CHANGES.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 3.0.2
2
+
3
+ * Initialize `Config@wrap` to avoid Ruby's warning.
4
+ * Add `#render` and `#parse` alias methods to all format modules as a generic entry point.
5
+ * In `GetValue`, use `public_send` now.
6
+
1
7
  # 3.0.1
2
8
 
3
9
  * Loosen `uber` dependency.
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
@@ -35,8 +35,6 @@ Representations are defined using representer classes, called _decorator, or mod
35
35
  In these examples, let's use decorators
36
36
 
37
37
  ```ruby
38
- require 'representable/json'
39
-
40
38
  class SongRepresenter < Representable::Decorator
41
39
  include Representable::JSON
42
40
 
@@ -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'
@@ -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: ->(options) { self },
25
- setter: ->(options) { },
26
- instance: ->(options) { self },
24
+ getter: ->(opts) { self },
25
+ setter: ->(opts) { },
26
+ instance: ->(opts) { self },
27
27
  )
28
28
 
29
29
  if block
@@ -39,6 +39,9 @@ module Representable
39
39
  {wrap => hash}
40
40
  end
41
41
 
42
+ alias_method :render, :to_hash
43
+ alias_method :parse, :from_hash
44
+
42
45
  private
43
46
  def filter_wrap(data, options)
44
47
  return data if options[:wrap] == false
@@ -43,5 +43,8 @@ module Representable
43
43
  def to_json(*args)
44
44
  MultiJson.dump to_hash(*args)
45
45
  end
46
+
47
+ alias_method :render, :to_json
48
+ alias_method :parse, :from_json
46
49
  end
47
50
  end
@@ -1,4 +1,3 @@
1
- require 'representable/json'
2
1
  require 'representable/hash_methods'
3
2
 
4
3
  module Representable::JSON
@@ -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, options) do
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).send(options[:binding].getter) }
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)
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "3.0.1"
2
+ VERSION = "3.0.2"
3
3
  end
@@ -56,6 +56,9 @@ module Representable
56
56
  to_node(*args).to_s
57
57
  end
58
58
 
59
+ alias_method :render, :to_xml
60
+ alias_method :parse, :from_xml
61
+
59
62
  private
60
63
  def remove_namespaces?
61
64
  # TODO: make local Config easily extendable so you get Config#remove_ns? etc.
@@ -38,5 +38,8 @@ module Representable
38
38
  doc.children << to_ast(*args)
39
39
  stream.to_yaml
40
40
  end
41
+
42
+ alias_method :render, :to_yaml
43
+ alias_method :parse, :from_yaml
41
44
  end
42
45
  end
@@ -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
@@ -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
@@ -17,7 +17,6 @@ end
17
17
 
18
18
  song = Song.new(:title => "Fallout", :track => 1)
19
19
 
20
- require 'representable/json'
21
20
  module SongRepresenter
22
21
  include Representable::JSON
23
22
 
@@ -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
@@ -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
@@ -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
- describe "Mongoid compatibility" do
7
- it "allows #to_json" do
8
- class Profile
9
- include Mongoid::Document
10
- field :name
11
- end
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
- property :name
22
- end
13
+ # class Dude
14
+ # include Mongoid::Document
15
+ # embeds_one :profile, :class_name => "MongoidTest::Profile"
16
+ # end
23
17
 
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
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
@@ -76,8 +76,6 @@ class RepresentableTest < MiniTest::Spec
76
76
  end
77
77
 
78
78
  it "allows mixing in multiple representers" do
79
- require 'representable/json'
80
- require 'representable/xml'
81
79
  class Bodyjar
82
80
  include Representable::XML
83
81
  include Representable::JSON
@@ -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
@@ -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
@@ -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
- def initialize(name=nil)
10
- name and self.name = name
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
- class Album
15
- attr_accessor :songs
16
- end
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([Song.new("I Hate My Brain"), mr=Song.new("Mr. Charisma")], mr)
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
@@ -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.1
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-09 00:00:00.000000000 Z
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