representable 1.2.6 → 1.2.7

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.textile CHANGED
@@ -1,6 +1,10 @@
1
+ h2. 1.2.7
2
+
3
+ * Moving @Format.binding_for_definition@ to @Format#{format}_binding_for_definition@, making it an instance method in its own "namespace". This allows mixing in multiple representer engines into a user's representer module.
4
+
1
5
  h2. 1.2.6
2
6
 
3
- Extracted @HashRepresenter@ which operates on hash structures. This allows you to "parse" form data, e.g. as in Rails' @params@ hash. Internally, this is used by JSON and partly by YAML.
7
+ * Extracted @HashRepresenter@ which operates on hash structures. This allows you to "parse" form data, e.g. as in Rails' @params@ hash. Internally, this is used by JSON and partly by YAML.
4
8
 
5
9
  h2. 1.2.5
6
10
 
data/lib/representable.rb CHANGED
@@ -111,7 +111,7 @@ private
111
111
  end
112
112
 
113
113
  def representable_bindings_for(format)
114
- representable_attrs.map {|attr| format.binding_for_definition(attr) }
114
+ representable_attrs.map {|attr| send("#{format}_binding_for_definition", attr) } # DISCUSS: call that on attr directly?
115
115
  end
116
116
 
117
117
  # Returns the wrapper for the representation. Mostly used in XML.
@@ -16,32 +16,34 @@ module Representable
16
16
 
17
17
 
18
18
  module ClassMethods
19
- def binding_for_definition(definition)
20
- return Representable::Hash::CollectionBinding.new(definition) if definition.array?
21
- return Representable::Hash::HashBinding.new(definition) if definition.hash?
22
- Representable::Hash::PropertyBinding.new(definition)
23
- end
24
-
25
19
  def from_hash(*args, &block)
26
20
  create_represented(*args, &block).from_hash(*args)
27
21
  end
28
22
  end
29
23
 
30
24
 
31
- def from_hash(data, options={})
25
+ def from_hash(data, options={}, format=:hash)
32
26
  if wrap = options[:wrap] || representation_wrap
33
27
  data = data[wrap.to_s]
34
28
  end
35
29
 
36
- update_properties_from(data, options, JSON)
30
+ update_properties_from(data, options, format)
37
31
  end
38
32
 
39
- def to_hash(options={})
40
- hash = create_representation_with({}, options, JSON)
33
+ def to_hash(options={}, format=:hash)
34
+ hash = create_representation_with({}, options, format)
41
35
 
42
36
  return hash unless wrap = options[:wrap] || representation_wrap
43
37
 
44
38
  {wrap => hash}
45
39
  end
40
+
41
+ private
42
+
43
+ def hash_binding_for_definition(definition)
44
+ return Representable::Hash::CollectionBinding.new(definition) if definition.array?
45
+ return Representable::Hash::HashBinding.new(definition) if definition.hash?
46
+ Representable::Hash::PropertyBinding.new(definition)
47
+ end
46
48
  end
47
49
  end
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "1.2.6"
2
+ VERSION = "1.2.7"
3
3
  end
@@ -4,14 +4,6 @@ require 'nokogiri'
4
4
 
5
5
  module Representable
6
6
  module XML
7
- def self.binding_for_definition(definition)
8
- return CollectionBinding.new(definition) if definition.array?
9
- return HashBinding.new(definition) if definition.hash? and not definition.options[:use_attributes] # FIXME: hate this.
10
- return AttributeHashBinding.new(definition) if definition.hash? and definition.options[:use_attributes]
11
- return AttributeBinding.new(definition) if definition.attribute
12
- PropertyBinding.new(definition)
13
- end
14
-
15
7
  def self.included(base)
16
8
  base.class_eval do
17
9
  include Representable
@@ -45,18 +37,28 @@ module Representable
45
37
  end
46
38
 
47
39
  def from_node(node, options={})
48
- update_properties_from(node, options, XML)
40
+ update_properties_from(node, options, :xml)
49
41
  end
50
42
 
51
43
  # Returns a Nokogiri::XML object representing this object.
52
44
  def to_node(options={})
53
45
  root_tag = options[:wrap] || representation_wrap
54
46
 
55
- create_representation_with(Nokogiri::XML::Node.new(root_tag.to_s, Nokogiri::XML::Document.new), options, XML)
47
+ create_representation_with(Nokogiri::XML::Node.new(root_tag.to_s, Nokogiri::XML::Document.new), options, :xml)
56
48
  end
57
49
 
58
50
  def to_xml(*args)
59
51
  to_node(*args).to_s
60
52
  end
53
+
54
+ private
55
+
56
+ def xml_binding_for_definition(definition)
57
+ return CollectionBinding.new(definition) if definition.array?
58
+ return HashBinding.new(definition) if definition.hash? and not definition.options[:use_attributes] # FIXME: hate this.
59
+ return AttributeHashBinding.new(definition) if definition.hash? and definition.options[:use_attributes]
60
+ return AttributeBinding.new(definition) if definition.attribute
61
+ PropertyBinding.new(definition)
62
+ end
61
63
  end
62
64
  end
@@ -5,14 +5,6 @@ module Representable
5
5
  module YAML
6
6
  include Hash
7
7
 
8
- def self.binding_for_definition(definition)
9
- return CollectionBinding.new(definition) if definition.array?
10
- #return HashBinding.new(definition) if definition.hash? and not definition.options[:use_attributes] # FIXME: hate this.
11
- #return AttributeHashBinding.new(definition) if definition.hash? and definition.options[:use_attributes]
12
- #return AttributeBinding.new(definition) if definition.attribute
13
- PropertyBinding.new(definition)
14
- end
15
-
16
8
  def self.included(base)
17
9
  base.class_eval do
18
10
  include Representable
@@ -36,9 +28,9 @@ module Representable
36
28
  end
37
29
 
38
30
 
39
- def from_yaml(doc, *args)
31
+ def from_yaml(doc, options={})
40
32
  hash = Psych.load(doc)
41
- from_hash(hash, *args)
33
+ from_hash(hash, options, :yaml)
42
34
  end
43
35
 
44
36
  # Returns a Nokogiri::XML object representing this object.
@@ -46,7 +38,7 @@ module Representable
46
38
  #root_tag = options[:wrap] || representation_wrap
47
39
 
48
40
  Psych::Nodes::Mapping.new.tap do |map|
49
- create_representation_with(map, options, YAML)
41
+ create_representation_with(map, options, :yaml)
50
42
  end
51
43
  end
52
44
 
@@ -57,5 +49,15 @@ module Representable
57
49
  doc.children << to_ast(*args)
58
50
  stream.to_yaml
59
51
  end
52
+
53
+ private
54
+
55
+ def yaml_binding_for_definition(definition)
56
+ return CollectionBinding.new(definition) if definition.array?
57
+ #return HashBinding.new(definition) if definition.hash? and not definition.options[:use_attributes] # FIXME: hate this.
58
+ #return AttributeHashBinding.new(definition) if definition.hash? and definition.options[:use_attributes]
59
+ #return AttributeBinding.new(definition) if definition.attribute
60
+ PropertyBinding.new(definition)
61
+ end
60
62
  end
61
63
  end
data/test/json_test.rb CHANGED
@@ -128,26 +128,26 @@ module JsonTest
128
128
 
129
129
  describe "#binding_for_definition" do
130
130
  it "returns ObjectBinding" do
131
- assert_kind_of Representable::Hash::ObjectBinding, Json.binding_for_definition(Def.new(:band, :class => Hash))
131
+ assert_kind_of Representable::Hash::ObjectBinding, @band.send(:hash_binding_for_definition, Def.new(:band, :class => Hash))
132
132
  end
133
133
 
134
134
  it "returns TextBinding" do
