representable 3.3.0 → 3.4.0

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.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +10 -0
  3. data/Gemfile +0 -1
  4. data/README.md +1 -7
  5. data/lib/representable/json.rb +11 -14
  6. data/lib/representable/version.rb +1 -1
  7. data/representable.gemspec +1 -2
  8. metadata +3 -65
  9. data/test/as_test.rb +0 -60
  10. data/test/benchmarking.rb +0 -78
  11. data/test/binding_test.rb +0 -54
  12. data/test/cached_test.rb +0 -177
  13. data/test/class_test.rb +0 -132
  14. data/test/coercion_test.rb +0 -73
  15. data/test/config/inherit_test.rb +0 -130
  16. data/test/config_test.rb +0 -120
  17. data/test/decorator_scope_test.rb +0 -30
  18. data/test/decorator_test.rb +0 -109
  19. data/test/default_test.rb +0 -34
  20. data/test/defaults_options_test.rb +0 -121
  21. data/test/definition_test.rb +0 -253
  22. data/test/examples/example.rb +0 -314
  23. data/test/examples/object.rb +0 -28
  24. data/test/exec_context_test.rb +0 -88
  25. data/test/features_test.rb +0 -89
  26. data/test/filter_test.rb +0 -66
  27. data/test/for_collection_test.rb +0 -71
  28. data/test/generic_test.rb +0 -109
  29. data/test/getter_setter_test.rb +0 -26
  30. data/test/hash_bindings_test.rb +0 -96
  31. data/test/hash_test.rb +0 -197
  32. data/test/heritage_test.rb +0 -64
  33. data/test/if_test.rb +0 -83
  34. data/test/include_exclude_test.rb +0 -96
  35. data/test/inherit_test.rb +0 -246
  36. data/test/inline_test.rb +0 -281
  37. data/test/instance_test.rb +0 -289
  38. data/test/is_representable_test.rb +0 -79
  39. data/test/json_test.rb +0 -379
  40. data/test/lonely_test.rb +0 -241
  41. data/test/nested_test.rb +0 -112
  42. data/test/object_test.rb +0 -60
  43. data/test/option_test.rb +0 -38
  44. data/test/parse_pipeline_test.rb +0 -68
  45. data/test/pipeline_test.rb +0 -309
  46. data/test/populator_test.rb +0 -108
  47. data/test/prepare_test.rb +0 -64
  48. data/test/private_options_test.rb +0 -19
  49. data/test/reader_writer_test.rb +0 -19
  50. data/test/realistic_benchmark.rb +0 -96
  51. data/test/render_nil_test.rb +0 -21
  52. data/test/represent_test.rb +0 -84
  53. data/test/representable_test.rb +0 -526
  54. data/test/schema_test.rb +0 -175
  55. data/test/serialize_deserialize_test.rb +0 -33
  56. data/test/skip_test.rb +0 -92
  57. data/test/stringify_hash_test.rb +0 -39
  58. data/test/test_helper.rb +0 -153
  59. data/test/test_helper_test.rb +0 -25
  60. data/test/uncategorized_test.rb +0 -72
  61. data/test/user_options_test.rb +0 -24
  62. data/test/wrap_test.rb +0 -162
  63. data/test/xml_assertions.rb +0 -29
  64. data/test/xml_bindings_test.rb +0 -55
  65. data/test/xml_namespace_test.rb +0 -193
  66. data/test/xml_test.rb +0 -584
  67. data/test/yaml_test.rb +0 -209
