purzelrakete-restful 0.2.6 → 0.2.7

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.
data/TODO.markdown CHANGED
@@ -5,4 +5,5 @@ Refactor this shice. Seriously, this has devolved into some nasty-ass code.
5
5
  * move configuration object out of rails folder - this is general stuff.
6
6
  * remove xml serialization here and test resource directly (in active_record_converter_test)
7
7
  * get rid of to_a warning
8
- * convert underscores to dashes (or not) in serializers instead of converter
8
+ * convert underscores to dashes (or not) in serializers instead of converter
9
+ * implement xml serialization of hashes
data/lib/restful.rb CHANGED
@@ -3,6 +3,7 @@ require 'restful/rails/active_record/configuration'
3
3
  require 'restful/rails/active_record/metadata_tools'
4
4
  require 'restful/rails/action_controller'
5
5
  require 'restful/converters/active_record'
6
+ require 'restful/apimodel/map'
6
7
  require 'restful/apimodel/resource'
7
8
  require 'restful/apimodel/attribute'
8
9
  require 'restful/apimodel/collection'
@@ -3,40 +3,16 @@
3
3
  #
4
4
  module Restful
5
5
  module ApiModel
6
- class Resource
7
- attr_accessor :base, :path, :url, :values, :name, :type
6
+ class Resource < Map
7
+ attr_accessor :base, :path, :url
8
8
 
9
9
  def initialize(name, url)
10
+ super(name)
11
+
10
12
  self.url = url[:url]
11
13
  self.path = url[:path]
12
14
  self.base = url[:base]
13
- self.name = name
14
15
  self.type = :resource
15
- self.values = []
16
- end
17
-
18
- def links
19
- self.values.select { |attribute| attribute.type == :link }
20
- end
21
-
22
- def simple_attributes
23
- self.values.select { |attribute| attribute.type == :simple_attribute }
24
- end
25
-
26
- def collections
27
- self.values.select { |attribute| attribute.type == :collection }
28
- end
29
-
30
- # invoke serialization
31
- def serialize(type)
32
- serializer = Restful::Serializers::Base.serializer(type)
33
- serializer.serialize(self)
34
- end
35
-
36
- # invoke deserialization
37
- def deserialize_from(type)
38
- serializer = Restful::Serializers::Base.serializer(type)
39
- serializer.deserialize(self)
40
16
  end
41
17
 
42
18
  def full_url
@@ -39,11 +39,11 @@ module Restful
39
39
  add_to_whitelist = []
40
40
 
41
41
  if config && config.is_a?(Hash) && config.keys.size == 1 && includes = config[:include]
42
- add_to_whitelist = [*includes]
42
+ add_to_whitelist = [*includes].map { |el| el.is_a?(String) ? el.to_sym : el }
43
43
  config = nil
44
44
  end
45
45
 
46
- config ||= self.class.restful_config if self.class.respond_to?(:restful_config)
46
+ config ||= self.class.restful_config.clone if self.class.respond_to?(:restful_config)
47
47
  config ||= []
48
48
 
49
49
  if config && !config.is_a?(Config)
@@ -51,11 +51,12 @@ module Restful
51
51
  end
52
52
 
53
53
  if self.class.respond_to?(:restful_config)
54
- config.whitelisted = self.class.restful_config.whitelisted if config.whitelisted.empty?
54
+ config.whitelisted = Array.new(self.class.restful_config.whitelisted) if config.whitelisted.empty?
55
55
  config.restful_options.merge! self.class.restful_config.restful_options
56
56
  end
57
57
 
58
58
  config.whitelisted += add_to_whitelist
59
+ config.whitelisted = config.whitelisted.uniq
59
60
 
60
61
  # array
61
62
  if self.is_a?(Array)
@@ -66,11 +67,19 @@ module Restful
66
67
 
67
68
  element_name = elements.first ? elements.first.name.pluralize : "nil-classes"
68
69
 
69
- collection = returning Restful.collection(element_name, elements, :array) do |collection|
70
+ returning Restful.collection(element_name, elements, :array) do |collection|
70
71
  collection.total_entries = self.total_entries if self.respond_to?(:total_entries)
