representable 0.13.0 → 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ h2. 0.13.1
2
+
3
+ * Removed property :@name from @XML@ in favor of @:attribute => true@.
4
+
1
5
  h2. 0.13.0
2
6
 
3
7
  * We no longer create accessors in @Representable.property@ - you have to do this yourself using @attr_accessors@.
@@ -22,8 +22,8 @@ module Representable
22
22
  end
23
23
 
24
24
  # Reads values from +doc+ and sets properties accordingly.
25
- def update_properties_from(doc, options, &block)
26
- representable_bindings.each do |bin|
25
+ def update_properties_from(doc, options, format, &block)
26
+ representable_bindings_for(format).each do |bin|
27
27
  next if skip_property?(bin, options)
28
28
 
29
29
  value = bin.read(doc) || bin.definition.default
@@ -34,8 +34,8 @@ module Representable
34
34
 
35
35
  private
36
36
  # Compiles the document going through all properties.
37
- def create_representation_with(doc, options, &block)
38
- representable_bindings.each do |bin|
37
+ def create_representation_with(doc, options, format, &block)
38
+ representable_bindings_for(format).each do |bin|
39
39
  next if skip_property?(bin, options)
40
40
 
41
41
  value = send(bin.definition.getter) || bin.definition.default # DISCUSS: eventually move back to Ref.
@@ -55,8 +55,8 @@ private
55
55
  @representable_attrs ||= self.class.representable_attrs # DISCUSS: copy, or better not?
56
56
  end
57
57
 
58
- def representable_bindings
59
- representable_attrs.map {|attr| binding_for_definition(attr) }
58
+ def representable_bindings_for(format)
59
+ representable_attrs.map {|attr| format.binding_for_definition(attr) }
60
60
  end
61
61
 
62
62
  # Returns the wrapper for the representation. Mostly used in XML.
@@ -1,23 +1,18 @@
1
1
  module Representable
2
2
  # Created at class compile time. Keeps configuration options for one property.
3
3
  class Definition
4
- attr_reader :name, :sought_type, :from, :default, :representer_module
4
+ attr_reader :name, :sought_type, :from, :default, :representer_module, :attribute
5
5
  alias_method :getter, :name
6
6
 
7
7
  def initialize(sym, options={})
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[:as] || :text # TODO: deprecate :as in 1.0.
11
+ @sought_type = options[:class]
12
12
  @default = options[:default]
13
13
  @default ||= [] if array?
14
14
  @representer_module = options[:extend] # DISCUSS: move to Representable::DCI?
15
-
16
- # FIXME: move me to xml.
17
- if options[:from].to_s =~ /^@/
18
- @sought_type = :attr
19
- options[:from].sub!('@', '')
20
- end
15
+ @attribute = options[:attribute]
21
16
  end
22
17
 
23
18
  def instance_variable_name
@@ -8,9 +8,10 @@ module Representable
8
8
  # Note: The authorative methods are #to_hash and #from_hash, if you override #to_json instead,
9
9
  # things might work as expected.
10
10
  module JSON
11
- BINDING_FOR_TYPE = { # TODO: refactor #representable_accessor for better extendability.
12
- :text => TextBinding,
13
- }
11
+ def self.binding_for_definition(definition)
12
+ return ObjectBinding.new(definition) if definition.typed?
13
+ TextBinding.new(definition)
14
+ end
14
15
 
15
16
  def self.included(base)
16
17
  base.class_eval do
@@ -43,11 +44,11 @@ module Representable
43
44
  data = data[wrap.to_s]
44
45
  end
45
46
 
46
- update_properties_from(data, options)
47
+ update_properties_from(data, options, JSON)
47
48
  end
48
49
 
49
50
  def to_hash(options={})
50
- hash = create_representation_with({}, options)
51
+ hash = create_representation_with({}, options, JSON)
51
52
 
52
53
  return hash unless wrap = options[:wrap] || representation_wrap
53
54
 
@@ -58,9 +59,5 @@ module Representable
58
59
  def to_json(*args)
59
60
  to_hash(*args).to_json
60
61
  end
61
-
62
- def binding_for_definition(definition)
63
- (BINDING_FOR_TYPE[definition.sought_type] or ObjectBinding).new(definition)
64
- end
65
62
  end
