rabl 0.7.5 → 0.7.6

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 CHANGED
@@ -1,6 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
- ## 0.7.6 (unreleased)
3
+ ## 0.7.7 (unreleased)
4
+
5
+ ## 0.7.6
6
+
7
+ * Fix render behavior by separating data_object and data_name in engine
8
+ * Fix regression with 'child' behavior with nil on extends (with tests)
4
9
 
5
10
  ## 0.7.5
6
11
 
data/lib/rabl/engine.rb CHANGED
@@ -26,13 +26,14 @@ module Rabl
26
26
  locals.each { |k,v| instance_variable_set(:"@#{k}", v) }
27
27
  @_options[:scope] = @_scope
28
28
  @_options[:format] ||= self.request_format
29
- @_data = locals[:object].nil? ? self.default_object : locals[:object]
29
+ data = locals[:object].nil? ? self.default_object : locals[:object]
30
+ @_data_object, @_data_name = data_object(data), data_name(data)
30
31
  if @_options[:source_location]
31
32
  instance_eval(@_source, @_options[:source_location]) if @_source.present?
32
33
  else # without source location
33
34
  instance_eval(@_source) if @_source.present?
34
35
  end
35
- instance_exec(data_object(@_data), &block) if block_given?
36
+ instance_exec(@_data_object, &block) if block_given?
36
37
  cache_results { self.send("to_" + @_options[:format].to_s) }
37
38
  end
38
39
 
@@ -40,9 +41,9 @@ module Rabl
40
41
  # to_hash(:root => true, :child_root => true)
41
42
  def to_hash(options={})
42
43
  options = @_options.merge(options)
43
- data = data_object(@_data)
44
+ data = @_data_object
44
45
  builder = Rabl::Builder.new(options)
45
- options[:root_name] = determine_object_root(@_data, options[:root])
46
+ options[:root_name] = determine_object_root(@_data_object, @_data_name, options[:root])
46
47
 
47
48
  if is_object?(data) || !data # object @user
48
49
  builder.build(data, options)
@@ -88,7 +89,7 @@ module Rabl
88
89
  include_root = Rabl.configuration.include_xml_root
89
90
  include_child_root = include_root && Rabl.configuration.include_child_root
90
91
  options = options.reverse_merge(:root => include_root, :child_root => include_child_root)
91
- xml_options = Rabl.configuration.default_xml_options.merge(:root => data_name(@_data))
92
+ xml_options = Rabl.configuration.default_xml_options.merge(:root => @_data_name)
92
93
  to_hash(options).to_xml(xml_options)
93
94
  end
94
95
 
@@ -100,8 +101,8 @@ module Rabl
100
101
  options = options.reverse_merge(:root => include_root, :child_root => include_child_root)
101
102
  result = if collection_root_name
102
103
  { collection_root_name => to_hash(options) }
103
- elsif is_collection?(@_data) && @_data.is_a?(Array)
104
- { data_name(@_data) => to_hash(options) }
104
+ elsif is_collection?(@_data_object) && @_data_object.is_a?(Array)
105
+ { @_data_name => to_hash(options) }
105
106
  else
106
107
  to_hash(options)
107
108
  end
@@ -112,15 +113,17 @@ module Rabl
112
113
  # object(@user)
113
114
  # object @user => :person
114
115
  # object @users
115
- def object(data)
116
- @_data = data unless @_locals[:object]
116
+ def object(template_data)
117
+ current_data = (@_locals[:object].nil? || template_data == false) ? template_data : @_locals[:object]
118
+ @_data_object = data_object(current_data)
119
+ @_data_name = data_name(template_data.is_a?(Hash) && !current_data.is_a?(Hash) ? template_data : current_data)
117
120
  end
118
121
 
119
122
  # Returns the current object that is the topic of this template
120
123
  # Can be the collection or the object depending on topic assigned
121
124
  # root_object => @user
122
125
  def root_object
123
- @_data
126
+ @_data_object
124
127
  end
125
128
 
126
129
  # Sets the object as a collection casted to a simple array
@@ -143,7 +146,7 @@ module Rabl
143
146
  # cache 'user', expires_in: 1.hour
144
147
  # options is passed through to the cache store
