rabl 0.5.5.i → 0.5.5.j

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -12,6 +12,7 @@
12
12
  * Adds PList format support (thanks @alzeih)
13
13
  * Fixes infinite recursion in edge case calculating object root name
14
14
  * Fixes issue with nameless node that has an array result
15
+ * Adds support for `object_root => false` (Thanks @Lytol)
15
16
 
16
17
  ## 0.5.4
17
18
 
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, root_name = data_object(@_data), data_name(@_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[:object_root]
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
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.5.5.i"
2
+ VERSION = "0.5.5.j"
3
3
  end
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
- # TODO fix this test
81
- # asserts "that it sets root node for objects" do
82
- # template = rabl %{
83
- # collection @users => :people
84
- # }
85
- # scope = Object.new
86
- # scope.instance_variable_set :@users, [User.new, User.new]
87
- # template.render(scope)
88
- # end.equals "{\"people\":[{\"person\":{}},{\"person\":{}}]}"
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{
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rabl
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 6
5
- version: 0.5.5.i
5
+ version: 0.5.5.j
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nathan Esquenazi