71
- end
72
-
73
- collection
72
+ end
73
+
74
+ elsif self.is_a?(Hash)
75
+ elements = self.map do |k,v|
76
+ value = v.respond_to?(:to_restful) ? Restful::Converters::ActiveRecord.convert(v, v.class.restful_config) : v
77
+ Restful::ApiModel::Attribute.new(k, value, :map)
78
+ end
79
+
80
+ map = Restful::ApiModel::Map.new("hash")
81
+ map.values = elements
82
+ map
74
83
  else
75
84
  Restful::Converters::ActiveRecord.convert(self, config)
76
85
  end
@@ -29,6 +29,8 @@ module Restful
29
29
  end
30
30
 
31
31
  def formatted_value(value)
32
+ return value unless value.respond_to?(:value)
33
+
32
34
  return nil if value.value.blank? && value.extended_type != :false_class
33
35
  case value.extended_type
34
36
  when :datetime
@@ -10,9 +10,13 @@ module Restful
10
10
  serializer_name :hash
11
11
 
12
12
  def serialize(resource, options = {})
13
- resource.is_a?(Restful::ApiModel::Collection) ?
14
- serialize_collection(resource) :
13
+ case resource
14
+ when Restful::ApiModel::Collection then serialize_collection(resource)
15
+ when Restful::ApiModel::Resource then serialize_tuples(resource.values, resource.full_url)
16
+ when Restful::ApiModel::Map then serialize_map(resource)
17
+ else
15
18
  serialize_tuples(resource.values, resource.full_url)
19
+ end
16
20
  end
17
21
 
18
22
  private
@@ -27,6 +31,13 @@ module Restful
27
31
  values
28
32
  end
29
33
 
34
+ def serialize_map(map)
35
+ map.values.inject({}) do |memo, attribute|
36
+ memo[attribute.name] = serialize_value(attribute.value)
37
+ memo
38
+ end
39
+ end
40
+
30
41
  def serialize_tuples(tuples, url)
31
42
  tuples.inject({ "restful_url" => url }) do |params, value|
32
43
  params[value.name.to_s.tr("-", "_").to_sym] = serialize_value(value)
@@ -17,6 +17,8 @@ module Restful
17
17
  xml = options[:builder] || Builder::XmlMarkup.new(:indent => 2)
18
18
  xml.instruct! unless options[:instruct].is_a?(FalseClass)
19
19
 
20
+ raise NotImplementedError.new("xml serialization of maps has not been implemented. ") if resource.class == Restful::ApiModel::Map
21
+
20
22
  if resource.is_a?(Restful::ApiModel::Collection)
21
23
  add_collection(resource, xml, show_as_array = false)
22
24
  else
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.6"
3
+ s.version = "0.2.7"
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"
@@ -67,4 +67,14 @@ bloggs_da_pet_hater:
67
67
  "name": "Joe Bloggs",
68
68
  "pets": [],
69
69
  "created_at": "<%= @person.created_at.xmlschema %>"
70
+ }
71
+
72
+ hash_with_person:
73
+ |
74
+ {
75
+ "total_hits": 10,
76
+ "a_person": {
77
+ "restful_url": "http://example.com:3000/people/<%= @person.to_param %>",
78
+ "name": "Joe Bloggs"
79
+ }
70
80
  }
@@ -26,6 +26,7 @@ context "restful publish" do
26
26
  @pet = @person.pets.create(:name => "Mietze")
27
27
 
28
28
  @pet.to_restful(:include => :owner).values.map(&:name).should.include :owner
29
+ Pet.restful_config.whitelisted.should.not.include? :owner
29
30
  end
30
31
  end
31
32
 
@@ -69,4 +69,9 @@ context "json serializer" do
69
69
 
70
70
  json_should_eql_fixture(pets.to_restful_json, "pets", :pets_array_with_total_entries)
71
71
  end
72
+
73
+ specify "should be able to serialize a map" do
74
+ Person.restful_publish(:name)
75
+ json_should_eql_fixture({ "total_hits" => 10, "a_person" => @person }.to_restful_json, "people", :hash_with_person)
76
+ end
72
77
  end
@@ -44,7 +44,7 @@ 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
-
47
+
48
48
  specify "should serialize collections correctly" do
49
49
  xml_should_eql_fixture(@person.pets.to_restful_xml, "pets", :pets_array)
50
50
  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.6
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bornkessel