66
63
  end
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "0.13.0"
2
+ VERSION = "0.13.1"
3
3
  end
@@ -4,10 +4,11 @@ require 'nokogiri'
4
4
 
5
5
  module Representable
6
6
  module XML
7
- BINDING_FOR_TYPE = {
8
- :attr => AttributeBinding,
9
- :text => TextBinding,
10
- }
7
+ def self.binding_for_definition(definition)
8
+ return ObjectBinding.new(definition) if definition.typed?
9
+ return AttributeBinding.new(definition) if definition.attribute
10
+ TextBinding.new(definition)
11
+ end
11
12
 
12
13
  def self.included(base)
13
14
  base.class_eval do
@@ -42,22 +43,18 @@ module Representable
42
43
  end
43
44
 
44
45
  def from_node(node, options={})
45
- update_properties_from(node, options)
46
+ update_properties_from(node, options, XML)
46
47
  end
47
48
 
48
49
  # Returns a Nokogiri::XML object representing this object.
49
50
  def to_node(options={})
50
51
  root_tag = options[:wrap] || representation_wrap
51
52
 
52
- create_representation_with(Nokogiri::XML::Node.new(root_tag.to_s, Nokogiri::XML::Document.new), options)
53
+ create_representation_with(Nokogiri::XML::Node.new(root_tag.to_s, Nokogiri::XML::Document.new), options, XML)
53
54
  end
54
55
 
55
56
  def to_xml(*args)
56
57
  to_node(*args).to_s
57
58
  end
58
-
59
- def binding_for_definition(definition)
60
- (BINDING_FOR_TYPE[definition.sought_type] or ObjectBinding).new(definition)
61
- end
62
59
  end
63
60
  end
@@ -35,7 +35,7 @@ class DefinitionTest < MiniTest::Spec
35
35
  end
36
36
 
37
37
  it "responds to #sought_type" do
38
- assert_equal :text, @def.sought_type
38
+ assert_equal nil, @def.sought_type
39
39
  end
40
40
  end
41
41
 
@@ -67,7 +67,7 @@ class DefinitionTest < MiniTest::Spec
67
67
  end
68
68
 
69
69
  it "responds to #sought_type" do
70
- assert_equal :text, @def.sought_type
70
+ assert_equal nil, @def.sought_type
71
71
  end
72
72
 
73
73
  it "responds to #default" do
@@ -129,18 +129,18 @@ module JsonTest
129
129
 
130
130
  describe "#binding_for_definition" do
131
131
  it "returns ObjectBinding" do
132
- assert_kind_of Json::ObjectBinding, @band.binding_for_definition(Def.new(:band, :class => Hash))
132
+ assert_kind_of Json::ObjectBinding, Json.binding_for_definition(Def.new(:band, :class => Hash))
133
133
  end
134
134
 
135
135
  it "returns TextBinding" do
136
- assert_kind_of Json::TextBinding, @band.binding_for_definition(Def.new(:band))
136
+ assert_kind_of Json::TextBinding, Json.binding_for_definition(Def.new(:band))
137
137
  end
138
138
  end
139
139
 
140
140
  describe "#representable_bindings" do
141
141
  it "returns bindings for each property" do
142
- assert_equal 2, @band.send(:representable_bindings).size
143
- assert_equal "name", @band.send(:representable_bindings).first.definition.name
142
+ assert_equal 2, @band.send(:representable_bindings_for, Json).size
143
+ assert_equal "name", @band.send(:representable_bindings_for, Json).first.definition.name
144
144
  end
145
145
  end
146
146
  end
@@ -100,13 +100,13 @@ class RepresentableTest < MiniTest::Spec
100
100
  band.name = "Bodyjar"
101
101
 
102
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.
103
+ assert_xml_equal "<band><name>Bodyjar</name></band>", band.to_xml
104
104
  end
105
105
 
106
106
  it "allows extending with different representers subsequentially" do
107
107
  module SongXmlRepresenter
108
108
  include Representable::XML
109
- property :name, :from => "@name"
109
+ property :name, :from => "name", :attribute => true
110
110
  end
111
111
 
112
112
  module SongJsonRepresenter
@@ -214,25 +214,25 @@ class RepresentableTest < MiniTest::Spec
214
214
  end
215
215
 
216
216
  it "copies values from document to object" do
