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 +4 -4
- data/CHANGES.md +5 -0
- data/Gemfile +2 -1
- data/README.md +20 -3
- data/lib/representable.rb +1 -1
- data/lib/representable/bindings/xml_bindings.rb +16 -0
- data/lib/representable/definition.rb +4 -0
- data/lib/representable/version.rb +1 -1
- data/test/generic_test.rb +3 -24
- data/test/xml_bindings_test.rb +16 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 585370fa8b9543301be8e4a6804f2aff74639f1d
|
4
|
+
data.tar.gz: dd72e65cd02b0e5215a49957335088aab429be8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0164fdb08a11640bb6d3a42b24f5ea97f8d0c35b995abd4129adfe859ae4d6fde41767d01298bf969f41771fcb90a50f0824dde32f062fd15765dc6b827e5241
|
7
|
+
data.tar.gz: bf1a835d25ec7ac0d3e5c1465c634ef4ec3e67af5ef914adf7a92c6ff378f0b7d42e51b24a0621bc642a708231c17d552e7695a2f3dcae7d28c8d84139fedd39
|
data/CHANGES.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -595,9 +595,9 @@ composers: [Steward Copeland, Sting]
|
|
595
595
|
|
596
596
|
## More on XML
|
597
597
|
|
598
|
-
### Mapping
|
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
|
-
|
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
|
|
data/lib/representable.rb
CHANGED
@@ -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
|
data/test/generic_test.rb
CHANGED
@@ -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 "
|
21
|
+
it "leaves properties untouched" do
|
22
22
|
album.from_hash({})
|
23
|
-
|
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
|
data/test/xml_bindings_test.rb
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2013-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|