representable 0.12.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,11 @@
1
+ h2. 0.13.0
2
+
3
+ * We no longer create accessors in @Representable.property@ - you have to do this yourself using @attr_accessors@.
4
+
1
5
  h2. 0.12.0
2
6
 
3
7
  * @:as@ is now @:class@.
4
8
 
5
-
6
9
  h2. 0.11.0
7
10
 
8
11
  * Representer modules can now be injected into objects using @#extend@.
@@ -37,11 +37,13 @@ Representations are usually defined using a module. This makes them super flexib
37
37
  property :surename
38
38
  end
39
39
 
40
- By using #property we declare two simple attributes. Representable will automatically add accessors to the module.
40
+ By using #property we declare two simple attributes that should be considered when representing.
41
41
 
42
- To use your representer include it in the matching class. Note that you could reuse a representer in multiple classes.
42
+ To use your representer include it in the matching class. Note that you could reuse a representer in multiple classes. The represented class must have getter and setter methods for each property.
43
43
 
44
44
  class Hero
45
+ attr_accessor :forename, :surename
46
+
45
47
  include Representable
46
48
  include HeroRepresenter
47
49
  end
@@ -53,6 +55,8 @@ Many people dislike including representers on class layer. You might also extend
53
55
  Alternatively, if you don't like modules (which you shouldn't), declarations can be put into classes directly.
54
56
 
55
57
  class Hero
58
+ attr_accessor :forename, :surename
59
+
56
60
  include Representable::JSON
57
61
 
58
62
  property :forename
@@ -87,6 +91,8 @@ See how easy this is? You can use an object-oriented method to read from the doc
87
91
  You need a second domain object. Every hero has a place it comes from.
88
92
 
89
93
  class Location
94
+ attr_accessor :title
95
+
90
96
  include Representable::JSON
91
97
 
92
98
  property :title
@@ -254,7 +260,7 @@ Representable was written with REST representations in mind. However, it is a ge
254
260
 
255
261
  == Copyright
256
262
 
257
- Representable is a heavily simplified fork of the ROXML gem. Big thanks to Ben Woosley for his work.
263
+ Representable is a heavily simplified fork of the ROXML gem. Big thanks to Ben Woosley for his inspiring work.
258
264
 
259
265
  * Copyright (c) 2011 Nick Sutterer <apotonick@gmail.com>
260
- * Copyright (c) 2004-2009 Ben Woosley, Zak Mandhro and Anders Engstrom.
266
+ * ROXML is Copyright (c) 2004-2009 Ben Woosley, Zak Mandhro and Anders Engstrom.
@@ -85,13 +85,9 @@ private
85
85
  # property :name
86
86
  # property :name, :from => :title
87
87
  # property :name, :class => Name
88
- # property :name, :accessors => false
89
88
  # property :name, :default => "Mike"
90
89
  def property(name, options={})
91
- attr = add_property(name, options)
92
-
93
- attr_reader(attr.getter) unless options[:accessors] == false
94
- attr_writer(attr.getter) unless options[:accessors] == false
90
+ representable_attrs << definition_class.new(name, options)
95
91
  end
96
92
 
97
93
  # Declares a represented document node collection.
@@ -105,13 +101,6 @@ private
105
101
  options[:collection] = true
106
102
  property(name, options)
107
103
  end
108
-
109
- private
110
- def add_property(*args)
111
- definition_class.new(*args).tap do |attr|
112
- representable_attrs << attr
113
- end
114
- end
115
104
  end
116
105
 
117
106
 
@@ -8,7 +8,7 @@ module Representable
8
8
  @name = sym.to_s
9
9
  @array = options[:collection]
10
10
  @from = (options[:from] || name).to_s
11
- @sought_type = options[:class] || options[:class] || :text # TODO: deprecate :class in 1.0.
11
+ @sought_type = options[:class] || options[:as] || :text # TODO: deprecate :as in 1.0.
12
12
  @default = options[:default]
13
13
  @default ||= [] if array?
14
14
  @representer_module = options[:extend] # DISCUSS: move to Representable::DCI?
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "0.12.0"
2
+ VERSION = "0.13.0"
3
3
  end
@@ -12,6 +12,7 @@ module JsonTest
12
12
  include Representable::JSON
13
13
  property :name
14
14
  property :label
15
+ attr_accessor :name, :label
15
16
 
16
17
  def initialize(name=nil)
17
18
  self.name = name if name
@@ -167,6 +168,9 @@ module JsonTest
167
168
  civ = Object.new
168
169
  civ.instance_eval do
169
170
  def name; "CIV"; end
171
+ def name=(v)
172
+ @name = v
173
+ end
170
174
  end
171
175
 
172
176
  civ.extend(BandRepresenter)
@@ -196,6 +200,7 @@ module JsonTest
196
200
  class Band
197
201
  include Representable::JSON
198
202
  property :name
203
+ attr_accessor :name
199
204
  end
200
205
 
201
206
  it "#from_json creates correct accessors" do
@@ -215,11 +220,13 @@ module JsonTest
215
220
  class Label
216
221
  include Representable::JSON
217
222
  property :name
223
+ attr_accessor :name
218
224
  end
219
225
 
220
226
  class Album
221
227
  include Representable::JSON
222
228
  property :label, :class => Label
229
+ attr_accessor :label
223
230
  end
224
231
 
225
232
  it "#from_json creates one Item instance" do
@@ -239,6 +246,7 @@ module JsonTest
239
246
  @Album = Class.new do
240
247
  include Representable::JSON
241
248
  property :seller, :class => Label
249
+ attr_accessor :seller
242
250
  end
243
251
  end
244
252
 
@@ -255,6 +263,7 @@ module JsonTest
255
263
  class Song
256
264
  include Representable::JSON
257
265
  property :name, :from => :songName
266
+ attr_accessor :name
258
267
  end
259
268
 
260
269
  it "respects :from in #from_json" do
@@ -273,6 +282,7 @@ module JsonTest
273
282
  @Album = Class.new do
274
283
  include Representable::JSON
275
284
  property :name, :default => "30 Years Live"
285
+ attr_accessor :name
276
286
  end
277
287
  end
278
288
 
@@ -319,6 +329,7 @@ end
319
329
  class CD
320
330
  include Representable::JSON
321
331
  collection :songs
332
+ attr_accessor :songs
322
333
  end
323
334
 
324
335
  it "#from_json creates correct accessors" do
@@ -338,6 +349,7 @@ end
338
349
  class Band
339
350
  include Representable::JSON
340
351
  property :name
352
+ attr_accessor :name
341
353
 
342
354
  def initialize(name="")
343
355
  self.name = name
@@ -347,6 +359,7 @@ end
347
359
  class Compilation
348
360
  include Representable::JSON
349
361
  collection :bands, :class => Band
362
+ attr_accessor :bands
350
363
  end
351
364
 
352
365
  describe "#from_json" do
@@ -376,6 +389,7 @@ end
376
389
  class Songs
377
390
  include Representable::JSON
378
391
  collection :tracks, :from => :songList
392
+ attr_accessor :tracks
379
393
  end
380
394
 
381
395
  it "respects :from in #from_json" do
@@ -4,10 +4,12 @@ class RepresentableTest < MiniTest::Spec
4
4
  class Band
5
5
  include Representable
6
6
  property :name
7
+ attr_accessor :name
7
8
  end
8
9
 
9
10
  class PunkBand < Band
10
11
  property :street_cred
12
+ attr_accessor :street_cred
11
13
  end
12
14
 
13
15
  module BandRepresentation
@@ -56,6 +58,7 @@ class RepresentableTest < MiniTest::Spec
56
58
  it "allows including the concrete representer module later" do
57
59
  require 'representable/json'
58
60
  vd = class VD
61
+ attr_accessor :name, :street_cred
59
62
  include Representable::JSON
60
63
  include PunkBandRepresentation
61
64
  end.new
@@ -80,6 +83,45 @@ class RepresentableTest < MiniTest::Spec
80
83
  end
81
84
 
82
85
 
86
+ describe "Representable" do
87
+ it "allows mixing in multiple representers" do
88
+ require 'representable/json'
89
+ require 'representable/xml'
90
+ class Bodyjar
91
+ include Representable::XML
92
+ include Representable::JSON
93
+ include PunkBandRepresentation
94
+
95
+ self.representation_wrap = "band"
96
+ attr_accessor :name, :street_cred
97
+ end
98
+
99
+ band = Bodyjar.new
100
+ band.name = "Bodyjar"
101
+
102
+ assert_equal "{\"band\":{\"name\":\"Bodyjar\"}}", band.to_json
103
+ #assert_equal "{\"name\":\"Bodyjar\"}", band.to_xml # FIXME: now, #binding_for_definition returns wrong binding.
104
+ end
105
+
106
+ it "allows extending with different representers subsequentially" do
107
+ module SongXmlRepresenter
108
+ include Representable::XML
109
+ property :name, :from => "@name"
110
+ end
111
+
112
+ module SongJsonRepresenter
113
+ include Representable::JSON
114
+ property :name
115
+ end
116
+
117
+ @song = Song.new("Days Go By")
118
+ assert_xml_equal "<song name=\"Days Go By\"/>", @song.extend(SongXmlRepresenter).to_xml
119
+ assert_equal "{\"name\":\"Days Go By\"}", @song.extend(SongJsonRepresenter).to_json
120
+ end
121
+ end
122
+
123
+
124
+
83
125
  describe "#property" do
84
126
  it "creates accessors for the attribute" do
85
127
  @band = PunkBand.new
@@ -163,6 +205,7 @@ class RepresentableTest < MiniTest::Spec
163
205
  include Representable::JSON
164
206
  property :name
165
207
  property :groupies
208
+ attr_accessor :name, :groupies
166
209
  end
167
210
 
168
211
  describe "#update_properties_from" do
@@ -10,6 +10,7 @@ require 'test_xml/mini_test'
10
10
  require 'mocha'
11
11
 
12
12
  class Album
13
+ attr_accessor :songs, :best_song
13
14
  def initialize(*songs)
14
15
  @songs = songs
15
16
  @best_song = songs.last
@@ -17,6 +18,7 @@ class Album
17
18
  end
18
19
 
19
20
  class Song
21
+ attr_accessor :name
20
22
  def initialize(name=nil)
21
23
  @name = name
22
24
  end
@@ -4,6 +4,7 @@ require 'representable/xml'
4
4
  class Band
5
5
  include Representable::XML
6
6
  property :name
7
+ attr_accessor :name
7
8
 
8
9
  def initialize(name=nil)
9
10
  name and self.name = name
@@ -24,6 +25,7 @@ class XmlTest < MiniTest::Spec
24
25
  self.representation_wrap = :band
25
26
  property :name
26
27
  property :label
28
+ attr_accessor :name, :label
27
29
  end
28
30
 
29
31
  @band = @Band.new
@@ -159,6 +161,9 @@ class XmlTest < MiniTest::Spec
159
161
  civ = Object.new
160
162
  civ.instance_eval do
161
163
  def name; "CIV"; end
164
+ def name=(v)
165
+ @name = v
166
+ end
162
167
  end
163
168
 
164
169
  civ.extend(BandRepresenter)
@@ -194,6 +199,7 @@ class AttributesTest < MiniTest::Spec
194
199
  include Representable::XML
195
200
  property :href, :from => "@href"
196
201
  property :title, :from => "@title"
202
+ attr_accessor :href, :title
197
203
  end
198
204
 
199
205
  it "#from_xml creates correct accessors" do
@@ -221,6 +227,7 @@ class TypedPropertyTest < MiniTest::Spec
221
227
 
222
228
 
223
229
  class Album
230
+ attr_accessor :band
224
231
  def initialize(band=nil)
225
232
  @band = band
226
233
  end
@@ -271,6 +278,7 @@ class CollectionTest < MiniTest::Spec
271
278
  class Compilation
272
279
  include Representable::XML
273
280
  collection :bands, :class => Band, :from => :band
281
+ attr_accessor :bands
274
282
  end
275
283
 
276
284
  describe "#from_xml" do
@@ -309,6 +317,7 @@ class CollectionTest < MiniTest::Spec
309
317
  class Album
310
318
  include Representable::XML
311
319
  collection :songs, :from => :song
320
+ attr_accessor :songs
312
321
  end
313
322
 
314
323
  it "collects untyped items" do
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 12
7
+ - 13
8
8
  - 0
9
- version: 0.12.0
9
+ version: 0.13.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nick Sutterer
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-12-19 00:00:00 +01:00
17
+ date: 2011-12-24 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency