rabl 0.7.0 → 0.7.1
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 +6 -1
- data/README.md +34 -2
- data/lib/rabl/engine.rb +1 -1
- data/lib/rabl/renderer.rb +7 -0
- data/lib/rabl/version.rb +1 -1
- data/test/engine_test.rb +25 -3
- data/test/renderer_test.rb +3 -2
- metadata +3 -3
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -243,7 +243,7 @@ attributes :bar => :baz, :dog => :animal
|
|
243
243
|
# => # { baz : <bar value>, animal : <dog value> }
|
244
244
|
```
|
245
245
|
|
246
|
-
This currently does not work:
|
246
|
+
Named and aliased attributes can not be combined on the same line. This currently does not work:
|
247
247
|
|
248
248
|
```ruby
|
249
249
|
attributes :foo, :bar => :baz # throws exception
|
@@ -277,6 +277,15 @@ child :posts => :foobar do
|
|
277
277
|
end
|
278
278
|
```
|
279
279
|
|
280
|
+
You can also pass in the current object:
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
object @user
|
284
|
+
child :posts do |user|
|
285
|
+
attribute :title unless user.suspended?
|
286
|
+
end
|
287
|
+
```
|
288
|
+
|
280
289
|
### Gluing Attributes ###
|
281
290
|
|
282
291
|
You can also append child attributes back to the root node:
|
@@ -290,6 +299,13 @@ end
|
|
290
299
|
|
291
300
|
Use glue to add additional attributes to the parent object.
|
292
301
|
|
302
|
+
You can also pass in the current object:
|
303
|
+
|
304
|
+
```ruby
|
305
|
+
object @user
|
306
|
+
glue(@post) {|user| attribute :title if user.active? }
|
307
|
+
```
|
308
|
+
|
293
309
|
### Custom Nodes ###
|
294
310
|
|
295
311
|
This will generate a json response based on the result of the `node` block:
|
@@ -440,6 +456,19 @@ Rabl::Renderer.xml(@post, 'posts/show')
|
|
440
456
|
|
441
457
|
These methods allow RABL to be used for arbitrary conversions of an object into a desired format.
|
442
458
|
|
459
|
+
You can also pass in other instance variables to be used in your template as:
|
460
|
+
|
461
|
+
```ruby
|
462
|
+
Rabl::Renderer.new(@post, 'posts/show', :locals => { :custom_title => "Hello world!" })
|
463
|
+
````
|
464
|
+
|
465
|
+
Then, in your template, you can use `@custom_title` as:
|
466
|
+
|
467
|
+
```
|
468
|
+
attribute :content
|
469
|
+
node(:title) { @custom_title }
|
470
|
+
```
|
471
|
+
|
443
472
|
### Content Type Headers ###
|
444
473
|
|
445
474
|
Currently in RABL, the content-type of your response is not set automatically. This is because RABL is intended
|
@@ -532,7 +561,9 @@ Thanks to [Miso](http://gomiso.com) for allowing me to create this for our appli
|
|
532
561
|
* [Alli Witheford](https://github.com/alzeih) - Added Plist format support
|
533
562
|
* [Ryan Bigg](https://github.com/radar) - Improved template resolution code
|
534
563
|
* [Ivan Vanderbyl](https://github.com/ivanvanderbyl) - Added general purpose renderer
|
535
|
-
* [Cyril Mougel](https://github.com/shingara) - Added cache_engine pluggable support
|
564
|
+
* [Cyril Mougel](https://github.com/shingara) - Added cache_engine pluggable support and renderer tweaks
|
565
|
+
* [Teng Siong Ong](https://github.com/siong1987) - Improved renderer interface
|
566
|
+
* [Brad Dunbar](https://github.com/braddunbar) - Pass current object into blocks
|
536
567
|
|
537
568
|
and many more contributors listed in the [CHANGELOG](https://github.com/nesquena/rabl/blob/master/CHANGELOG.md).
|
538
569
|
|
@@ -555,3 +586,4 @@ Thanks again for all of these great projects.
|
|
555
586
|
## Copyright ##
|
556
587
|
|
557
588
|
Copyright © 2011-2012 Nathan Esquenazi. See [MIT-LICENSE](https://github.com/nesquena/rabl/blob/master/MIT-LICENSE) for details.
|
589
|
+
|
data/lib/rabl/engine.rb
CHANGED
@@ -31,7 +31,7 @@ module Rabl
|
|
31
31
|
else # without source location
|
32
32
|
instance_eval(@_source) if @_source.present?
|
33
33
|
end
|
34
|
-
|
34
|
+
instance_exec(data_object(@_data), &block) if block_given?
|
35
35
|
cache_results { self.send("to_" + @_options[:format].to_s) }
|
36
36
|
end
|
37
37
|
|
data/lib/rabl/renderer.rb
CHANGED
@@ -31,6 +31,13 @@ module Rabl
|
|
31
31
|
|
32
32
|
@options = options
|
33
33
|
@object = object
|
34
|
+
|
35
|
+
if @options[:locals]
|
36
|
+
@options[:locals].delete(:object) if @object
|
37
|
+
@options[:locals].each do |k,v|
|
38
|
+
instance_variable_set(:"@#{k}", v)
|
39
|
+
end
|
40
|
+
end
|
34
41
|
engine.source = self.process_source(source)
|
35
42
|
end
|
36
43
|
|
data/lib/rabl/version.rb
CHANGED
data/test/engine_test.rb
CHANGED
@@ -263,6 +263,18 @@ context "Rabl::Engine" do
|
|
263
263
|
template.render(scope).split('').sort
|
264
264
|
|
265
265
|
end.equals "{\"user\":{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}}".split('').sort
|
266
|
+
|
267
|
+
asserts "that it passes the data object to the block" do
|
268
|
+
template = rabl %{
|
269
|
+
object @user
|
270
|
+
child(@user => :person) do |user|
|
271
|
+
attribute :name if user.name == 'leo'
|
272
|
+
end
|
273
|
+
}
|
274
|
+
scope = Object.new
|
275
|
+
scope.instance_variable_set :@user, User.new(:name => 'leo')
|
276
|
+
template.render(scope)
|
277
|
+
end.equals "{\"user\":{\"person\":{\"name\":\"leo\"}}}"
|
266
278
|
end
|
267
279
|
|
268
280
|
context "#glue" do
|
@@ -277,6 +289,16 @@ context "Rabl::Engine" do
|
|
277
289
|
scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
|
278
290
|
template.render(scope).split('').sort
|
279
291
|
end.equals "{\"user\":{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}}".split('').sort
|
292
|
+
|
293
|
+
asserts "that it passes the data object to the block" do
|
294
|
+
template = rabl %{
|
295
|
+
object @user
|
296
|
+
glue(@user) {|user| attribute :age if user.name == 'leo' }
|
297
|
+
}
|
298
|
+
scope = Object.new
|
299
|
+
scope.instance_variable_set :@user, User.new(:name => 'leo', :age => 12)
|
300
|
+
template.render(scope)
|
301
|
+
end.equals "{\"user\":{\"age\":12}}"
|
280
302
|
end
|
281
303
|
|
282
304
|
teardown do
|
@@ -475,7 +497,7 @@ context "Rabl::Engine" do
|
|
475
497
|
Rabl.reset_configuration!
|
476
498
|
end
|
477
499
|
end
|
478
|
-
|
500
|
+
|
479
501
|
context "without child root" do
|
480
502
|
setup do
|
481
503
|
Rabl.configure do |config|
|
@@ -484,9 +506,9 @@ context "Rabl::Engine" do
|
|
484
506
|
config.enable_json_callbacks = false
|
485
507
|
end
|
486
508
|
end
|
487
|
-
|
509
|
+
|
488
510
|
context "#child" do
|
489
|
-
|
511
|
+
|
490
512
|
asserts "that it can create a child node without child root" do
|
491
513
|
template = rabl %{
|
492
514
|
child @users
|
data/test/renderer_test.rb
CHANGED
@@ -61,13 +61,14 @@ context "Rabl::Renderer" do
|
|
61
61
|
asserts 'passes :locals to render' do
|
62
62
|
source = %q{
|
63
63
|
attribute :name, :as => 'city'
|
64
|
+
node(:zipcode) { @zipcode }
|
64
65
|
}
|
65
66
|
|
66
67
|
user = User.new(:name => 'irvine')
|
67
68
|
|
68
|
-
renderer = Rabl::Renderer.new(source, nil, { :format => 'json', :locals => {:object => user} })
|
69
|
+
renderer = Rabl::Renderer.new(source, nil, { :format => 'json', :locals => {:object => user, :zipcode => "92602"} })
|
69
70
|
renderer.render.split("").sort
|
70
|
-
end.equals "{\"user\":{\"city\":\"irvine\"}}".split("").sort
|
71
|
+
end.equals "{\"user\":{\"city\":\"irvine\",\"zipcode\":\"92602\"}}".split("").sort
|
71
72
|
|
72
73
|
asserts 'loads source from file' do
|
73
74
|
File.open(tmp_path + "test.json.rabl", "w") do |f|
|
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.
|
4
|
+
version: 0.7.1
|
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-07
|
12
|
+
date: 2012-08-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -444,7 +444,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
444
444
|
version: '0'
|
445
445
|
requirements: []
|
446
446
|
rubyforge_project: rabl
|
447
|
-
rubygems_version: 1.8.
|
447
|
+
rubygems_version: 1.8.24
|
448
448
|
signing_key:
|
449
449
|
specification_version: 3
|
450
450
|
summary: General ruby templating with json, bson, xml and msgpack support
|