data/test/class_test.rb DELETED
@@ -1,132 +0,0 @@
1
- require "test_helper"
2
-
3
- class ClassTest < BaseTest
4
- class RepresentingSong
5
- attr_reader :name
6
-
7
- def from_hash(doc, *)
8
- @name = doc["__name__"]
9
-
10
- self # DISCUSS: do we wanna be able to return whatever we want here? this is a trick to replace the actual object
11
- end
12
- end
13
-
14
- describe "class: ClassName, only" do
15
- representer! do
16
- property :song, :class => RepresentingSong # supposed this class exposes #from_hash itself.
17
- end
18
-
19
- it "creates fresh instance and doesn't extend" do
20
- song = representer.prepare(OpenStruct.new).from_hash({"song" => {"__name__" => "Captured"}}).song
21
- _(song).must_be_instance_of RepresentingSong
22
- _(song.name).must_equal "Captured"
23
- end
24
- end
25
-
26
- describe "class: lambda, only" do
27
- representer! do
28
- property :song, :class => ->(*) { RepresentingSong }
29
- end
30
-
31
- it "creates fresh instance and doesn't extend" do
32
- song = representer.prepare(OpenStruct.new).from_hash({"song" => {"__name__" => "Captured"}}).song
33
- _(song).must_be_instance_of RepresentingSong
34
- _(song.name).must_equal "Captured"
35
- end
36
- end
37
-
38
- # this throws a DeserializationError now.
39
- describe "lambda { nil }" do
40
- representer! do
41
- property :title, :class => nil
42
- end
43
-
44
- it do
45
- assert_raises Representable::DeserializeError do
46
- OpenStruct.new.extend(representer).from_hash({"title" => {}})
47
- end
48
- end
49
- end
50
-
51
- describe "lambda receiving fragment and args" do
52
- let(:klass) do
53
- Class.new do
54
- class << self
55
- attr_accessor :args
56
- end
57
-
58
- def from_hash(*)
59
- self.class.new
60
- end
61
- end
62
- end
63
-
64
- representer!(:inject => :klass) do
65
- _klass = klass
66
- property :song, :class => ->(options) { _klass.args = ([options[:fragment], options[:user_options]]); _klass }
67
- end
68
-
69
- it {
70
- _(
71
- representer.prepare(OpenStruct.new).from_hash(
72
- {"song" => {"name" => "Captured"}},
73
- user_options: {volume: true}
74
- ).song.class.args
75
- )
76
- .must_equal([{"name"=>"Captured"}, {:volume=>true}])
77
- }
78
- end
79
-
80
- describe "collection: lambda receiving fragment and args" do
81
- let(:klass) do
82
- Class.new do
83
- class << self
84
- attr_accessor :args
85
- end
86
-
87
- def from_hash(*)
88
- self.class.new
89
- end
90
- end
91
- end
92
-
93
- representer!(:inject => :klass) do
94
- _klass = klass
95
- collection :songs, class: ->(options) {
96
- _klass.args = ([options[:fragment], options[:index], options[:user_options]]); _klass
97
- }
98
- end
99
-
100
- it {
101
- _(
102
- representer.prepare(OpenStruct.new).from_hash(
103
- {"songs" => [{"name" => "Captured"}]},
104
- user_options: {volume: true}
105
- ).songs.first.class.args
106
- )
107
- .must_equal([{"name"=>"Captured"}, 0, {:volume=>true}])
108
- }
109
- end
110
-
111
- describe "class: implementing #from_hash" do
112
- let(:parser) do
113
- Class.new do
114
- def from_hash(*)
115
- [1, 2, 3, 4]
116
- end
117
- end
118
- end
119
-
120
- representer!(:inject => :parser) do
121
- property :song, :class => parser # supposed this class exposes #from_hash itself.
122
- end
123
-
124
- it "allows returning arbitrary objects in #from_hash" do
125
- _(representer.prepare(OpenStruct.new).from_hash({"song" => 1}).song).must_equal [1, 2, 3, 4]
126
- end
127
- end
128
- end
129
-
130
- # TODO: test fragment,
131
-
132
- # `class: Song` only, no :extend.
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
- require "representable/coercion"
5
-
6
- class CoercionTest < Minitest::Spec
7
- representer! do
8
- include Representable::Coercion
9
-
10
- property :title # no coercion.
11
- property :length, type: Representable::Coercion::Types::Params::Float
12
-
13
- property :band, class: OpenStruct do
14
- property :founded, type: Representable::Coercion::Types::Params::Integer
15
- end
16
-
17
- collection :songs, class: OpenStruct do
18
- property :ok, type: Representable::Coercion::Types::Params::Bool
19
- end
20
- end
21
-
22
- let(:album) do
23
- OpenStruct.new(
24
- title: "Dire Straits", length: 41.34,
25
- band: OpenStruct.new(founded: "1977"),
26
- songs: [OpenStruct.new(ok: 1), OpenStruct.new(ok: 0)]
27
- )
28
- end
29
-
30
- it do
31
- _(album.extend(representer).to_hash).must_equal(
32
- {
33
- "title" => "Dire Straits",
34
- "length" => 41.34,
35
- "band" => {"founded" => 1977},
36
- "songs" => [{"ok" => true}, {"ok" => false}]
37
- }
38
- )
39
- end
40
-
41
- it {
42
- album = OpenStruct.new
43
- album.extend(representer)
44
- album.from_hash(
45
- {
46
- "title" => "Dire Straits",
47
- "length" => "41.34",
48
- "band" => {"founded" => "1977"},
49
- "songs" => [{"ok" => 1}, {"ok" => 0}]
50
- }
51
- )
52
-
53
- # it
54
- _(album.length).must_equal 41.34
55
- _(album.band.founded).must_equal 1977
56
- _(album.songs[0].ok).must_equal true
57
- }
58
-
59
- describe "with user :parse_filter and :render_filter" do
60
- representer! do
61
- include Representable::Coercion
62
-
63
- property :length, type: Representable::Coercion::Types::Params::Float,
64
- parse_filter: ->(input, _options) { "#{input}.1" }, # happens BEFORE coercer.
65
- render_filter: ->(fragment, *) { "#{fragment}.1" }
66
- end
67
-
68
- # user's :parse_filter(s) are run before coercion.
69
- it { _(OpenStruct.new.extend(representer).from_hash("length" => "1").length).must_equal 1.1 }
70
- # user's :render_filter(s) are run before coercion.
71
- it { _(OpenStruct.new(length: 1).extend(representer).to_hash).must_equal({"length" => 1.1}) }
72
- end
73
- end
@@ -1,130 +0,0 @@
1
- require "test_helper"
2
-
3
- # tests defining representers in modules, decorators and classes and the inheritance when combined.
4
-
5
- class ConfigInheritTest < Minitest::Spec
6
- def assert_cloned(child, parent, property)
7
- child_def = child.representable_attrs.get(property)
8
- parent_def = parent.representable_attrs.get(property)
9
-
10
- child_def.merge!(:alias => property)
11
-
12
- _(child_def[:alias]).wont_equal parent_def[:alias]
13
- _(child_def.object_id).wont_equal parent_def.object_id
14
- end
15
- # class Object
16
-
17
- # end
18
- module GenreModule
19
- include Representable::Hash
20
- property :genre
21
- end
22
-
23
- # in Decorator ------------------------------------------------
24
- class Decorator < Representable::Decorator
25
- include Representable::Hash
26
- property :title
27
- property :artist do
28
- property :id
29
- end
30
- end
31
-
32
- it { _(Decorator.definitions.keys).must_equal %w[title artist] }
33
-
34
- # in inheriting Decorator
35
-
36
- class InheritingDecorator < Decorator
37
- property :location
38
- end
39
-
40
- it { _(InheritingDecorator.definitions.keys).must_equal %w[title artist location] }
41
- it { assert_cloned(InheritingDecorator, Decorator, "title") }
42
- it do
43
- _(InheritingDecorator.representable_attrs.get(:artist).representer_module.object_id).wont_equal Decorator.representable_attrs.get(:artist).representer_module.object_id
44
- end
45
-
46
- # in inheriting and including Decorator
47
-
48
- class InheritingAndIncludingDecorator < Decorator
49
- include GenreModule
50
- property :location
51
- end
52
-
53
- it { _(InheritingAndIncludingDecorator.definitions.keys).must_equal %w[title artist genre location] }
54
- it { assert_cloned(InheritingAndIncludingDecorator, GenreModule, :genre) }
55
-
56
- # in module ---------------------------------------------------
57
- module Module
58
- include Representable
59
- property :title
60
- end
61
-
62
- it { _(Module.definitions.keys).must_equal ["title"] }
63
-
64
- # in module including module
65
- module SubModule
66
- include Representable
67
- include Module
68
-
69
- property :location
70
- end
71
-
72
- it { _(SubModule.definitions.keys).must_equal %w[title location] }
73
- it { assert_cloned(SubModule, Module, :title) }
74
-
75
- # including preserves order
76
- module IncludingModule
77
- include Representable
78
- property :genre
79
- include Module
80
-
81
- property :location
82
- end
83
-
84
- it { _(IncludingModule.definitions.keys).must_equal %w[genre title location] }
85
-
86
- # included in class -------------------------------------------
87
- class Class
88
- include Representable
89
- include IncludingModule
90
- end
91
-
92
- it { _(Class.definitions.keys).must_equal %w[genre title location] }
93
- it { assert_cloned(Class, IncludingModule, :title) }
94
- it { assert_cloned(Class, IncludingModule, :location) }
95
- it { assert_cloned(Class, IncludingModule, :genre) }
96
-
97
- # included in class with order
98
- class DefiningClass
99
- include Representable
100
- property :street_cred
101
- include IncludingModule
102
- end
103
-
104
- it { _(DefiningClass.definitions.keys).must_equal %w[street_cred genre title location] }
105
-
106
- # in class
107
- class RepresenterClass
108
- include Representable
109
- property :title
110
- end
111
-
112
- it { _(RepresenterClass.definitions.keys).must_equal ["title"] }
113
-
114
- # in inheriting class
115
- class InheritingClass < RepresenterClass
116
- include Representable
117
- property :location
118
- end
119
-
120
- it { _(InheritingClass.definitions.keys).must_equal %w[title location] }
121
- it { assert_cloned(InheritingClass, RepresenterClass, :title) }
122
-
123
- # in inheriting class and including
124
- class InheritingAndIncludingClass < RepresenterClass
125
- property :location
126
- include GenreModule
127
- end
128
-
129
- it { _(InheritingAndIncludingClass.definitions.keys).must_equal %w[title location genre] }
130
- end
data/test/config_test.rb DELETED
@@ -1,120 +0,0 @@
1
- require "test_helper"
2
-
3
- class ConfigTest < Minitest::Spec
4
- subject { Representable::Config.new(Representable::Definition) }
5
- PunkRock = Class.new
6
- Definition = Representable::Definition
7
-
8
- describe "wrapping" do
9
- it "returns false per default" do
10
- assert_nil subject.wrap_for("Punk", nil)
11
- end
12
-
13
- # it "infers a printable class name if set to true" do
14
- # subject.wrap = true
15
- # assert_equal "punk_rock", subject.wrap_for(PunkRock, nil)
16
- # end
17
-
18
- # it "can be set explicitely" do
19
- # subject.wrap = "Descendents"
20
- # assert_equal "Descendents", subject.wrap_for(PunkRock, nil)
21
- # end
22
- end
23
-
24
- describe "#[]" do
25
- # does return nil for non-existent
26
- it { _(subject[:hello]).must_be_nil }
27
- end
28
-
29
- # describe "#[]" do
30
- # before { subject.add(:title, {:me => true}) }
31
-
32
- # it { subject[:unknown].must_be_nil }
33
- # it { subject.get(:title)[:me].must_equal true }
34
- # it { subject["title"][:me].must_equal true }
35
- # end
36
-
37
- # []=
38
- # []=(... inherit: true)
39
- # forwarded to Config#definitions
40
- # that goes to ConfigDefinitionsTest
41
- describe "#add" do
42
- describe "returns" do
43
- it do
44
- # #add returns Definition.`
45
- subject = Representable::Config.new(Representable::Definition).add(:title, {:me => true})
46
-
47
- _(subject).must_be_kind_of Representable::Definition
48
- _(subject[:me]).must_equal true
49
- end
50
- end
51
-
52
- before { subject.add(:title, {:me => true}) }
53
-
54
- # must be kind of Definition
55
- it { _(subject.size).must_equal 1 }
56
- it { _(subject.get(:title).name).must_equal "title" }
57
- it { _(subject.get(:title)[:me]).must_equal true }
58
-
59
- # this is actually tested in context in inherit_test.
60
- it "overrides former definition" do
61
- subject.add(:title, {:peer => Module})
62
- _(subject.get(:title)[:me]).must_be_nil
63
- _(subject.get(:title)[:peer]).must_equal Module
64
- end
65
-
66
- describe "inherit: true" do
67
- before do
68
- subject.add(:title, {:me => true})
69
- subject.add(:title, {:peer => Module, :inherit => true})
70
- end
71
-
72
- it { _(subject.get(:title)[:me]).must_equal true }
73
- it { _(subject.get(:title)[:peer]).must_equal Module }
74
- end
75
- end
76
-
77
- describe "#remove" do
78
- subject { Representable::Config.new(Representable::Definition) }
79
-
80
- it do
81
- subject.add(:title, {:me => true})
82
- subject.add(:genre, {})
83
- _(subject.get(:genre)).must_be_kind_of Representable::Definition
84
-
85
- subject.remove(:genre)
86
- _(subject.get(:genre)).must_be_nil
87
- end
88
- end
89
-
90
- describe "#each" do
91
- before { subject.add(:title, {:me => true}) }
92
-
93
- it "what" do
94
- definitions = []
95
- subject.each { |dfn| definitions << dfn }
96
- _(definitions.size).must_equal 1
97
- _(definitions[0][:me]).must_equal true
98
- end
99
- end
100
-
101
- describe "#options" do
102
- it { _(subject.options).must_equal({}) }
103
- it do
104
- subject.options[:namespacing] = true
105
- _(subject.options[:namespacing]).must_equal true
106
- end
107
- end
108
-
109
- describe "#get" do
110
- subject { Representable::Config.new(Representable::Definition) }
111
-
112
- it do
113
- title = subject.add(:title, {})
114
- length = subject.add(:length, {})
115
-
116
- _(subject.get(:title)).must_equal title
117
- _(subject.get(:length)).must_equal length
118
- end
119
- end
120
- end
@@ -1,30 +0,0 @@
1
- require "test_helper"
2
-
3
- # TODO: remove in 2.0.
4
- class DecoratorScopeTest < Minitest::Spec
5
- representer! do
6
- property :title, :getter => ->(*) { title_from_representer }, :decorator_scope => true
7
- end
8
-
9
- let(:representer_with_method) do
10
- Module.new do
11
- include Representable::Hash
12
- property :title, :decorator_scope => true
13
- def title; "Crystal Planet"; end
14
- end
15
- end
16
-
17
- it "executes lambdas in represented context" do
18
- _(
19
- Class.new {
20
- def title_from_representer
21
- "Sounds Of Silence"
22
- end
23
- }.new.extend(representer).to_hash
24
- ).must_equal({"title"=>"Sounds Of Silence"})
25
- end
26
-
27
- it "executes method in represented context" do
28
- _(Object.new.extend(representer_with_method).to_hash).must_equal({"title"=>"Crystal Planet"})
29
- end
30
- end
@@ -1,109 +0,0 @@
1
- require "test_helper"
2
-
3
- class DecoratorTest < Minitest::Spec
4
- class SongRepresentation < Representable::Decorator
5
- include Representable::JSON
6
- property :name
7
- end
8
-
9
- class AlbumRepresentation < Representable::Decorator
10
- include Representable::JSON
11
-
12
- collection :songs, :class => Song, :extend => SongRepresentation
13
- end
14
-
15
- class RatingRepresentation < Representable::Decorator
16
- include Representable::JSON
17
-
18
- property :system
19
- property :value
20
- end
21
-
22
- let(:song) { Song.new("Mama, I'm Coming Home") }
23
- let(:album) { Album.new([song]) }
24
-
25
- let(:rating) { OpenStruct.new(system: "MPAA", value: "R") }
26
-
27
- describe "inheritance" do
28
- let(:inherited_decorator) do
29
- Class.new(AlbumRepresentation) {
30
- property :best_song
31
- }.new(Album.new([song], "Stand Up"))
32
- end
33
-
34
- it { _(inherited_decorator.to_hash).must_equal({"songs" => [{"name"=>"Mama, I'm Coming Home"}], "best_song" => "Stand Up"}) }
35
- end
36
-
37
- let(:decorator) { AlbumRepresentation.new(album) }
38
-
39
- let(:rating_decorator) { RatingRepresentation.new(rating) }
40
-
41
- it "renders" do
42
- _(decorator.to_hash).must_equal({"songs"=>[{"name"=>"Mama, I'm Coming Home"}]})
43
- _(album).wont_respond_to :to_hash
44
- _(song).wont_respond_to :to_hash # DISCUSS: weak test, how to assert blank slate?
45
- # no @representable_attrs in decorated objects
46
- _(song).wont_be(:instance_variable_defined?, :@representable_attrs)
47
-
48
- _(rating_decorator.to_hash).must_equal({"system" => "MPAA", "value" => "R"})
49
- end
50
-
51
- describe "#from_hash" do
52
- it "returns represented" do
53
- _(decorator.from_hash({"songs"=>[{"name"=>"Mama, I'm Coming Home"}]})).must_equal album
54
- end
55
-
56
- it "parses" do
57
- decorator.from_hash({"songs"=>[{"name"=>"Atomic Garden"}]})
58
- _(album.songs.first).must_be_kind_of Song
59
- _(album.songs).must_equal [Song.new("Atomic Garden")]
60
- _(album).wont_respond_to :to_hash
61
- _(song).wont_respond_to :to_hash # DISCUSS: weak test, how to assert blank slate?
62
- end
63
- end
64
-
65
- describe "#decorated" do
66
- it "is aliased to #represented" do
67
- _(AlbumRepresentation.prepare(album).decorated).must_equal album
68
- end
69
- end
70
-
71
- describe "inline decorators" do
72
- representer!(decorator: true) do
73
- collection :songs, :class => Song do
74
- property :name
75
- end
76
- end
77
-
78
- it "does not pollute represented" do
79
- representer.new(album).from_hash({"songs"=>[{"name"=>"Atomic Garden"}]})
80
-
81
- # no @representable_attrs in decorated objects
82
- _(song).wont_be(:instance_variable_defined?, :@representable_attrs)
83
- _(album).wont_be(:instance_variable_defined?, :@representable_attrs)
84
- end
85
- end
86
- end
87
-
88
- require "uber/inheritable_attr"
89
- class InheritanceWithDecoratorTest < Minitest::Spec
90
- class Twin
91
- extend Uber::InheritableAttr
92
- inheritable_attr :representer_class
93
- self.representer_class = Class.new(Representable::Decorator) { include Representable::Hash }
94
- end
95
-
96
- class Album < Twin
97
- representer_class.property :title # Twin.representer_class.clone
98
- end
99
-
100
- # Twin.representer_class.clone
101
- class Song < Twin
102
- end
103
-
104
- it do
105
- _(Twin.representer_class.definitions.size).must_equal 0
106
- _(Album.representer_class.definitions.size).must_equal 1
107
- _(Song.representer_class.definitions.size).must_equal 0
108
- end
109
- end
data/test/default_test.rb DELETED
@@ -1,34 +0,0 @@
1
- require "test_helper"
2
-
3
- class DefaultTest < Minitest::Spec
4
- Song = Struct.new(:id, :title)
5
-
6
- representer! do
7
- property :id
8
- property :title, default: "Huber Breeze" #->(options) { options[:default] }
9
- end
10
-
11
- describe "#from_hash" do
12
- let(:song) { Song.new.extend(representer) }
13
-
14
- it { _(song.from_hash({})).must_equal Song.new(nil, "Huber Breeze") }
15
- # default doesn't apply when empty string.
16
- it { _(song.from_hash({"title"=>""})).must_equal Song.new(nil, "") }
17
- it { _(song.from_hash({"title"=>nil})).must_equal Song.new(nil, nil) }
18
- it { _(song.from_hash({"title"=>"Blindfold"})).must_equal Song.new(nil, "Blindfold") }
19
- end
20
-
21
- describe "#to_json" do
22
- it "uses :default when not available from object" do
23
- _(Song.new.extend(representer).to_hash).must_equal({"title"=>"Huber Breeze"})
24
- end
25
-
26
- it "uses value from represented object when present" do
27
- _(Song.new(nil, "After The War").extend(representer).to_hash).must_equal({"title"=>"After The War"})
28
- end
29
-
30
- it "uses value from represented object when emtpy string" do
31
- _(Song.new(nil, "").extend(representer).to_hash).must_equal({"title"=>""})
32
- end
33
- end
34
- end