217
- @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {})
217
+ @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {}, Representable::JSON)
218
218
  assert_equal "No One's Choice", @band.name
219
219
  assert_equal 2, @band.groupies
220
220
  end
221
221
 
222
222
  it "accepts :except option" do
223
- @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, :except => [:groupies])
223
+ @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {:except => [:groupies]}, Representable::JSON)
224
224
  assert_equal "No One's Choice", @band.name
225
225
  assert_equal nil, @band.groupies
226
226
  end
227
227
 
228
228
  it "accepts :include option" do
229
- @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, :include => [:groupies])
229
+ @band.update_properties_from({"name"=>"No One's Choice", "groupies"=>2}, {:include => [:groupies]}, Representable::JSON)
230
230
  assert_equal 2, @band.groupies
231
231
  assert_equal nil, @band.name
232
232
  end
233
233
 
234
234
  it "always returns self" do
235
- assert_equal @band, @band.update_properties_from({"name"=>"Nofx"}, {})
235
+ assert_equal @band, @band.update_properties_from({"name"=>"Nofx"}, {}, Representable::JSON)
236
236
  end
237
237
  end
238
238
 
@@ -244,16 +244,16 @@ class RepresentableTest < MiniTest::Spec
244
244
  end
245
245
 
246
246
  it "compiles document from properties in object" do
247
- assert_equal({"name"=>"No One's Choice", "groupies"=>2}, @band.send(:create_representation_with, {}, {}))
247
+ assert_equal({"name"=>"No One's Choice", "groupies"=>2}, @band.send(:create_representation_with, {}, {}, Representable::JSON))
248
248
  end
249
249
 
250
250
  it "accepts :except option" do
251
- hash = @band.send(:create_representation_with, {}, :except => [:groupies])
251
+ hash = @band.send(:create_representation_with, {}, {:except => [:groupies]}, Representable::JSON)
252
252
  assert_equal({"name"=>"No One's Choice"}, hash)
253
253
  end
254
254
 
255
255
  it "accepts :include option" do
256
- hash = @band.send(:create_representation_with, {}, :include => [:groupies])
256
+ hash = @band.send(:create_representation_with, {}, {:include => [:groupies]}, Representable::JSON)
257
257
  assert_equal({"groupies"=>2}, hash)
258
258
  end
259
259
  end
@@ -122,17 +122,17 @@ class XmlTest < MiniTest::Spec
122
122
  end
123
123
 
124
124
 
125
- describe "#binding_for_definition" do
125
+ describe "XML#binding_for_definition" do
126
126
  it "returns AttributeBinding" do
127
- assert_kind_of XML::AttributeBinding, @band.binding_for_definition(Def.new(:band, :from => "@band"))
127
+ assert_kind_of XML::AttributeBinding, Representable::XML.binding_for_definition(Def.new(:band, :from => "band", :attribute => true))
128
128
  end
129
129
 
130
130
  it "returns ObjectBinding" do
131
- assert_kind_of XML::ObjectBinding, @band.binding_for_definition(Def.new(:band, :class => Hash))
131
+ assert_kind_of XML::ObjectBinding, Representable::XML.binding_for_definition(Def.new(:band, :class => Hash))
132
132
  end
133
133
 
134
134
  it "returns TextBinding" do
135
- assert_kind_of XML::TextBinding, @band.binding_for_definition(Def.new(:band, :from => :content))
135
+ assert_kind_of XML::TextBinding, Representable::XML.binding_for_definition(Def.new(:band, :from => :content))
136
136
  end
137
137
  end
138
138
 
@@ -194,11 +194,11 @@ end
194
194
 
195
195
 
196
196
  class AttributesTest < MiniTest::Spec
197
- describe ":from => @rel" do
197
+ describe ":from => rel, :attribute => true" do
198
198
  class Link
199
199
  include Representable::XML
200
- property :href, :from => "@href"
201
- property :title, :from => "@title"
200
+ property :href, :from => "href", :attribute => true
201
+ property :title, :from => "title", :attribute => true
202
202
  attr_accessor :href, :title
203
203
  end
204
204
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 13
8
- - 0
9
- version: 0.13.0
8
+ - 1
9
+ version: 0.13.1
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-24 00:00:00 +01:00
17
+ date: 2011-12-27 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency