representable 2.4.1 → 3.0.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 (80) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +7 -12
  3. data/CHANGES.md +6 -27
  4. data/README.md +28 -1326
  5. data/lib/representable.rb +4 -14
  6. data/lib/representable/binding.rb +3 -7
  7. data/lib/representable/definition.rb +1 -2
  8. data/lib/representable/populator.rb +35 -0
  9. data/lib/representable/version.rb +1 -1
  10. data/test/definition_test.rb +0 -7
  11. data/test/exec_context_test.rb +2 -2
  12. data/test/instance_test.rb +0 -19
  13. data/test/realistic_benchmark.rb +0 -13
  14. data/test/representable_test.rb +0 -16
  15. data/test/skip_test.rb +1 -1
  16. data/test/test_helper.rb +0 -2
  17. metadata +3 -65
  18. data/lib/representable/deprecations.rb +0 -127
  19. data/lib/representable/parse_strategies.rb +0 -93
  20. data/test-with-deprecations/as_test.rb +0 -65
  21. data/test-with-deprecations/benchmarking.rb +0 -83
  22. data/test-with-deprecations/binding_test.rb +0 -46
  23. data/test-with-deprecations/blaaaaaaaa_test.rb +0 -69
  24. data/test-with-deprecations/cached_test.rb +0 -147
  25. data/test-with-deprecations/class_test.rb +0 -119
  26. data/test-with-deprecations/coercion_test.rb +0 -52
  27. data/test-with-deprecations/config/inherit_test.rb +0 -135
  28. data/test-with-deprecations/config_test.rb +0 -122
  29. data/test-with-deprecations/decorator_scope_test.rb +0 -28
  30. data/test-with-deprecations/decorator_test.rb +0 -96
  31. data/test-with-deprecations/default_test.rb +0 -34
  32. data/test-with-deprecations/defaults_options_test.rb +0 -93
  33. data/test-with-deprecations/definition_test.rb +0 -264
  34. data/test-with-deprecations/example.rb +0 -310
  35. data/test-with-deprecations/examples/object.rb +0 -31
  36. data/test-with-deprecations/exec_context_test.rb +0 -93
  37. data/test-with-deprecations/features_test.rb +0 -70
  38. data/test-with-deprecations/filter_test.rb +0 -57
  39. data/test-with-deprecations/for_collection_test.rb +0 -74
  40. data/test-with-deprecations/generic_test.rb +0 -116
  41. data/test-with-deprecations/getter_setter_test.rb +0 -21
  42. data/test-with-deprecations/hash_bindings_test.rb +0 -87
  43. data/test-with-deprecations/hash_test.rb +0 -160
  44. data/test-with-deprecations/heritage_test.rb +0 -62
  45. data/test-with-deprecations/if_test.rb +0 -79
  46. data/test-with-deprecations/include_exclude_test.rb +0 -88
  47. data/test-with-deprecations/inherit_test.rb +0 -159
  48. data/test-with-deprecations/inline_test.rb +0 -272
  49. data/test-with-deprecations/instance_test.rb +0 -266
  50. data/test-with-deprecations/is_representable_test.rb +0 -77
  51. data/test-with-deprecations/json_test.rb +0 -355
  52. data/test-with-deprecations/lonely_test.rb +0 -239
  53. data/test-with-deprecations/mongoid_test.rb +0 -31
  54. data/test-with-deprecations/nested_test.rb +0 -115
  55. data/test-with-deprecations/object_test.rb +0 -60
  56. data/test-with-deprecations/parse_pipeline_test.rb +0 -64
  57. data/test-with-deprecations/parse_strategy_test.rb +0 -279
  58. data/test-with-deprecations/pass_options_test.rb +0 -27
  59. data/test-with-deprecations/pipeline_test.rb +0 -277
  60. data/test-with-deprecations/populator_test.rb +0 -105
  61. data/test-with-deprecations/prepare_test.rb +0 -67
  62. data/test-with-deprecations/private_options_test.rb +0 -18
  63. data/test-with-deprecations/reader_writer_test.rb +0 -19
  64. data/test-with-deprecations/realistic_benchmark.rb +0 -115
  65. data/test-with-deprecations/render_nil_test.rb +0 -21
  66. data/test-with-deprecations/represent_test.rb +0 -88
  67. data/test-with-deprecations/representable_test.rb +0 -511
  68. data/test-with-deprecations/schema_test.rb +0 -148
  69. data/test-with-deprecations/serialize_deserialize_test.rb +0 -33
  70. data/test-with-deprecations/skip_test.rb +0 -81
  71. data/test-with-deprecations/stringify_hash_test.rb +0 -41
  72. data/test-with-deprecations/test_helper.rb +0 -135
  73. data/test-with-deprecations/test_helper_test.rb +0 -25
  74. data/test-with-deprecations/uncategorized_test.rb +0 -67
  75. data/test-with-deprecations/user_options_test.rb +0 -15
  76. data/test-with-deprecations/wrap_test.rb +0 -152
  77. data/test-with-deprecations/xml_bindings_test.rb +0 -62
  78. data/test-with-deprecations/xml_test.rb +0 -503
  79. data/test-with-deprecations/yaml_test.rb +0 -162
  80. data/test/parse_strategy_test.rb +0 -279