135
- assert_kind_of Representable::Hash::PropertyBinding, Json.binding_for_definition(Def.new(:band))
135
+ assert_kind_of Representable::Hash::PropertyBinding, @band.send(:hash_binding_for_definition, Def.new(:band))
136
136
  end
137
137
 
138
138
  it "returns HashBinding" do
139
- assert_kind_of Representable::Hash::HashBinding, Json.binding_for_definition(Def.new(:band, :hash => true))
139
+ assert_kind_of Representable::Hash::HashBinding, @band.send(:hash_binding_for_definition, Def.new(:band, :hash => true))
140
140
  end
141
141
 
142
142
  it "returns CollectionBinding" do
143
- assert_kind_of Representable::Hash::CollectionBinding, Json.binding_for_definition(Def.new(:band, :collection => true))
143
+ assert_kind_of Representable::Hash::CollectionBinding, @band.send(:hash_binding_for_definition, Def.new(:band, :collection => true))
144
144
  end
145
145
  end
146
146
 
147
147
  describe "#representable_bindings" do
148
148
  it "returns bindings for each property" do
149
- assert_equal 2, @band.send(:representable_bindings_for, Json).size
150
- assert_equal "name", @band.send(:representable_bindings_for, Json).first.definition.name
149
+ assert_equal 2, @band.send(:representable_bindings_for, :hash).size
150
+ assert_equal "name", @band.send(:representable_bindings_for, :hash).first.definition.name
151
151
  end
152
152
  end
153
153
  end
@@ -218,40 +218,40 @@ class RepresentableTest < MiniTest::Spec
218
218
  end
219
219
 
220
220
  it "copies values from document to object" do
221
- @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {}, Representable::JSON)
221
+ @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {}, :hash)
222
222
  assert_equal "No One's Choice", @band.name
223
223
  assert_equal 2, @band.groupies
224
224
  end
225
225
 
226
226
  it "accepts :exclude option" do
227
- @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {:exclude => [:groupies]}, Representable::JSON)
227
+ @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {:exclude => [:groupies]}, :hash)
228
228
  assert_equal "No One's Choice", @band.name
229
229
  assert_equal nil, @band.groupies
230
230
  end
231
231
 
232
232
  it "still accepts deprecated :except option" do # FIXME: remove :except option.
233
- assert_equal @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {:except => [:groupies]}, Representable::JSON), @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {:exclude => [:groupies]}, Representable::JSON)
233
+ assert_equal @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {:except => [:groupies]}, :hash), @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {:exclude => [:groupies]}, :hash)
234
234
  end
235
235
 
236
236
  it "accepts :include option" do
237
- @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {:include => [:groupies]}, Representable::JSON)
237
+ @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {:include => [:groupies]}, :hash)
238
238
  assert_equal 2, @band.groupies
239
239
  assert_equal nil, @band.name
240
240
  end
241
241
 
242
242
  it "always returns self" do
243
- assert_equal @band, @band.update_properties_from({"name"=>"Nofx"}, {}, Representable::JSON)
243
+ assert_equal @band, @band.update_properties_from({"name"=>"Nofx"}, {}, :hash)
244
244
  end
245
245
 
246
246
  it "includes false attributes" do
247
- @band.update_properties_from({"groupies"=>false}, {}, Representable::JSON)
247
+ @band.update_properties_from({"groupies"=>false}, {}, :hash)
248
248
  assert_equal false, @band.groupies
249
249
  end
250
250
 
251
251
  it "ignores (no-default) properties not present in the incoming document" do
252
- { Representable::JSON => {},
253
- Representable::XML => xml(%{<band/>})
254
- }.each do |format, document|
252
+ { Representable::JSON => [{}, :hash],
253
+ Representable::XML => [xml(%{<band/>}), :xml]
254
+ }.each do |format, config|
255
255
  nested_repr = Module.new do # this module is never applied.
256
256
  include format
257
257
  property :created_at
@@ -263,7 +263,7 @@ class RepresentableTest < MiniTest::Spec
263
263
  end
264
264
 
265
265
  @band = Band.new.extend(repr)
266
- @band.update_properties_from(document, {}, format)
266
+ @band.update_properties_from(config.first, {}, config.last)
267
267
  assert_equal nil, @band.name, "Failed in #{format}"
268
268
  end
269
269
  end
@@ -277,31 +277,31 @@ class RepresentableTest < MiniTest::Spec
277
277
  end
278
278
 
279
279
  it "compiles document from properties in object" do
280
- assert_equal({"name"=>"No One's Choice", "groupies"=>2}, @band.send(:create_representation_with, {}, {}, Representable::JSON))
280
+ assert_equal({"name"=>"No One's Choice", "groupies"=>2}, @band.send(:create_representation_with, {}, {}, :hash))
281
281
  end
282
282
 
283
283
  it "accepts :exclude option" do
284
- hash = @band.send(:create_representation_with, {}, {:exclude => [:groupies]}, Representable::JSON)
284
+ hash = @band.send(:create_representation_with, {}, {:exclude => [:groupies]}, :hash)
285
285
  assert_equal({"name"=>"No One's Choice"}, hash)
286
286
  end
287
287
 
288
288
  it "still accepts deprecated :except option" do # FIXME: remove :except option.
289
- assert_equal @band.send(:create_representation_with, {}, {:except => [:groupies]}, Representable::JSON), @band.send(:create_representation_with, {}, {:exclude => [:groupies]}, Representable::JSON)
289
+ assert_equal @band.send(:create_representation_with, {}, {:except => [:groupies]}, :hash), @band.send(:create_representation_with, {}, {:exclude => [:groupies]}, :hash)
290
290
  end
291
291
 
292
292
  it "accepts :include option" do
293
- hash = @band.send(:create_representation_with, {}, {:include => [:groupies]}, Representable::JSON)
293
+ hash = @band.send(:create_representation_with, {}, {:include => [:groupies]}, :hash)
294
294
  assert_equal({"groupies"=>2}, hash)
295
295
  end
296
296
 
297
297
  it "does not write nil attributes" do
298
298
  @band.groupies = nil
299
- assert_equal({"name"=>"No One's Choice"}, @band.send(:create_representation_with, {}, {}, Representable::JSON))
299
+ assert_equal({"name"=>"No One's Choice"}, @band.send(:create_representation_with, {}, {}, :hash))
300
300
  end
301
301
 
302
302
  it "writes false attributes" do
303
303
  @band.groupies = false
304
- assert_equal({"name"=>"No One's Choice","groupies"=>false}, @band.send(:create_representation_with, {}, {}, Representable::JSON))
304
+ assert_equal({"name"=>"No One's Choice","groupies"=>false}, @band.send(:create_representation_with, {}, {}, :hash))
305
305
  end
306
306
 
307
307
  describe "when :render_nil is true" do
@@ -314,7 +314,7 @@ class RepresentableTest < MiniTest::Spec
314
314
 
315
315
  @band.extend(mod) # FIXME: use clean object.
316
316
  @band.groupies = nil
317
- hash = @band.send(:create_representation_with, {}, {}, Representable::JSON)
317
+ hash = @band.send(:create_representation_with, {}, {}, :hash)
318
318
  assert_equal({"name"=>"No One's Choice", "groupies" => nil}, hash)
319
319
  end
320
320
 
@@ -327,7 +327,7 @@ class RepresentableTest < MiniTest::Spec
327
327
 
328
328
  @band.extend(mod) # FIXME: use clean object.
329
329
  @band.groupies = nil
330
- hash = @band.send(:create_representation_with, {}, {}, Representable::JSON)
330
+ hash = @band.send(:create_representation_with, {}, {}, :hash)
331
331
  assert_equal({"name"=>"No One's Choice", "groupies" => nil}, hash)
332
332
  end
333
333
  end
@@ -341,21 +341,21 @@ class RepresentableTest < MiniTest::Spec
341
341
  it "respects property when condition true" do
342
342
  @pop.class_eval { property :fame, :if => lambda { true } }
343
343
  band = @pop.new
344
- band.update_properties_from({"fame"=>"oh yes"}, {}, Representable::JSON)
344
+ band.update_properties_from({"fame"=>"oh yes"}, {}, :hash)
345
345
  assert_equal "oh yes", band.fame
346
346
  end
347
347
 
348
348
  it "ignores property when condition false" do
349
349
  @pop.class_eval { property :fame, :if => lambda { false } }
350
350
  band = @pop.new
351
- band.update_properties_from({"fame"=>"oh yes"}, {}, Representable::JSON)
351
+ band.update_properties_from({"fame"=>"oh yes"}, {}, :hash)
352
352
  assert_equal nil, band.fame
353
353
  end
354
354
 
355
355
  it "ignores property when :exclude'ed even when condition is true" do
356
356
  @pop.class_eval { property :fame, :if => lambda { true } }
357
357
  band = @pop.new
358
- band.update_properties_from({"fame"=>"oh yes"}, {:exclude => [:fame]}, Representable::JSON)
358
+ band.update_properties_from({"fame"=>"oh yes"}, {:exclude => [:fame]}, :hash)
359
359
  assert_equal nil, band.fame
360
360
  end
361
361
 
@@ -364,7 +364,7 @@ class RepresentableTest < MiniTest::Spec
364
364
  @pop.class_eval { property :fame, :if => lambda { groupies } }
365
365
  band = @pop.new
366
366
  band.groupies = true
367
- band.update_properties_from({"fame"=>"oh yes"}, {}, Representable::JSON)
367
+ band.update_properties_from({"fame"=>"oh yes"}, {}, :hash)
368
368
  assert_equal "oh yes", band.fame
369
369
  end
370
370
  end
data/test/xml_test.rb CHANGED
@@ -126,20 +126,20 @@ class XmlTest < MiniTest::Spec
126
126
 
127
127
  describe "XML#binding_for_definition" do
128
128
  it "returns AttributeBinding" do
129
- assert_kind_of XML::AttributeBinding, Representable::XML.binding_for_definition(Def.new(:band, :from => "band", :attribute => true))
129
+ assert_kind_of XML::AttributeBinding, @band.send(:xml_binding_for_definition, Def.new(:band, :from => "band", :attribute => true))
130
130
  end
131
131
 
132
132
  it "returns PropertyBinding" do
133
- assert_kind_of XML::PropertyBinding, Representable::XML.binding_for_definition(Def.new(:band, :class => Hash))
134
- assert_kind_of XML::PropertyBinding, Representable::XML.binding_for_definition(Def.new(:band, :from => :content))
133
+ assert_kind_of XML::PropertyBinding, @band.send(:xml_binding_for_definition, Def.new(:band, :class => Hash))
134
+ assert_kind_of XML::PropertyBinding, @band.send(:xml_binding_for_definition, Def.new(:band, :from => :content))
135
135
  end
136
136
 
137
137
  it "returns CollectionBinding" do
138
- assert_kind_of XML::CollectionBinding, Representable::XML.binding_for_definition(Def.new(:band, :collection => :true))
138
+ assert_kind_of XML::CollectionBinding, @band.send(:xml_binding_for_definition, Def.new(:band, :collection => :true))
139
139
  end
140
140
 
141
141
  it "returns HashBinding" do
142
- assert_kind_of XML::HashBinding, Representable::XML.binding_for_definition(Def.new(:band, :hash => :true))
142
+ assert_kind_of XML::HashBinding, @band.send(:xml_binding_for_definition, Def.new(:band, :hash => :true))
143
143
  end
144
144
  end
145
145
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: representable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.6
4
+ version: 1.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-06 00:00:00.000000000 Z
12
+ date: 2012-10-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri