rabl 0.5.5.i → 0.5.5.j
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/CHANGELOG.md +1 -0
- data/README.md +6 -1
- data/lib/rabl/engine.rb +3 -5
- data/lib/rabl/helpers.rb +15 -0
- data/lib/rabl/version.rb +1 -1
- data/test/engine_test.rb +26 -9
- metadata +1 -1
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -258,7 +258,12 @@ collection @users, :root => "people", :object_root => "user"
|
|
258
258
|
# => { "people" : [ { "user" : { ... } } ] }
|
259
259
|
```
|
260
260
|
|
261
|
-
and this will be used as the default data for the rendering
|
261
|
+
and this will be used as the default data for the rendering, or disable the object root explicitly:
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
collection @users, :root => "people", :object_root => false
|
265
|
+
# => { "people" : [ { ... }, { ... } ] }
|
266
|
+
```
|
262
267
|
|
263
268
|
There can also be odd cases where the root-level of the response doesn't map directly to any object:
|
264
269
|
|
data/lib/rabl/engine.rb
CHANGED
@@ -31,14 +31,12 @@ module Rabl
|
|
31
31
|
# to_hash(:root => true, :child_root => true)
|
32
32
|
def to_hash(options={})
|
33
33
|
options = @_options.merge(options)
|
34
|
-
data
|
34
|
+
data = data_object(@_data)
|
35
35
|
builder = Rabl::Builder.new(options)
|
36
|
+
options[:root_name] = determine_object_root(@_data, options[:root])
|
36
37
|
if is_object?(data) || !data # object @user
|
37
|
-
options[:root_name] = root_name if options[:root]
|
38
38
|
builder.build(data, options)
|
39
39
|
elsif is_collection?(data) # collection @users
|
40
|
-
options[:root_name] = object_root_name if object_root_name
|
41
|
-
options[:root_name] ||= root_name.to_s.singularize if options[:root]
|
42
40
|
data.map { |object| builder.build(object, options) }
|
43
41
|
end
|
44
42
|
end
|
@@ -111,7 +109,7 @@ module Rabl
|
|
111
109
|
def collection(data, options={})
|
112
110
|
@_collection_name = options[:root] if options[:root]
|
113
111
|
@_collection_name ||= data.values.first if data.respond_to?(:each_pair)
|
114
|
-
@_object_root_name = options[:object_root] if options
|
112
|
+
@_object_root_name = options[:object_root] if options.has_key?(:object_root)
|
115
113
|
self.object(data_object(data).to_a) if data
|
116
114
|
end
|
117
115
|
|
data/lib/rabl/helpers.rb
CHANGED
@@ -30,6 +30,21 @@ module Rabl
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
# Returns the object rootname based on if the root should be included
|
34
|
+
# Can be called with data as a collection or object
|
35
|
+
# determine_object_root(@user, true) => "user"
|
36
|
+
# determine_object_root(@user => :person) => "person"
|
37
|
+
# determine_object_root([@user, @user]) => "user"
|
38
|
+
def determine_object_root(data, include_root=true)
|
39
|
+
return if object_root_name == false
|
40
|
+
root_name = data_name(data).to_s if include_root
|
41
|
+
if is_object?(data)
|
42
|
+
root_name
|
43
|
+
elsif is_collection?(data)
|
44
|
+
object_root_name || (root_name.singularize if root_name)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
33
48
|
# Returns true if obj is not enumerable
|
34
49
|
# is_object?(@user) => true
|
35
50
|
# is_object?([]) => false
|
data/lib/rabl/version.rb
CHANGED
data/test/engine_test.rb
CHANGED
@@ -77,15 +77,32 @@ context "Rabl::Engine" do
|
|
77
77
|
template.render(scope)
|
78
78
|
end.equals "[{\"user\":{}},{\"user\":{}}]"
|
79
79
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
80
|
+
asserts "that it sets root node for objects" do
|
81
|
+
template = rabl %{
|
82
|
+
collection @users => :people
|
83
|
+
}
|
84
|
+
scope = Object.new
|
85
|
+
scope.instance_variable_set :@users, [User.new, User.new]
|
86
|
+
template.render(scope)
|
87
|
+
end.equals "{\"people\":[{\"person\":{}},{\"person\":{}}]}"
|
88
|
+
|
89
|
+
asserts "that it doesn't set root node for objects when specified" do
|
90
|
+
template = rabl %{
|
91
|
+
collection @users, :root => :people, :object_root => false
|
92
|
+
}
|
93
|
+
scope = Object.new
|
94
|
+
scope.instance_variable_set :@users, [User.new, User.new]
|
95
|
+
template.render(scope)
|
96
|
+
end.equals "{\"people\":[{},{}]}"
|
97
|
+
|
98
|
+
asserts "that it sets proper object and root names when specified" do
|
99
|
+
template = rabl %{
|
100
|
+
collection @users, :root => :people, :object_root => :user
|
101
|
+
}
|
102
|
+
scope = Object.new
|
103
|
+
scope.instance_variable_set :@users, [User.new, User.new]
|
104
|
+
template.render(scope)
|
105
|
+
end.equals "{\"people\":[{\"user\":{}},{\"user\":{}}]}"
|
89
106
|
|
90
107
|
asserts "that it can use non-ORM objects" do
|
91
108
|
template = rabl %q{
|