@@ -1,310 +0,0 @@
1
- require 'bundler'
2
- Bundler.setup
3
-
4
- require 'representable/yaml'
5
- require 'ostruct'
6
-
7
- def reset_representer(*module_name)
8
- module_name.each do |mod|
9
- mod.module_eval do
10
- @representable_attrs = nil
11
- end
12
- end
13
- end
14
-
15
- class Song < OpenStruct
16
- end
17
-
18
- song = Song.new(:title => "Fallout", :track => 1)
19
-
20
- require 'representable/json'
21
- module SongRepresenter
22
- include Representable::JSON
23
-
24
- property :title
25
- property :track
26
- end
27
-
28
- puts song.extend(SongRepresenter).to_json
29
-
30
- rox = Song.new.extend(SongRepresenter).from_json(%{ {"title":"Roxanne"} })
31
- puts rox.inspect
32
-
33
- module SongRepresenter
34
- include Representable::JSON
35
-
36
- self.representation_wrap= :hit
37
-
38
- property :title
39
- property :track
40
- end
41
-
42
- puts song.extend(SongRepresenter).to_json
43
-
44
-
45
-
46
- ######### collections
47
-
48
- reset_representer(SongRepresenter)
49
-
50
- module SongRepresenter
51
- include Representable::JSON
52
-
53
- property :title
54
- property :track
55
- collection :composers
56
- end
57
-
58
-
59
- song = Song.new(:title => "Fallout", :composers => ["Stewart Copeland", "Sting"])
60
- puts song.extend(SongRepresenter).to_json
61
-
62
-
63
- ######### nesting types
64
-
65
- class Album < OpenStruct
66
- def name
67
- puts @table.inspect
68
- #@attributes
69
- @table[:name]
70
- end
71
- end
72
-
73
- module AlbumRepresenter
74
- include Representable::JSON
75
-
76
- property :name
77
- property :song, :extend => SongRepresenter, :class => Song
78
- end
79
-
80
- album = Album.new(:name => "The Police", :song => song)
81
- puts album.extend(AlbumRepresenter).to_json
82
-
83
- reset_representer(AlbumRepresenter)
84
-
85
- module AlbumRepresenter
86
- include Representable::JSON
87
-
88
- property :name
89
- collection :songs, :extend => SongRepresenter, :class => Song
90
- end
91
-
92
- album = Album.new(:name => "The Police", :songs => [song, Song.new(:title => "Synchronicity")])
93
- puts album.extend(AlbumRepresenter).to_json
94
-
95
-
96
- album = Album.new.extend(AlbumRepresenter).from_json(%{{"name":"Offspring","songs":[{"title":"Genocide"},{"title":"Nitro","composers":["Offspring"]}]}
97
- })
98
- puts album.inspect
99
-
100
- reset_representer(SongRepresenter)
101
-
102
- ######### using helpers (customizing the rendering/parsing)
103
- module AlbumRepresenter
104
- include Representable::JSON
105
-
106
- def name
107
- super.upcase
108
- end
109
- end
110
-
111
- puts Album.new(:name => "The Police").
112
- extend(AlbumRepresenter).to_json
113
-
114
- reset_representer(SongRepresenter)
115
-
116
-
117
- ######### inheritance
118
- module SongRepresenter
119
- include Representable::JSON
120
-
121
- property :title
122
- property :track
123
- end
124
-
125
- module CoverSongRepresenter
126
- include Representable::JSON
127
- include SongRepresenter
128
-
129
- property :covered_by
130
- end
131
-
132
-
133
- song = Song.new(:title => "Truth Hits Everybody", :covered_by => "No Use For A Name")
134
- puts song.extend(CoverSongRepresenter).to_json
135
-
136
-
137
- ### XML
138
- require 'representable/xml'
139
- module SongRepresenter
140
- include Representable::XML
141
-
142
- property :title
143
- property :track
144
- collection :composers
145
- end
146
- song = Song.new(:title => "Fallout", :composers => ["Stewart Copeland", "Sting"])
147
- puts song.extend(SongRepresenter).to_xml
148
-
149
- reset_representer(SongRepresenter)
150
-
151
-
152
- ### YAML
153
- require 'representable/yaml'
154
- module SongRepresenter
155
- include Representable::YAML
156
-
157
- property :title
158
- property :track
159
- collection :composers
160
- end
161
- puts song.extend(SongRepresenter).to_yaml
162
-
163
-
164
- SongRepresenter.module_eval do
165
- @representable_attrs = nil
166
- end
167
-
168
-
169
- ### YAML
170
- module SongRepresenter
171
- include Representable::YAML
172
-
173
- property :title
174
- property :track
175
- collection :composers, :style => :flow
176
- end
177
- puts song.extend(SongRepresenter).to_yaml
178
-
179
- SongRepresenter.module_eval do
180
- @representable_attrs = nil
181
- end
182
-
183
- # R/W support
184
- song = Song.new(:title => "You're Wrong", :track => 4)
185
- module SongRepresenter
186
- include Representable::Hash
187
-
188
- property :title, :readable => false
189
- property :track
190
- end
191
- puts song.extend(SongRepresenter).to_hash
192
-
193
- SongRepresenter.module_eval do
194
- @representable_attrs = nil
195
- end
196
-
197
- module SongRepresenter
198
- include Representable::Hash
199
-
200
- property :title, :writeable => false
201
- property :track
202
- end
203
- song = Song.new.extend(SongRepresenter)
204
- song.from_hash({:title => "Fallout", :track => 1})
205
- puts song
206
-
207
- ######### custom methods in representer (using helpers)
208
- ######### conditions
209
- ######### :as
210
- ######### XML::Collection
211
- ######### polymorphic :extend and :class, instance context!, :instance
212
- class CoverSong < Song
213
- end
214
-
215
- songs = [Song.new(title: "Weirdo", track: 5), CoverSong.new(title: "Truth Hits Everybody", track: 6, copyright: "The Police")]
216
- album = Album.new(name: "Incognito", songs: songs)
217
-
218
-
219
- reset_representer(SongRepresenter, AlbumRepresenter)
220
-
221
- module SongRepresenter
222
- include Representable::Hash
223
-
224
- property :title
225
- property :track
226
- end
227
-
228
- module CoverSongRepresenter
229
- include Representable::Hash
230
- include SongRepresenter
231
-
232
- property :copyright
233
- end
234
-
235
- module AlbumRepresenter
236
- include Representable::Hash
237
-
238
- property :name
239
- collection :songs, :extend => lambda { |song| song.is_a?(CoverSong) ? CoverSongRepresenter : SongRepresenter }
240
- end
241
-
242
- puts album.extend(AlbumRepresenter).to_hash
243
-
244
- reset_representer(AlbumRepresenter)
245
-
246
- module AlbumRepresenter
247
- include Representable::Hash
248
-
249
- property :name
250
- collection :songs,
251
- :extend => lambda { |song| song.is_a?(CoverSong) ? CoverSongRepresenter : SongRepresenter },
252
- :class => lambda { |hsh| hsh.has_key?("copyright") ? CoverSong : Song } #=> {"title"=>"Weirdo", "track"=>5}
253
- end
254
-
255
- album = Album.new.extend(AlbumRepresenter).from_hash({"name"=>"Incognito", "songs"=>[{"title"=>"Weirdo", "track"=>5}, {"title"=>"Truth Hits Everybody", "track"=>6, "copyright"=>"The Police"}]})
256
- puts album.inspect
257
-
258
- reset_representer(AlbumRepresenter)
259
-
260
- module AlbumRepresenter
261
- include Representable::Hash
262
-
263
- property :name
264
- collection :songs,
265
- :extend => lambda { |song| song.is_a?(CoverSong) ? CoverSongRepresenter : SongRepresenter },
266
- :instance => lambda { |hsh| hsh.has_key?("copyright") ? CoverSong.new : Song.new(original: true) }
267
- end
268
-
269
- album = Album.new.extend(AlbumRepresenter).from_hash({"name"=>"Incognito", "songs"=>[{"title"=>"Weirdo", "track"=>5}, {"title"=>"Truth Hits Everybody", "track"=>6, "copyright"=>"The Police"}]})
270
- puts album.inspect
271
-
272
- ######### #hash
273
-
274
- reset_representer(SongRepresenter)
275
-
276
- module SongRepresenter
277
- include Representable::JSON
278
-
279
- property :title
280
- hash :ratings
281
- end
282
-
283
- puts Song.new(title: "Bliss", ratings: {"Rolling Stone" => 4.9, "FryZine" => 4.5}).extend(SongRepresenter).to_json
284
-
285
- ######### JSON::Hash
286
-
287
- module FavoriteSongsRepresenter
288
- include Representable::JSON::Hash
289
- end
290
-
291
- puts( {"Nick" => "Hyper Music", "El" => "Blown In The Wind"}.extend(FavoriteSongsRepresenter).to_json)
292
-
293
- require 'representable/json/hash'
294
- module FavoriteSongsRepresenter
295
- include Representable::JSON::Hash
296
-
297
- values extend: SongRepresenter, class: Song
298
- end
299
-
300
- puts( {"Nick" => Song.new(title: "Hyper Music")}.extend(FavoriteSongsRepresenter).to_json)
301
-
302
-
303
- require 'representable/json/collection'
304
- module SongsRepresenter
305
- include Representable::JSON::Collection
306
-
307
- items extend: SongRepresenter, class: Song
308
- end
309
-
310
- puts [Song.new(title: "Hyper Music"), Song.new(title: "Screenager")].extend(SongsRepresenter).to_json
@@ -1,31 +0,0 @@
1
- require "test_helper"
2
-
3
- require "ostruct"
4
- require "pp"
5
-
6
-
7
- source = OpenStruct.new(name: "30 Years Live", songs: [
8
- OpenStruct.new(id: 1, title: "Dear Beloved"), OpenStruct.new(id: 2, title: "Fuck Armageddon")])
9
-
10
- pp source
11
-
12
- require "representable/object"
13
-
14
- class AlbumRepresenter < Representable::Decorator
15
- include Representable::Object
16
-
17
- property :name
18
- collection :songs, instance: ->(fragment, *) { Song.new } do
19
- property :title
20
- end
21
- end
22
-
23
-
24
- Album = Struct.new(:name, :songs)
25
- Song = Struct.new(:title)
26
-
27
- target = Album.new
28
-
29
- AlbumRepresenter.new(target).from_object(source)
30
-
31
- pp target
@@ -1,93 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ExecContextTest < MiniTest::Spec
4
- for_formats(
5
- :hash => [Representable::Hash, {Song => "Rebel Fate"}, {Song=>"Timing"}],
6
- # :xml => [Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>", "<open_struct><song><name>You've Taken Everything</name></song>/open_struct>"],
7
- # :yaml => [Representable::YAML, "---\nsong:\n name: Alive\n", "---\nsong:\n name: You've Taken Everything\n"],
8
- ) do |format, mod, input, output|
9
-
10
- let (:song) { representer.prepare(Song.new("Timing")) }
11
- let (:format) { format }
12
-
13
-
14
- describe "exec_context: nil" do
15
- representer!(:module => mod) do
16
- property :name, :as => lambda { |*| self.class }
17
- end
18
-
19
- it { render(song).must_equal_document output }
20
- it { parse(song, input).name.must_equal "Rebel Fate" }
21
- end
22
-
23
-
24
- describe "exec_context: :decorator" do
25
- representer!(:module => mod) do
26
- property :name, :as => lambda { |*| self.class }, :exec_context => :decorator
27
- end
28
-
29
- it { render(song).must_equal_document output }
30
- it { parse(song, input).name.must_equal "Rebel Fate" }
31
- end
32
-
33
-
34
- describe "exec_context: :binding" do
35
- representer!(:module => mod) do
36
- property :name,
37
- :as => lambda { |*| self.class }, # to actually test
38
- :exec_context => :binding,
39
- :setter => lambda { |v, *args| represented.name = v # to make parsing work.
40
- }
41
- end
42
-
43
- it { render(song).must_equal_document({Representable::Hash::Binding => "name"}) }
44
- it { parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name.must_equal "Rebel Fate" }
45
- end
46
-
47
-
48
- describe "Decorator" do
49
- # DISCUSS: do we need this test?
50
- describe "exec_context: nil" do
51
- representer!(:module => mod, :decorator => true) do
52
- property :name, :as => lambda { |*| self.class }
53
- end
54
-
55
- it { render(song).must_equal_document output }
56
- it { parse(song, input).name.must_equal "Rebel Fate" }
57
- end
58
-
59
-
60
- describe "exec_context: :decorator" do # this tests if lambdas are run in the right context, if methods are called in the right context and if we can access the represented object.
61
- representer!(:module => mod, :decorator => true) do
62
- property :name, :as => lambda { |*| self.class.superclass }, :exec_context => :decorator
63
-
64
- define_method :name do # def in Decorator class.
65
- "Timebomb"
66
- end
67
-
68
- define_method :"name=" do |v| # def in Decorator class.
69
- represented.name = v
70
- end
71
- end
72
-
73
- it { render(song).must_equal_document({Representable::Decorator=>"Timebomb"}) }
74
- it { parse(song, {Representable::Decorator=>"Listless"}).name.must_equal "Listless" }
75
- end
76
-
77
-
78
- # DISCUSS: do we need this test?
79
- describe "exec_context: :binding" do
80
- representer!(:module => mod, :decorator => true) do
81
- property :name,
82
- :as => lambda { |*| self.class }, # to actually test
83
- :exec_context => :binding,
84
- :setter => lambda { |v, *args| represented.name = v # to make parsing work.
85
- }
86
- end
87
-
88
- it { render(song).must_equal_document({Representable::Hash::Binding => "name"}) }
89
- it("xxx") { parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name.must_equal "Rebel Fate" }
90
- end
91
- end
92
- end
93
- end
@@ -1,70 +0,0 @@
1
- require 'test_helper'
2
-
3
- class FeaturesTest < MiniTest::Spec
4
- module Title
5
- def title; "Is It A Lie"; end
6
- end
7
- module Length
8
- def length; "2:31"; end
9
- end
10
-
11
- definition = lambda {
12
- feature Title
13
- feature Length
14
-
15
- # exec_context: :decorator, so the readers are called on the Decorator instance (which gets readers from features!).
16
- property :title, exec_context: :decorator
17
- property :length, exec_context: :decorator
18
- property :details do
19
- property :title, exec_context: :decorator
20
- end
21
- }
22
-
23
- let (:song) { OpenStruct.new(:details => Object.new) }
24
-
25
- describe "Module" do
26
- representer! do
27
- instance_exec &definition
28
- end
29
-
30
- it { song.extend(representer).to_hash.must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
31
- end
32
-
33
-
34
- describe "Decorator" do
35
- representer!(:decorator => true) do
36
- instance_exec &definition
37
- end
38
-
39
- it { representer.new(song).to_hash.must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
40
- end
41
- end
42
-
43
- class FeatureInclusionOrderTest < MiniTest::Spec
44
- module Title
45
- def title
46
- "I was first!"
47
- end
48
- end
49
-
50
- module OverridingTitle
51
- def title
52
- "I am number two, " + super
53
- end
54
- end
55
-
56
- representer!(decorator: true) do
57
- feature Title
58
- feature OverridingTitle
59
-
60
- property :title, exec_context: :decorator
61
-
62
- property :song do
63
- property :title, exec_context: :decorator
64
- end
65
- end
66
-
67
- it do
68
- representer.new(OpenStruct.new(song: Object)).to_hash.must_equal({"title"=>"I am number two, I was first!", "song"=>{"title"=>"I am number two, I was first!"}})
69
- end
70
- end