purzelrakete-restful 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,12 @@ module Restful
9
9
 
10
10
  self.type = :collection
11
11
  end
12
+
13
+ # invoke serialization
14
+ def serialize(type)
15
+ serializer = Restful::Serializers::Base.serializer(type)
16
+ serializer.serialize(self)
17
+ end
12
18
  end
13
19
  end
14
20
  end
@@ -8,38 +8,39 @@ module Restful
8
8
  class HashSerializer < Base
9
9
 
10
10
  serializer_name :hash
11
-
12
11
 
13
12
  def serialize(resource, options = {})
14
- params = { "restful_url" => resource.full_url }
15
- resource.values.each do |value|
16
- params[hashify_key(value.name)] = serialize_value(value)
17
- end
18
- params
13
+ resource.is_a?(Restful::ApiModel::Collection) ?
14
+ serialize_array(resource.value) :
15
+ serialize_collection(resource)
19
16
  end
20
17
 
21
- private
18
+ private
22
19
 
23
20
  def hashify_key(original_key)
24
21
  original_key.to_s.tr("-", "_").to_sym
25
22
  end
26
23
 
24
+ def serialize_array(resources)
25
+ resources.map { |r| serialize(r) }
26
+ end
27
+
28
+ def serialize_collection(resource)
29
+ resource.values.inject({ "restful_url" => resource.full_url }) do |params, value|
30
+ params[hashify_key(value.name)] = serialize_value(value)
31
+ params
32
+ end
33
+ end
34
+
27
35
  def serialize_value(value)
28
36
  case value.type
29
- when :collection then serialize_collection(value.value)
37
+ when :collection then serialize_array(value.value)
30
38
  when :link then Restful::Rails.tools.dereference(value.value)
31
39
  when :resource then serialize(value)
32
40
  else formatted_value(value)
33
41
  end
34
42
  end
35
-
36
- def serialize_collection(resources)
37
- returning [] do |array|
38
- resources.each do |r|
39
- array << serialize(r)
40
- end
41
- end
42
- end
43
+
43
44
  end
44
45
  end
45
46
  end
@@ -13,33 +13,29 @@ module Restful
13
13
  serializer_name :xml
14
14
 
15
15
  def serialize(resource, options = {})
16
+
16
17
  xml = options[:builder] || Builder::XmlMarkup.new(:indent => 2)
17
18
  xml.instruct! unless options[:instruct].is_a?(FalseClass)
18
19
 
19
- xml.tag!(*root_element(resource)) do
20
- add_link_to(resource, xml, :self => true)
21
-
22
- resource.values.each do |value|
23
-
24
- if value.type == :collection # serialize the stuffs
25
- resources = value.value
26
- if first_resource = resources.first
27
- xml.tag!(first_resource.name.pluralize, collections_decorations) do
28
- resources.each do |resource|
29
- serialize(resource, { :instruct => false, :builder => xml })
30
- end
31
- end
20
+ if resource.is_a?(Restful::ApiModel::Collection)
21
+ add_collection(resource, xml, show_as_array = false)
22
+ else
23
+ xml.tag!(*root_element(resource)) do
24
+ add_link_to(resource, xml, :self => true)
25
+
26
+ resource.values.each do |value|
27
+ if value.type == :collection # serialize the stuffs
28
+ add_collection(value, xml)
29
+ elsif value.type == :link
30
+ add_link_to(value, xml)
31
+ elsif value.type == :resource
32
+ serialize(value, {:instruct => false, :builder => xml})
33
+ else # plain ole
34
+ add_tag(xml, value)
32
35
  end
33
-
34
- elsif value.type == :link
35
- add_link_to(value, xml)
36
- elsif value.type == :resource
37
- serialize(value, {:instruct => false, :builder => xml})
38
- else # plain ole
39
- add_tag(xml, value)
40
36
  end
41
- end
42
- end
37
+ end
38
+ end
43
39
  end
44
40
 
45
41
  # returns a resource, or collection of resources.
@@ -49,6 +45,17 @@ module Restful
49
45
 
50
46
  protected
51
47
 
48
+ def add_collection(value, xml, show_as_array = true)
49
+ resources = value.value
50
+ if first_resource = resources.first
51
+ xml.tag!(first_resource.name.pluralize, (show_as_array ? collections_decorations : {})) do
52
+ resources.each do |resource|
53
+ serialize(resource, { :instruct => false, :builder => xml })
54
+ end
55
+ end
56
+ end
57
+ end
58
+
52
59
  def add_link_to(resource, builder, options = {})
53
60
  is_self = !!options[:self]
54
61
 
data/restful.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "restful"
3
- s.version = "0.2.3"
3
+ s.version = "0.2.4"
4
4
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
5
5
  s.authors = ["Daniel Bornkessel", "Rany Keddo"]
6
6
  s.date = "2009-08-11"
@@ -18,4 +18,14 @@ gracie:
18
18
  <person-restful-url type="link">http://example.com:3000/people/<%= @person.id %></person-restful-url>
19
19
  <species>123</species>
20
20
  <name>Gracie</name>
21
- </pet>
21
+ </pet>
22
+
23
+ pets_array:
24
+ |
25
+ <?xml version="1.0" encoding="UTF-8"?>
26
+ <pets>
27
+ <pet>
28
+ <restful-url type="link">http://example.com:3000/pets/<%= @pet.id %></restful-url>
29
+ <name>mietze</name>
30
+ </pet>
31
+ </pets>
@@ -58,6 +58,7 @@ context "json serializer" do
58
58
  end
59
59
  end
60
60
 
61
-
62
-
61
+ specify "should serialize collections correctly" do
62
+ json_should_eql_fixture(@person.pets.to_restful_json, "pets", :pets_array)
63
+ end
63
64
  end
@@ -44,4 +44,8 @@ context "params serializer" do
44
44
  @person.pets.create(:species => "cat", :age => 100, :name => "motze")
45
45
  xml_should_eql_fixture(@person.to_restful_xml(:pets_ages_hash), "people", :hashy_person)
46
46
  end
47
- end
47
+
48
+ specify "should serialize collections correctly" do
49
+ xml_should_eql_fixture(@person.pets.to_restful_xml, "pets", :pets_array)
50
+ end
51
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: purzelrakete-restful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bornkessel