representable 1.7.1 → 1.7.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57ca6d3739a88e3c3e38ecb6b4aa0b33b5da43a4
4
- data.tar.gz: fc6f1765a152cbbb9300f01c60ae8f55c169ec6d
3
+ metadata.gz: 585370fa8b9543301be8e4a6804f2aff74639f1d
4
+ data.tar.gz: dd72e65cd02b0e5215a49957335088aab429be8d
5
5
  SHA512:
6
- metadata.gz: ff20b22f94094d5bd9b99927c1f9ed28f82b74270d30d9eab5f8cab40facf22a8a9dbe9ccbb258ec68145489c8f81bd03766c6082f6c0a16c5f54d1f45610b61
7
- data.tar.gz: 1c6c950c144548a986f55cdff935348ae253eee59339a3219416aba556aef22e59f707bf8ad9a30d3a3104314eb13ad1b67911ef2ccb10489bf6c21d0dda3f7c
6
+ metadata.gz: 0164fdb08a11640bb6d3a42b24f5ea97f8d0c35b995abd4129adfe859ae4d6fde41767d01298bf969f41771fcb90a50f0824dde32f062fd15765dc6b827e5241
7
+ data.tar.gz: bf1a835d25ec7ac0d3e5c1465c634ef4ec3e67af5ef914adf7a92c6ff378f0b7d42e51b24a0621bc642a708231c17d552e7695a2f3dcae7d28c8d84139fedd39
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ h2. 1.7.2
2
+
3
+ * `Representable#update_properties_from` is private now.
4
+ * Added the `:content` option in XML to map top-level node's content to a property.
5
+
1
6
  h2. 1.7.1
2
7
 
3
8
  * Introduce `Config#options` hash to store per-representer configuration.
data/Gemfile CHANGED
@@ -3,4 +3,5 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  #gem "virtus", :path => "../virtus"
6
- gem 'rake', "10.1.0"
6
+ gem 'rake', "10.1.0"
7
+ #gem 'debugger'
data/README.md CHANGED
@@ -595,9 +595,9 @@ composers: [Steward Copeland, Sting]
595
595
 
596
596
  ## More on XML
597
597
 
598
- ### Mapping tag attributes
598
+ ### Mapping Tag Attributes
599
599
 
600
- You can also map properties to tag attributes in representable.
600
+ You can also map properties to tag attributes in representable. This works only for the top-level node, thou (seen from the representer's perspective).
601
601
 
602
602
  ```ruby
603
603
  module SongRepresenter
@@ -613,7 +613,24 @@ Song.new(title: "American Idle").to_xml
613
613
 
614
614
  Naturally, this works for both ways.
615
615
 
616
- ### Wrapping collections
616
+
617
+ ### Mapping Content
618
+
619
+ The same concept can also be applied to content. If you need to map a property to the top-level node's content, use the `:content` option. Again, _top-level_ refers to the document fragment that maps to the representer.
620
+
621
+ ```ruby
622
+ module SongRepresenter
623
+ include Representable::XML
624
+
625
+ property :title, content: true
626
+ end
627
+
628
+ Song.new(title: "American Idle").to_xml
629
+ #=> <song>American Idle</song>
630
+ ```
631
+
632
+
633
+ ### Wrapping Collections
617
634
 
618
635
  It is sometimes unavoidable to wrap tag lists in a container tag.
619
636
 
@@ -17,13 +17,13 @@ module Representable
17
17
  end
18
18
  end
19
19
 
20
+ private
20
21
  # Reads values from +doc+ and sets properties accordingly.
21
22
  def update_properties_from(doc, options, format)
22
23
  # deserialize_for(bindings, mapper ? , options)
23
24
  representable_mapper(format, options).deserialize(doc, options)
24
25
  end
25
26
 
26
- private
27
27
  # Compiles the document going through all properties.
28
28
  def create_representation_with(doc, options, format)
29
29
  representable_mapper(format, options).serialize(doc, options)
@@ -11,6 +11,7 @@ module Representable
11
11
  return HashBinding.new(definition, *args) if definition.hash? and not definition.options[:use_attributes] # FIXME: hate this.
12
12
  return AttributeHashBinding.new(definition, *args) if definition.hash? and definition.options[:use_attributes]
13
13
  return AttributeBinding.new(definition, *args) if definition.attribute
14
+ return ContentBinding.new(definition, *args) if definition.content
14
15
  new(definition, *args)
15
16
  end
16
17
 
@@ -153,5 +154,20 @@ module Representable
153
154
  serialize_for(value, parent)
154
155
  end
155
156
  end
157
+
158
+ # Represents tag content.
159
+ class ContentBinding < PropertyBinding
160
+ def read(node)
161
+ node.content
162
+ end
163
+
164
+ def serialize_for(value, parent)
165
+ parent.content = serialize(value.to_s)
166
+ end
167
+
168
+ def write(parent, value)
169
+ serialize_for(value, parent)
170
+ end
171
+ end
156
172
  end
157
173
  end
@@ -57,6 +57,10 @@ module Representable
57
57
  options[:attribute]
58
58
  end
59
59
 
60
+ def content
61
+ options[:content]
62
+ end
63
+
60
64
  def skipable_nil_value?(value)
61
65
  value.nil? and not options[:render_nil]
62
66
  end
@@ -1,3 +1,3 @@
1
1
  module Representable
2
- VERSION = "1.7.1"
2
+ VERSION = "1.7.2"
3
3
  end
@@ -18,9 +18,10 @@ class GenericTest < MiniTest::Spec
18
18
  new_album.songs.must_equal [] # DISCUSS: do we really want this?
19
19
  end
20
20
 
21
- it "overrides property with empty array" do
21
+ it "leaves properties untouched" do
22
22
  album.from_hash({})
23
- album.songs.must_equal [] # DISCUSS: do we really want this?
23
+ # TODO: test property.
24
+ album.songs.must_equal ["Fuck Armageddon"] # DISCUSS: do we really want this?
24
25
  end
25
26
  end
26
27
 
@@ -189,28 +190,6 @@ class GenericTest < MiniTest::Spec
189
190
  end
190
191
  end
191
192
 
192
- def render(object)
193
- AssertableDocument.new(object.send("to_#{format}"), format)
194
- end
195
-
196
- def parse(object, input)
197
- object.send("from_#{format}", input)
198
- end
199
-
200
- class AssertableDocument
201
- attr_reader :document
202
-
203
- def initialize(document, format)
204
- @document, @format = document, format
205
- end
206
-
207
- def must_equal_document(*args)
208
- return document.must_equal_xml(*args) if @format == :xml
209
- document.must_equal(*args)
210
- end
211
- end
212
-
213
-
214
193
  describe "mix :extend and inline representers" do
215
194
  representer! do
216
195
  rpr_module = Module.new do
@@ -150,4 +150,20 @@ class XMLBindingTest < MiniTest::Spec
150
150
  end
151
151
  end
152
152
  end
153
+
154
+ describe "ContentBinding" do
155
+ before do
156
+ @property = Representable::XML::ContentBinding.new(Representable::Definition.new(:name, :content => true), nil)
157
+ end
158
+
159
+ it "extracts with #read" do
160
+ assert_equal "The Gargoyle", @property.read(Nokogiri::XML("<song>The Gargoyle</song>").root)
161
+ end
162
+
163
+ it "inserts with #write" do
164
+ parent = Nokogiri::XML::Node.new("song", @doc)
165
+ @property.write(parent, "The Gargoyle")
166
+ assert_xml_equal("<song>The Gargoyle</song>", parent.to_s)
167
+ end
168
+ end
153
169
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: representable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1
4
+ version: 1.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-16 00:00:00.000000000 Z
11
+ date: 2013-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri