purzelrakete-restful 0.2.2 → 0.2.3
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.
@@ -5,7 +5,6 @@ module Restful
|
|
5
5
|
module Converters
|
6
6
|
class ActiveRecord
|
7
7
|
def self.convert(model, config, options = {})
|
8
|
-
|
9
8
|
published = []
|
10
9
|
nested = config.nested?
|
11
10
|
|
@@ -15,15 +14,10 @@ module Restful
|
|
15
14
|
:path => model.restful_path,
|
16
15
|
:url => model.restful_url
|
17
16
|
})
|
18
|
-
|
17
|
+
|
19
18
|
# simple attributes
|
20
|
-
resource.values += Restful::Rails.tools.simple_attributes_on(model).map do |
|
21
|
-
key, value
|
22
|
-
|
23
|
-
if config.published?(key.to_sym)
|
24
|
-
published << key.to_sym
|
25
|
-
Restful.attr(key.to_sym, value, compute_extended_type(model, key))
|
26
|
-
end
|
19
|
+
resource.values += Restful::Rails.tools.simple_attributes_on(model).map do |key, value|
|
20
|
+
convert_to_simple_attribute(key, value, config, published, model)
|
27
21
|
end.compact
|
28
22
|
|
29
23
|
# has_many, has_one
|
@@ -91,6 +85,14 @@ module Restful
|
|
91
85
|
|
92
86
|
resource
|
93
87
|
end
|
88
|
+
|
89
|
+
def self.convert_to_simple_attribute(key, value, config, published, model = nil)
|
90
|
+
if config.published?(key.to_sym)
|
91
|
+
published << key.to_sym
|
92
|
+
ext_type = (model ? compute_extended_type(model, key) : value.class.to_s.underscore.to_sym)
|
93
|
+
Restful.attr(key.to_sym, value, ext_type)
|
94
|
+
end
|
95
|
+
end
|
94
96
|
|
95
97
|
private
|
96
98
|
|
@@ -8,7 +8,8 @@ module Restful
|
|
8
8
|
def self.included(base)
|
9
9
|
base.send :class_inheritable_accessor, :restful_config
|
10
10
|
base.restful_config = Config.new
|
11
|
-
base.send :include,
|
11
|
+
base.send :include, ResourceInstanceMethods
|
12
|
+
base.send :include, CommonInstanceMethods
|
12
13
|
base.extend(ClassMethods)
|
13
14
|
end
|
14
15
|
|
@@ -27,26 +28,50 @@ module Restful
|
|
27
28
|
self.restful_config = Restful.cfg(*fieldnames)
|
28
29
|
end
|
29
30
|
end
|
30
|
-
|
31
|
-
module
|
32
|
-
|
31
|
+
|
32
|
+
module CommonInstanceMethods
|
33
33
|
#
|
34
34
|
# converts this AR object to an apimodel object. per default, only the
|
35
35
|
# attributes in self.class.restful_config are shown. this can be overriden
|
36
36
|
# by passing in something like @pet.to_restful(:name, :species).
|
37
37
|
#
|
38
|
-
def to_restful(config =
|
39
|
-
|
38
|
+
def to_restful(config = nil)
|
39
|
+
config ||= self.class.restful_config if self.class.respond_to?(:restful_config)
|
40
|
+
config ||= []
|
41
|
+
|
40
42
|
if config && !config.is_a?(Config)
|
41
43
|
config = Config.new(config)
|
42
44
|
end
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
+
if self.class.respond_to?(:restful_config)
|
47
|
+
config.whitelisted = self.class.restful_config.whitelisted if config.whitelisted.empty?
|
48
|
+
config.restful_options.merge! self.class.restful_config.restful_options
|
49
|
+
end
|
46
50
|
|
47
|
-
|
51
|
+
# array
|
52
|
+
if self.is_a?(Array)
|
53
|
+
elements = self.map do |el|
|
54
|
+
raise TypeError.new("Not all array elements respond to #to_restful. ") unless el.respond_to?(:to_restful)
|
55
|
+
Restful::Converters::ActiveRecord.convert(el, el.class.restful_config)
|
56
|
+
end
|
57
|
+
|
58
|
+
element_name = elements.first ? elements.first.name.pluralize : "nil-classes"
|
59
|
+
Restful.collection(element_name, elements, :array)
|
60
|
+
else
|
61
|
+
Restful::Converters::ActiveRecord.convert(self, config)
|
62
|
+
end
|
48
63
|
end
|
49
64
|
|
65
|
+
# FIXME: read Restful::Serializers::Base.serializers. Load order problems?
|
66
|
+
[:xml, :json, :atom_like].each do |format|
|
67
|
+
define_method("to_restful_#{ format }") do |*args|
|
68
|
+
self.to_restful(*args).serialize(format)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
module ResourceInstanceMethods
|
74
|
+
|
50
75
|
# simple method through which a model should know it's own name. override this where necessary.
|
51
76
|
def restful_url(url_base = Restful::Rails.api_hostname)
|
52
77
|
"#{ url_base }#{ restful_path }"
|
@@ -55,13 +80,6 @@ module Restful
|
|
55
80
|
def restful_path
|
56
81
|
"/#{ self.class.to_s.tableize }/#{ self.to_param }"
|
57
82
|
end
|
58
|
-
|
59
|
-
# FIXME: read out Restful::Serializers::Base.serializers. Load order problems?
|
60
|
-
[:xml, :json, :atom_like].each do |format|
|
61
|
-
define_method("to_restful_#{ format }") do |*args|
|
62
|
-
self.to_restful(*args).serialize(format)
|
63
|
-
end
|
64
|
-
end
|
65
83
|
end
|
66
84
|
|
67
85
|
class Config # configures what attributes are exposed to the api. for a single resource.
|
@@ -117,4 +135,5 @@ module Restful
|
|
117
135
|
end
|
118
136
|
end
|
119
137
|
|
120
|
-
ActiveRecord::Base.send :include, Restful::Rails::ActiveRecord::Configuration
|
138
|
+
ActiveRecord::Base.send :include, Restful::Rails::ActiveRecord::Configuration
|
139
|
+
Array.send :include, Restful::Rails::ActiveRecord::Configuration::CommonInstanceMethods
|
@@ -11,37 +11,35 @@ module Restful
|
|
11
11
|
|
12
12
|
|
13
13
|
def serialize(resource, options = {})
|
14
|
-
params = {}
|
15
|
-
|
14
|
+
params = { "restful_url" => resource.full_url }
|
16
15
|
resource.values.each do |value|
|
17
|
-
|
18
|
-
resources = value.value
|
19
|
-
next if resources.empty?
|
20
|
-
name = resources.first.name.pluralize
|
21
|
-
|
22
|
-
array = []
|
23
|
-
resources.each do |r|
|
24
|
-
array << serialize(r)
|
25
|
-
end
|
26
|
-
|
27
|
-
params[hashify_key(value.name)] = array
|
28
|
-
elsif value.type == :link
|
29
|
-
params[hashify_key(value.name)] = Restful::Rails.tools.dereference(value.value)
|
30
|
-
elsif value.type == :resource
|
31
|
-
params[hashify_key(value.name)] = serialize(value)
|
32
|
-
else # plain ole
|
33
|
-
params[hashify_key(value.name)] = formatted_value(value)
|
34
|
-
end
|
16
|
+
params[hashify_key(value.name)] = serialize_value(value)
|
35
17
|
end
|
36
|
-
|
37
|
-
params["restful_url"] = resource.full_url
|
38
18
|
params
|
39
19
|
end
|
40
20
|
|
41
21
|
private
|
22
|
+
|
42
23
|
def hashify_key(original_key)
|
43
24
|
original_key.to_s.tr("-", "_").to_sym
|
44
25
|
end
|
26
|
+
|
27
|
+
def serialize_value(value)
|
28
|
+
case value.type
|
29
|
+
when :collection then serialize_collection(value.value)
|
30
|
+
when :link then Restful::Rails.tools.dereference(value.value)
|
31
|
+
when :resource then serialize(value)
|
32
|
+
else formatted_value(value)
|
33
|
+
end
|
34
|
+
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
|
45
43
|
end
|
46
44
|
end
|
47
45
|
end
|
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
|
+
s.version = "0.2.3"
|
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"
|
data/test/test_helper.rb
CHANGED
@@ -99,7 +99,7 @@ end
|
|
99
99
|
def puts_hash_diff(hash1, hash2, indent = 0)
|
100
100
|
return if hash1 == hash2
|
101
101
|
|
102
|
-
(hash1.keys + hash2.keys).uniq.each do |key|
|
102
|
+
((hash1 || {}).keys + (hash2 || {}).keys).uniq.each do |key|
|
103
103
|
next if hash1[key] == hash2[key]
|
104
104
|
print " "*indent
|
105
105
|
if hash1[key].is_a? Hash or hash2[key].is_a? Hash
|