145
148
  def cache(key = nil, options = nil)
146
- key ||= @_data # if called but missing, use object
149
+ key ||= @_data_object # if called but missing, use object
147
150
  @_cache = [key, options]
148
151
  end
149
152
 
data/lib/rabl/helpers.rb CHANGED
@@ -37,15 +37,15 @@ module Rabl
37
37
 
38
38
  # Returns the object rootname based on if the root should be included
39
39
  # Can be called with data as a collection or object
40
- # determine_object_root(@user, true) => "user"
41
- # determine_object_root(@user => :person) => "person"
40
+ # determine_object_root(@user, :user, true) => "user"
41
+ # determine_object_root(@user, :person) => "person"
42
42
  # determine_object_root([@user, @user]) => "user"
43
- def determine_object_root(data, include_root=true)
43
+ def determine_object_root(data_object, data_name=nil, include_root=true)
44
44
  return if object_root_name == false
45
- root_name = data_name(data).to_s if include_root
46
- if is_object?(data)
45
+ root_name = data_name.to_s if include_root
46
+ if is_object?(data_object)
47
47
  root_name
48
- elsif is_collection?(data)
48
+ elsif is_collection?(data_object)
49
49
  object_root_name || (root_name.singularize if root_name)
50
50
  end
51
51
  end
data/lib/rabl/partials.rb CHANGED
@@ -21,6 +21,7 @@ module Rabl
21
21
  # options must have :source (rabl file contents)
22
22
  # options can have :source_location (source filename)
23
23
  def object_to_hash(object, options={}, &block)
24
+ return object if object.nil?
24
25
  return [] if is_collection?(object) && object.blank? # empty collection
25
26
  engine_options = options.reverse_merge(:format => "hash", :view_path => @_view_path, :root => (options[:root] || false))
26
27
  Rabl::Engine.new(options[:source], engine_options).render(@_scope, :object => object, &block)
data/lib/rabl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.7.5"
2
+ VERSION = "0.7.6"
3
3
  end
@@ -22,7 +22,7 @@ context 'Rabl::Configuration' do
22
22
  end
23
23
  end
24
24
 
25
- asserts('uses a custom JSON engine') { topic.json_engine.to_s =~ /MultiJson.*::Yajl/ }
25
+ asserts('uses a custom JSON engine') { topic.json_engine.to_s =~ /yajl/i }
26
26
  end # custom json, symbol
27
27
 
28
28
  context 'custom JSON engine configured as Class' do
@@ -9,6 +9,7 @@ context "Rabl::Renderer" do
9
9
  helper(:context_scope) { |name, value|
10
10
  scope = Object.new
11
11
  stub(scope).controller { stub(Object).controller_name { name } }
12
+ scope.instance_variable_set :"@#{name.pluralize}", nil
12
13
  scope.instance_variable_set :"@#{name}", value
13
14
  scope
14
15
  }
@@ -129,6 +130,39 @@ context "Rabl::Renderer" do
129
130
  JSON.parse(renderer.render)
130
131
  end.equals JSON.parse("{\"user\":{\"age\":24,\"name\":\"irvine\"}}")
131
132
 
133
+
134
+ asserts 'handles extends with child as nil' do
135
+ File.open(tmp_path + "foo.json.rabl", "w") do |f|
136
+ f.puts %q{
137
+ object @foo
138
+ node(:test) do |foo|
139
+ {
140
+ test: "#{foo.attribute}"
141
+ }
142
+ end
143
+ }
144
+ end
145
+
146
+ File.open(tmp_path + "bar.json.rabl", "w") do |f|
147
+ f.puts %q{
148
+ object false
149
+ node do
150
+ {
151
+ test_attribute: 'test_value'
152
+ }
153
+ end
154
+ child(@foos => :foo_collection) do
155
+ extends 'foo'
156
+ end
157
+ }
158
+ end
159
+
160
+ sc = Object.new
161
+ sc.instance_variable_set :@foos, nil
162
+ renderer = Rabl::Renderer.new('bar', false, :view_path => tmp_path, :scope => sc)
163
+ JSON.parse(renderer.render)
164
+ end.equals JSON.parse("{\"test_attribute\":\"test_value\", \"foo_collection\":null}")
165
+
132
166
  asserts 'handles extends with custom node and object set false' do
133
167
  File.open(tmp_path + "test.json.rabl", "w") do |f|
134
168
  f.puts %q{
@@ -211,9 +245,9 @@ context "Rabl::Renderer" do
211
245
  }
212
246
  end
213
247
 
214
- scope = Object.new
215
- scope.instance_variable_set :@users, nil
216
- Rabl.render([], 'test', :view_path => tmp_path, :scope => scope)
248
+ sc = Object.new
249
+ sc.instance_variable_set :@users, nil
250
+ Rabl.render([], 'test', :view_path => tmp_path, :scope => sc)
217
251
  end.equals "{\"users\":[]}"
218
252
 
219
253
  asserts 'it renders an array when given an empty collection' do
@@ -257,7 +291,60 @@ context "Rabl::Renderer" do
257
291
  renderer = Rabl::Renderer.new('user', user, :view_path => tmp_path)
258
292
  JSON.parse(renderer.render)
259
293
  end.equals JSON.parse("{\"user\":{\"name\":\"irvine\",\"object\":{\"gender\":\"male\"},\"gender\":\"male\"}}")
260
- end
294
+
295
+
296
+ asserts "it renders for object false" do
297
+ File.open(tmp_path + "test2.rabl", "w") do |f|
298
+ f.puts %q{
299
+ object false
300
+ node(:foo) { 'bar' }
301
+ }
302
+ end
303
+
304
+ user = User.new(:name => 'ivan')
305
+ JSON.parse(Rabl.render(user, 'test2', :view_path => tmp_path))
306
+ end.equals JSON.parse("{\"foo\":\"bar\"}")
307
+
308
+ asserts "it renders for object key specified in template" do
309
+ File.open(tmp_path + "test3.rabl", "w") do |f|
310
+ f.puts %q{
311
+ object @user => :person
312
+ attributes :age, :name
313
+ }
314
+ end
315
+
316
+ user = User.new(:name => 'ivan')
317
+ JSON.parse(Rabl.render(user, 'test3', :view_path => tmp_path))
318
+ end.equals JSON.parse("{\"person\":{\"age\":24,\"name\":\"ivan\"} }")
319
+
320
+ asserts "it renders for overwriting object key specified in render" do
321
+ File.open(tmp_path + "test4.rabl", "w") do |f|
322
+ f.puts %q{
323
+ object @user => :person
324
+ attributes :age, :name
325
+ }
326
+ end
327
+
328
+ sc = Object.new
329
+ sc.instance_variable_set :@user, nil
330
+ user = User.new(:name => 'ivan')
331
+ JSON.parse(Rabl.render({ user => :human }, 'test4', :view_path => tmp_path, :scope => sc))
332
+ end.equals JSON.parse("{\"human\":{\"age\":24,\"name\":\"ivan\"} }")
333
+
334
+ asserts "it renders for specific object key passed to render" do
335
+ File.open(tmp_path + "test5.rabl", "w") do |f|
336
+ f.puts %q{
337
+ object @user
338
+ attributes :age, :name
339
+ }
340
+ end
341
+
342
+ sc = Object.new
343
+ sc.instance_variable_set :@user, nil
344
+ user = User.new(:name => 'ivan')
345
+ JSON.parse(Rabl.render({ user => :person }, 'test5', :view_path => tmp_path, :scope => sc))
346
+ end.equals JSON.parse("{\"person\":{\"age\":24,\"name\":\"ivan\"} }")
347
+ end # render
261
348
 
262
349
  context '.json' do
263
350
  asserts 'it renders json' do
@@ -271,7 +358,7 @@ context "Rabl::Renderer" do
271
358
  user = User.new(:name => 'ivan')
272
359
  JSON.parse(Rabl::Renderer.json(user, 'test', :view_path => tmp_path))
273
360
  end.equals JSON.parse("{\"user\":{\"age\":24,\"name\":\"ivan\",\"float\":1234.56}}")
274
- end
361
+ end # json
275
362
 
276
363
  context '.msgpack' do
277
364
  asserts 'it renders msgpack' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.7.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-29 00:00:00.000000000 Z
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport