rabl 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -9,4 +9,3 @@ notifications:
9
9
  recipients:
10
10
  - nesquena@gmail.com
11
11
  - databyte@gmail.com
12
-
data/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
- ## 0.7.2 (unreleased)
3
+ ## 0.7.3 (unreleased)
4
+
5
+ ## 0.7.2
6
+
7
+ * Set instance variables for locals in engine instead of renderer (Thanks @geehsien)
8
+ * Changes default JSON engine for Rails, move logic to separate class (Thanks @shmeltex)
4
9
 
5
10
  ## 0.7.1
6
11
 
data/Gemfile CHANGED
@@ -3,17 +3,21 @@ source :rubygems
3
3
  # Specify your gem's dependencies in rabl.gemspec
4
4
  gemspec
5
5
 
6
- gem "rake"
7
- gem "i18n", '~> 0.6'
6
+ gem 'rake', :require => false
7
+ gem 'i18n', '~> 0.6'
8
8
 
9
9
  platforms :mri_18 do
10
10
  gem 'SystemTimer'
11
11
  end
12
12
 
13
- # FIXTURES
14
13
  group :test do
15
- gem 'rack-test', :require => "rack/test"
16
- gem 'activerecord', :require => "active_record"
14
+ # RABL TEST
15
+ gem 'activesupport', :require => 'active_support'
16
+ gem 'builder'
17
+
18
+ # FIXTURES
19
+ gem 'rack-test', :require => 'rack/test'
20
+ gem 'activerecord', :require => 'active_record'
17
21
  gem 'sqlite3'
18
22
  gem 'sinatra', '>= 1.2.0'
19
23
  end
data/Gemfile.ci CHANGED
@@ -10,3 +10,5 @@ platforms :mri_18 do
10
10
  gem 'SystemTimer'
11
11
  end
12
12
 
13
+ gem 'activesupport', :require => 'active_support'
14
+ gem 'builder'
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # RABL #
2
2
 
3
+ [![Continuous Integration status](https://secure.travis-ci.org/nesquena/rabl.png)](http://travis-ci.org/nesquena/rabl)
4
+ [![Dependency Status](https://gemnasium.com/nesquena/rabl.png)](https://gemnasium.com/nesquena/rabl)
5
+
3
6
  RABL (Ruby API Builder Language) is a Rails and [Padrino](http://padrinorb.com) ruby templating system for generating JSON, XML, MessagePack, PList and BSON. When using the ActiveRecord 'to_json' method, I tend to quickly find myself wanting a more expressive and powerful solution for generating APIs.
4
7
  This is especially frustrating when the JSON representation is complex or doesn't match the exact schema defined in the database.
5
8
 
@@ -109,7 +112,7 @@ Rabl.configure do |config|
109
112
  # config.cache_sources = Rails.env != 'development' # Defaults to false
110
113
  # config.cache_engine = Rabl::CacheEngine.new # Defaults to Rails cache
111
114
  # config.escape_all_output = false
112
- # config.json_engine = nil # Any multi\_json engines
115
+ # config.json_engine = nil # Any multi_json engines or a Class with #encode method
113
116
  # config.msgpack_engine = nil # Defaults to ::MessagePack
114
117
  # config.bson_engine = nil # Defaults to ::BSON
115
118
  # config.plist_engine = nil # Defaults to ::Plist::Emit
@@ -148,7 +151,7 @@ If `view_paths` is set to a path, this view path will be checked for every rabl
148
151
  Add to this path especially when including Rabl in an engine and using view paths within a another Rails app.
149
152
 
150
153
  Note that the `json_engine` option uses [multi_json](http://intridea.com/2010/6/14/multi-json-the-swappable-json-handler) engine
151
- defaults so that in most cases you **don't need to configure this** directly. If you wish to use yajl as
154
+ defaults so that in most cases you **don't need to configure this** directly. For example, if you wish to use yajl as
152
155
  the primary JSON encoding engine simply add that to your Gemfile:
153
156
 
154
157
  ```ruby
@@ -158,6 +161,13 @@ gem 'yajl-ruby', :require => "yajl"
158
161
 
159
162
  and RABL will automatically start using that engine for encoding your JSON responses!
160
163
 
164
+ To use RABL with JSON engine not supported by `multi_json`, ensure that JSON engine
165
+ supports `encode` method and set `json_engine` option to the engine's Class name:
166
+
167
+ ```ruby
168
+ config.json_engine = ActiveSupport::JSON
169
+ ```
170
+
161
171
  ### Format Configuration ###
162
172
 
163
173
  RABL supports configuration for MessagePack, BSON, and Plist. Check the
@@ -347,7 +357,7 @@ node :location do
347
357
  end
348
358
  ```
349
359
 
350
- or event access an object associated with the parent model:
360
+ or even access an object associated with the parent model:
351
361
 
352
362
  ```ruby
353
363
  node :location do |m|
@@ -449,17 +459,20 @@ Rabl.render(object, template, :view_path => 'app/views', :format => :json) #=> "
449
459
  You can use convenience methods on `Rabl::Renderer` to render the objects as well:
450
460
 
451
461
  ```ruby
452
- Rabl::Renderer.new(@post, 'posts/show', :view_path => 'app/views')
453
462
  Rabl::Renderer.json(@post, 'posts/show')
454
463
  Rabl::Renderer.xml(@post, 'posts/show')
455
464
  ```
456
465
 
457
466
  These methods allow RABL to be used for arbitrary conversions of an object into a desired format.
458
467
 
468
+ ```ruby
469
+ Rabl::Renderer.new('posts/show', @post, :view_path => 'app/views', :format => 'hash').render
470
+ ```
471
+
459
472
  You can also pass in other instance variables to be used in your template as:
460
473
 
461
474
  ```ruby
462
- Rabl::Renderer.new(@post, 'posts/show', :locals => { :custom_title => "Hello world!" })
475
+ Rabl::Renderer.new('posts/show', @post, :locals => { :custom_title => "Hello world!" })
463
476
  ````
464
477
 
465
478
  Then, in your template, you can use `@custom_title` as:
@@ -535,12 +548,6 @@ Check out the [Issues](https://github.com/nesquena/rabl/issues) tab for a full l
535
548
 
536
549
  * Rigorous benchmarking and performance optimizations
537
550
 
538
- ## Continuous Integration ##
539
-
540
- [![Continuous Integration status](https://secure.travis-ci.org/nesquena/rabl.png)](http://travis-ci.org/nesquena/rabl)
541
-
542
- CI is hosted by [travis-ci.org](http://travis-ci.org).
543
-
544
551
  ## Authors and Contributors ##
545
552
 
546
553
  Thanks to [Miso](http://gomiso.com) for allowing me to create this for our applications and release this project!
@@ -16,8 +16,10 @@ code(:created_date) do |p|
16
16
  partial("posts/date", :object => p.created_at)
17
17
  end
18
18
 
19
- node(:foo) { helper_foo }
20
-
21
19
  node(:post) do |post|
22
20
  [post.title, post.body]
23
21
  end
22
+
23
+ node(:foo) { helper_foo }
24
+
25
+ node(:created_at_in_words) {|p| time_ago_in_words(p.created_at) }
@@ -12,6 +12,6 @@ gem 'riot', :group => "test"
12
12
  gem 'rack-test', :require => "rack/test", :group => "test"
13
13
 
14
14
  # Padrino
15
- gem 'padrino', '0.10.2'
15
+ gem 'padrino', '~> 0.10.7'
16
16
  gem 'json'
17
- gem 'rabl', :path => File.expand_path(File.dirname(__FILE__) + "/../../")
17
+ gem 'rabl', :path => File.expand_path(File.dirname(__FILE__) + "/../../")
@@ -1,6 +1,6 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
- gem 'rails', '3.0.12'
3
+ gem 'rails', '~> 3.0.17'
4
4
 
5
5
  # Bundle edge Rails instead:
6
6
  # gem 'rails', :git => 'git://github.com/rails/rails.git'
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rails', '3.2.3'
3
+ gem 'rails', '~> 3.2.8'
4
4
 
5
5
  # Bundle edge Rails instead:
6
6
  # gem 'rails', :git => 'git://github.com/rails/rails.git'
@@ -9,7 +9,6 @@ gem 'sqlite3'
9
9
  gem 'rabl', :path => File.expand_path(File.dirname(__FILE__) + "/../../")
10
10
  gem 'riot', :group => "test"
11
11
 
12
-
13
12
  # Gems used only for assets and not required
14
13
  # in production environments by default.
15
14
  group :assets do
@@ -134,6 +134,7 @@ context "PostsController" do
134
134
  end # date node
135
135
 
136
136
  asserts("contains helper action") { topic["foo"] }.equals { "BAR!" }
137
+ denies("contains helper action") { topic["created_at_in_words"] }.nil
137
138
 
138
139
  asserts("contains post attributes via node") { topic["post"] }.equals { [@post1.title, @post1.body] }
139
140
  end # show action, json
@@ -4,10 +4,10 @@ gem 'rake'
4
4
  gem 'activerecord', :require => "active_record"
5
5
  gem 'sqlite3'
6
6
 
7
- gem 'sinatra', '>= 1.2.0'
7
+ gem 'sinatra', '>= 1.3.3'
8
8
  gem 'json'
9
9
  gem 'rabl', :path => File.expand_path(File.dirname(__FILE__) + "/../../")
10
10
 
11
11
  # Test requirements
12
12
  gem 'riot', :group => "test"
13
- gem 'rack-test', :require => "rack/test", :group => "test"
13
+ gem 'rack-test', :require => "rack/test", :group => "test"
data/lib/rabl.rb CHANGED
@@ -12,6 +12,7 @@ require 'rabl/builder'
12
12
  require 'rabl/configuration'
13
13
  require 'rabl/renderer'
14
14
  require 'rabl/cache_engine'
15
+ require 'rabl/json_engine'
15
16
  require 'rabl/railtie' if defined?(Rails) && Rails.version =~ /^3/
16
17
 
17
18
  # Rabl.register!
@@ -16,9 +16,6 @@ begin
16
16
  rescue LoadError
17
17
  end
18
18
 
19
- # Load MultiJSON
20
- require 'multi_json'
21
-
22
19
  module Rabl
23
20
  # Rabl.host
24
21
  class Configuration
@@ -65,19 +62,16 @@ module Rabl
65
62
  @cache_engine = Rabl::CacheEngine.new
66
63
  end
67
64
 
68
- # @param [Symbol, String, #encode] engine_name The name of a JSON engine,
65
+ # @param [Symbol, Class] engine_name The name of a JSON engine,
69
66
  # or class that responds to `encode`, to use to encode Rabl templates
70
67
  # into JSON. For more details, see the MultiJson gem.
71
68
  def json_engine=(engine_name_or_class)
72
- @engine_name = engine_name_or_class
73
- # multi_json compatibility TODO
74
- MultiJson.respond_to?(:use) ? MultiJson.use(@engine_name) :
75
- MultiJson.engine = @engine_name
69
+ JsonEngine.instance.set(engine_name_or_class)
76
70
  end
77
71
 
78
72
  # @return The JSON engine used to encode Rabl templates into JSON
79
73
  def json_engine
80
- get_json_engine
74
+ JsonEngine.instance.current_engine
81
75
  end
82
76
 
83
77
  ##
@@ -109,16 +103,5 @@ module Rabl
109
103
  def default_xml_options
110
104
  @_default_xml_options ||= @xml_options.reverse_merge(DEFAULT_XML_OPTIONS)
111
105
  end
112
-
113
- private
114
-
115
- def get_json_engine
116
- if !defined?(@engine_name) && defined?(Rails)
117
- ActiveSupport::JSON
118
- else # use multi_json
119
- # multi_json compatibility TODO
120
- MultiJson.respond_to?(:adapter) ? MultiJson.adapter : MultiJson.engine
121
- end
122
- end
123
106
  end
124
107
  end
data/lib/rabl/engine.rb CHANGED
@@ -23,6 +23,7 @@ module Rabl
23
23
  reset_options!
24
24
  @_locals, @_scope = locals, scope
25
25
  self.copy_instance_variables_from(@_scope, [:@assigns, :@helpers])
26
+ locals.each { |k,v| instance_variable_set(:"@#{k}", v) }
26
27
  @_options[:scope] = @_scope
27
28
  @_options[:format] ||= self.request_format
28
29
  @_data = locals[:object] || self.default_object
@@ -0,0 +1,33 @@
1
+ # Defines the default JSON engine for RABL when rendering JSON is invoked on a template.
2
+ # You can define your own json engine by creating an object that responds to the `encode` method
3
+ # and setting the corresponding configuration option:
4
+ #
5
+ # config.json_engine = ActiveSupport::JSON
6
+ #
7
+
8
+ require 'multi_json'
9
+ require 'singleton'
10
+
11
+ module Rabl
12
+ class JsonEngine
13
+ include Singleton
14
+
15
+ attr_reader :current_engine
16
+
17
+ def initialize
18
+ @current_engine = MultiJson.respond_to?(:adapter) ? MultiJson.adapter : MultiJson.engine
19
+ end
20
+
21
+ def set(engine_name_or_class)
22
+ @current_engine = begin
23
+ MultiJson.respond_to?(:use) ?
24
+ MultiJson.use(engine_name_or_class) :
25
+ MultiJson.engine = engine_name_or_class
26
+ rescue RuntimeError => e #
27
+ # Re-raise if engine_name_or_class is invalid
28
+ raise e unless engine_name_or_class.respond_to?(:encode)
29
+ engine_name_or_class
30
+ end
31
+ end
32
+ end # JsonEngine
33
+ end # Rabl
data/lib/rabl/renderer.rb CHANGED
@@ -32,12 +32,6 @@ module Rabl
32
32
  @options = options
33
33
  @object = object
34
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
41
35
  engine.source = self.process_source(source)
42
36
  end
43
37
 
data/lib/rabl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.7.1"
2
+ VERSION = "0.7.2"
3
3
  end
data/rabl.gemspec CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
27
27
  s.add_development_dependency 'rake'
28
28
  s.add_development_dependency 'tilt'
29
29
  s.add_development_dependency 'yajl-ruby'
30
- s.add_development_dependency 'msgpack', '~> 0.4.5'
31
- s.add_development_dependency 'bson', '~> 1.5.2'
30
+ s.add_development_dependency 'msgpack', '~> 0.4.5'
31
+ s.add_development_dependency 'bson', '~> 1.7.0'
32
32
  s.add_development_dependency 'plist'
33
33
  end
data/test.watchr CHANGED
@@ -45,4 +45,3 @@ Signal.trap 'INT' do
45
45
  run_all_tests
46
46
  end
47
47
  end
48
-
@@ -1,7 +1,5 @@
1
1
  require File.expand_path('../teststrap', __FILE__)
2
- require File.expand_path('../../lib/rabl', __FILE__)
3
- require File.expand_path('../../lib/rabl/template', __FILE__)
4
- require File.expand_path('../models/user', __FILE__)
2
+ require 'rabl/template'
5
3
 
6
4
  context "Rabl::Engine" do
7
5
  helper(:rabl) { |t| RablTemplate.new("code", :format => 'bson') { t } }
data/test/builder_test.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require File.expand_path('../teststrap', __FILE__)
2
- require File.expand_path('../models/user', __FILE__)
3
2
 
4
3
  context "Rabl::Builder" do
5
4
  helper(:builder) { |opt| Rabl::Builder.new(opt) }
@@ -1,5 +1,4 @@
1
1
  require File.expand_path('../teststrap', __FILE__)
2
- require File.expand_path('../../lib/rabl', __FILE__)
3
2
 
4
3
  context 'Rabl::Configuration' do
5
4
  context 'defaults' do
@@ -16,7 +15,7 @@ context 'Rabl::Configuration' do
16
15
  asserts(:cache_engine).is_a?(Rabl::CacheEngine)
17
16
  end
18
17
 
19
- context 'custom JSON engine' do
18
+ context 'custom JSON engine configured as Symbol' do
20
19
  setup do
21
20
  Rabl.configure do |c|
22
21
  c.json_engine = :yajl
@@ -24,5 +23,23 @@ context 'Rabl::Configuration' do
24
23
  end
25
24
 
26
25
  asserts('uses a custom JSON engine') { topic.json_engine.to_s =~ /MultiJson.*::Yajl/ }
27
- end
26
+ end # custom json, symbol
27
+
28
+ context 'custom JSON engine configured as Class' do
29
+ setup do
30
+ Rabl.configure do |c|
31
+ c.json_engine = ActiveSupport::JSON
32
+ end
33
+ end
34
+
35
+ asserts('uses a custom JSON engine') { topic.json_engine.to_s == 'ActiveSupport::JSON' }
36
+ end # custom JSON, class
37
+
38
+ context 'invalid JSON engine configured' do
39
+ asserts {
40
+ Rabl.configure do |c|
41
+ c.json_engine = Kernel
42
+ end
43
+ }.raises(RuntimeError)
44
+ end # invalid
28
45
  end
data/test/engine_test.rb CHANGED
@@ -1,7 +1,6 @@
1
+ require 'json'
1
2
  require File.expand_path('../teststrap', __FILE__)
2
- require File.expand_path('../../lib/rabl', __FILE__)
3
- require File.expand_path('../../lib/rabl/template', __FILE__)
4
- require File.expand_path('../models/user', __FILE__)
3
+ require 'rabl/template'
5
4
  require File.expand_path('../models/ormless', __FILE__)
6
5
 
7
6
  context "Rabl::Engine" do
@@ -173,8 +172,8 @@ context "Rabl::Engine" do
173
172
  }
174
173
  scope = Object.new
175
174
  scope.instance_variable_set :@user, User.new(:name => 'irvine')
176
- template.render(scope).split('').sort
177
- end.equals "{\"user\":{\"name\":\"irvine\"}}".split('').sort
175
+ JSON.parse(template.render(scope))
176
+ end.equals JSON.parse("{\"user\":{\"name\":\"irvine\"}}")
178
177
 
179
178
  asserts "that it can add attribute under a different key name through :as" do
180
179
  template = rabl %{
@@ -183,8 +182,8 @@ context "Rabl::Engine" do
183
182
  }
184
183
  scope = Object.new
185
184
  scope.instance_variable_set :@user, User.new(:name => 'irvine')
186
- template.render(scope).split('').sort
187
- end.equals "{\"user\":{\"city\":\"irvine\"}}".split('').sort
185
+ JSON.parse(template.render(scope))
186
+ end.equals JSON.parse("{\"user\":{\"city\":\"irvine\"}}")
188
187
 
189
188
  asserts "that it can add attribute under a different key name through hash" do
190
189
  template = rabl %{
@@ -193,8 +192,8 @@ context "Rabl::Engine" do
193
192
  }
194
193
  scope = Object.new
195
194
  scope.instance_variable_set :@user, User.new(:name => 'irvine')
196
- template.render(scope).split('').sort
197
- end.equals "{\"user\":{\"city\":\"irvine\"}}".split('').sort
195
+ JSON.parse(template.render(scope))
196
+ end.equals JSON.parse("{\"user\":{\"city\":\"irvine\"}}")
198
197
  end
199
198
 
200
199
  context "#code" do
@@ -202,15 +201,15 @@ context "Rabl::Engine" do
202
201
  template = rabl %{
203
202
  code(:foo) { 'bar' }
204
203
  }
205
- template.render(Object.new).split('').sort
206
- end.equals "{\"foo\":\"bar\"}".split('').sort
204
+ template.render(Object.new)
205
+ end.equals "{\"foo\":\"bar\"}"
207
206
 
208
207
  asserts "that it can be passed conditionals" do
209
208
  template = rabl %{
210
209
  code(:foo, :if => lambda { |i| false }) { 'bar' }
211
210
  }
212
- template.render(Object.new).split('').sort
213
- end.equals "{}".split('').sort
211
+ template.render(Object.new)
212
+ end.equals "{}"
214
213
 
215
214
  asserts "that it can merge the result with a collection element given no name" do
216
215
  template = rabl %{
@@ -221,8 +220,8 @@ context "Rabl::Engine" do
221
220
  }
222
221
  scope = Object.new
223
222
  scope.instance_variable_set :@users, [User.new(:name => 'a'), User.new(:name => 'b')]
224
- template.render(scope).split('').sort
225
- end.equals "[{\"user\":{\"name\":\"a\"}},{\"user\":{\"name\":\"b\"}}]".split('').sort
223
+ JSON.parse(template.render(scope))
224
+ end.equals JSON.parse("[{\"user\":{\"name\":\"a\"}},{\"user\":{\"name\":\"b\"}}]")
226
225
 
227
226
  asserts "that it can merge the result on a child node given no name" do
228
227
  template = rabl %{
@@ -236,8 +235,8 @@ context "Rabl::Engine" do
236
235
  }
237
236
  scope = Object.new
238
237
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
239
- template.render(scope).split('').sort
240
- end.equals "{\"user\":{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}}".split('').sort
238
+ JSON.parse(template.render(scope))
239
+ end.equals JSON.parse("{\"user\":{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}}")
241
240
  end
242
241
 
243
242
  context "#child" do
@@ -249,8 +248,8 @@ context "Rabl::Engine" do
249
248
  }
250
249
  scope = Object.new
251
250
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
252
- template.render(scope).split('').sort
253
- end.equals "{\"user\":{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}}".split('').sort
251
+ JSON.parse(template.render(scope))
252
+ end.equals JSON.parse("{\"user\":{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}}")
254
253
 
255
254
  asserts "that it can create a child node with different key" do
256
255
  template = rabl %{
@@ -260,9 +259,8 @@ context "Rabl::Engine" do
260
259
  }
261
260
  scope = Object.new
262
261
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
263
- template.render(scope).split('').sort
264
-
265
- end.equals "{\"user\":{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}}".split('').sort
262
+ JSON.parse(template.render(scope))
263
+ end.equals JSON.parse("{\"user\":{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}}")
266
264
 
267
265
  asserts "that it passes the data object to the block" do
268
266
  template = rabl %{
@@ -287,8 +285,8 @@ context "Rabl::Engine" do
287
285
  }
288
286
  scope = Object.new
289
287
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
290
- template.render(scope).split('').sort
291
- end.equals "{\"user\":{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}}".split('').sort
288
+ JSON.parse(template.render(scope))
289
+ end.equals JSON.parse("{\"user\":{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}}")
292
290
 
293
291
  asserts "that it passes the data object to the block" do
294
292
  template = rabl %{
@@ -451,8 +449,8 @@ context "Rabl::Engine" do
451
449
  }
452
450
  scope = Object.new
453
451
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
454
- template.render(scope).split('').sort
455
- end.equals "{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}".split('').sort
452
+ JSON.parse(template.render(scope))
453
+ end.equals JSON.parse("{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}")
456
454
 
457
455
  asserts "that it can create a child node with different key" do
458
456
  template = rabl %{
@@ -462,9 +460,8 @@ context "Rabl::Engine" do
462
460
  }
463
461
  scope = Object.new
464
462
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
465
- template.render(scope).split('').sort
466
-
467
- end.equals "{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}".split('').sort
463
+ JSON.parse(template.render(scope))
464
+ end.equals JSON.parse("{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}")
468
465
 
469
466
  asserts "that it can be passed conditionals" do
470
467
  template = rabl %{
@@ -474,9 +471,8 @@ context "Rabl::Engine" do
474
471
  }
475
472
  scope = Object.new
476
473
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
477
- template.render(scope).split('').sort
478
-
479
- end.equals "{\"name\":\"leo\"}".split('').sort
474
+ template.render(scope)
475
+ end.equals "{\"name\":\"leo\"}"
480
476
  end
481
477
 
482
478
  context "#glue" do
@@ -489,8 +485,8 @@ context "Rabl::Engine" do
489
485
  }
490
486
  scope = Object.new
491
487
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
492
- template.render(scope).split('').sort
493
- end.equals "{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}".split('').sort
488
+ JSON.parse(template.render(scope))
489
+ end.equals JSON.parse("{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}")
494
490
  end
495
491
 
496
492
  teardown do
@@ -508,7 +504,6 @@ context "Rabl::Engine" do
508
504
  end
509
505
 
510
506
  context "#child" do
511
-
512
507
  asserts "that it can create a child node without child root" do
513
508
  template = rabl %{
514
509
  child @users
@@ -517,7 +512,6 @@ context "Rabl::Engine" do
517
512
  scope.instance_variable_set :@users, [User.new, User.new]
518
513
  template.render(scope)
519
514
  end.equals "{\"users\":[{},{}]}"
520
-
521
515
  end
522
516
 
523
517
  teardown do
data/test/helpers_test.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'tmpdir'
2
2
  require 'pathname'
3
3
  require File.expand_path('../teststrap', __FILE__)
4
- require File.expand_path('../../lib/rabl', __FILE__)
5
4
 
6
5
  class TestHelperMethods
7
6
  include Rabl::Helpers
@@ -134,6 +134,7 @@ context "PostsController" do
134
134
  end # date node
135
135
 
136
136
  asserts("contains helper action") { topic["foo"] }.equals { "BAR!" }
137
+ denies("contains helper action") { topic["created_at_in_words"] }.nil
137
138
 
138
139
  asserts("contains post attributes via node") { topic["post"] }.equals { [@post1.title, @post1.body] }
139
140
  end # show action, json
@@ -1,2 +1,3 @@
1
1
  class Ormless
2
+ # Nothing here
2
3
  end
data/test/models/user.rb CHANGED
@@ -1,22 +1,26 @@
1
- class User
2
- attr_accessor :age, :city, :name, :first
1
+ unless defined?(User)
2
+ class User
3
+ attr_accessor :age, :city, :name, :first
3
4
 
4
- DEFAULT_AGE = 24
5
- DEFAULT_CITY = 'irvine'
6
- DEFAULT_NAME = 'rabl'
7
- DEFAULT_FIRST = 'bob'
5
+ DEFAULT_AGE = 24
6
+ DEFAULT_CITY = 'irvine'
7
+ DEFAULT_NAME = 'rabl'
8
+ DEFAULT_FIRST = 'bob'
8
9
 
9
- def initialize(attributes={})
10
- self.age = attributes[:age] || DEFAULT_AGE
11
- self.city = attributes[:city] || DEFAULT_CITY
12
- self.name = attributes[:name] || DEFAULT_NAME
13
- self.first = attributes[:first] || DEFAULT_FIRST
10
+ def initialize(attributes={})
11
+ self.age = attributes[:age] || DEFAULT_AGE
12
+ self.city = attributes[:city] || DEFAULT_CITY
13
+ self.name = attributes[:name] || DEFAULT_NAME
14
+ self.first = attributes[:first] || DEFAULT_FIRST
15
+ end
14
16
  end
15
17
  end
16
18
 
17
- module NestedScope
18
- class User
19
- def controller; self; end
20
- def controller_name; self.class.name.downcase; end
19
+ unless defined?(NestedScope::User)
20
+ module NestedScope
21
+ class User
22
+ def controller; self; end
23
+ def controller_name; self.class.name.downcase; end
24
+ end
21
25
  end
22
26
  end
@@ -1,7 +1,5 @@
1
1
  require File.expand_path('../teststrap', __FILE__)
2
- require File.expand_path('../../lib/rabl', __FILE__)
3
- require File.expand_path('../../lib/rabl/template', __FILE__)
4
- require File.expand_path('../models/user', __FILE__)
2
+ require 'rabl/template'
5
3
 
6
4
  context "Rabl::Engine" do
7
5
  helper(:rabl) { |t| RablTemplate.new("code", :format => 'msgpack') { t } }
@@ -1,7 +1,6 @@
1
1
  require 'tmpdir'
2
2
  require 'pathname'
3
3
  require File.expand_path('../teststrap', __FILE__)
4
- require File.expand_path('../../lib/rabl', __FILE__)
5
4
 
6
5
  class TestPartial
7
6
  include Rabl::Partials
@@ -1,7 +1,5 @@
1
1
  require File.expand_path('../teststrap', __FILE__)
2
- require File.expand_path('../../lib/rabl', __FILE__)
3
- require File.expand_path('../../lib/rabl/template', __FILE__)
4
- require File.expand_path('../models/user', __FILE__)
2
+ require 'rabl/template'
5
3
 
6
4
  context "Rabl::Engine" do
7
5
  helper(:rabl) { |t| RablTemplate.new("code", :format => 'plist') { t } }
@@ -1,7 +1,7 @@
1
1
  require 'tmpdir'
2
2
  require 'pathname'
3
+ require 'json'
3
4
  require File.expand_path('../teststrap', __FILE__)
4
- require File.expand_path('../../lib/rabl', __FILE__)
5
5
 
6
6
  context "Rabl::Renderer" do
7
7
  helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
@@ -29,8 +29,8 @@ context "Rabl::Renderer" do
29
29
  user = User.new(:name => 'irvine')
30
30
 
31
31
  renderer = Rabl::Renderer.new(source, user, { :format => 'json', :root => true, :view_path => '/path/to/views' })
32
- renderer.render
33
- end.equals "{\"user\":{\"city\":\"irvine\"}}"
32
+ JSON.parse(renderer.render)
33
+ end.equals JSON.parse("{\"user\":{\"city\":\"irvine\"}}")
34
34
 
35
35
  asserts 'allows redirecting scope to another object' do
36
36
  source = %q{
@@ -42,8 +42,8 @@ context "Rabl::Renderer" do
42
42
  scope.instance_variable_set :@user, User.new(:name => 'irvine')
43
43
 
44
44
  renderer = Rabl::Renderer.new(source, nil, { :format => 'json', :scope => scope })
45
- renderer.render
46
- end.equals "{\"user\":{\"city\":\"irvine\"}}"
45
+ JSON.parse(renderer.render)
46
+ end.equals JSON.parse("{\"user\":{\"city\":\"irvine\"}}")
47
47
 
48
48
  asserts 'accepts scope on render' do
49
49
  source = %q{
@@ -55,8 +55,8 @@ context "Rabl::Renderer" do
55
55
  scope.instance_variable_set :@user, User.new(:name => 'irvine')
56
56
 
57
57
  renderer = Rabl::Renderer.new(source)
58
- renderer.render(scope)
59
- end.equals "{\"user\":{\"city\":\"irvine\"}}"
58
+ JSON.parse(renderer.render(scope))
59
+ end.equals JSON.parse("{\"user\":{\"city\":\"irvine\"}}")
60
60
 
61
61
  asserts 'passes :locals to render' do
62
62
  source = %q{
@@ -67,8 +67,8 @@ context "Rabl::Renderer" do
67
67
  user = User.new(:name => 'irvine')
68
68
 
69
69
  renderer = Rabl::Renderer.new(source, nil, { :format => 'json', :locals => {:object => user, :zipcode => "92602"} })
70
- renderer.render.split("").sort
71
- end.equals "{\"user\":{\"city\":\"irvine\",\"zipcode\":\"92602\"}}".split("").sort
70
+ JSON.parse(renderer.render)
71
+ end.equals JSON.parse("{\"user\":{\"city\":\"irvine\",\"zipcode\":\"92602\"}}")
72
72
 
73
73
  asserts 'loads source from file' do
74
74
  File.open(tmp_path + "test.json.rabl", "w") do |f|
@@ -81,8 +81,8 @@ context "Rabl::Renderer" do
81
81
  user = User.new(:name => 'irvine')
82
82
 
83
83
  renderer = Rabl::Renderer.new('test', user, :view_path => tmp_path)
84
- renderer.render.split("").sort
85
- end.equals "{\"user\":{\"age\":24,\"name\":\"irvine\"}}".split("").sort
84
+ JSON.parse(renderer.render)
85
+ end.equals JSON.parse("{\"user\":{\"age\":24,\"name\":\"irvine\"}}")
86
86
 
87
87
  asserts 'uses globally configured view paths' do
88
88
  Rabl.configure do |config|
@@ -98,9 +98,8 @@ context "Rabl::Renderer" do
98
98
  user = User.new(:name => 'irvine')
99
99
 
100
100
  renderer = Rabl::Renderer.new('test', user)
101
- renderer.render.split("").sort
102
- end.equals "{\"user\":{\"age\":24,\"name\":\"irvine\"}}".split("").sort
103
-
101
+ JSON.parse(renderer.render)
102
+ end.equals JSON.parse("{\"user\":{\"age\":24,\"name\":\"irvine\"}}")
104
103
 
105
104
  asserts 'handles paths for extends' do
106
105
  File.open(tmp_path + "test.json.rabl", "w") do |f|
@@ -120,8 +119,30 @@ context "Rabl::Renderer" do
120
119
  user = User.new(:name => 'irvine')
121
120
 
122
121
  renderer = Rabl::Renderer.new('user', user, :view_path => tmp_path)
123
- renderer.render.split("").sort
124
- end.equals "{\"user\":{\"age\":24,\"name\":\"irvine\"}}".split("").sort
122
+ JSON.parse(renderer.render)
123
+ end.equals JSON.parse("{\"user\":{\"age\":24,\"name\":\"irvine\"}}")
124
+
125
+ # FIXME template is found and rendered but not included in final results
126
+ # asserts 'handles paths for partial' do
127
+ # File.open(tmp_path + "test.json.rabl", "w") do |f|
128
+ # f.puts %q{
129
+ # attributes :age
130
+ # }
131
+ # end
132
+
133
+ # File.open(tmp_path + "user.json.rabl", "w") do |f|
134
+ # f.puts %(
135
+ # object @user
136
+ # attribute :name
137
+ # partial 'test', :object => @user
138
+ # )
139
+ # end
140
+
141
+ # user = User.new(:name => 'irvine')
142
+
143
+ # renderer = Rabl::Renderer.new('user', user, :view_path => tmp_path)
144
+ # JSON.parse(renderer.render)
145
+ # end.equals JSON.parse("{\"user\":{\"age\":24,\"name\":\"irvine\"}}")
125
146
 
126
147
  asserts 'Rabl.render calls Renderer' do
127
148
  File.open(tmp_path + "test.json.rabl", "w") do |f|
@@ -133,8 +154,8 @@ context "Rabl::Renderer" do
133
154
 
134
155
  user = User.new(:name => 'irvine')
135
156
 
136
- Rabl.render(user, 'test', :view_path => tmp_path).split("").sort
137
- end.equals "{\"user\":{\"age\":24,\"name\":\"irvine\"}}".split("").sort
157
+ JSON.parse(Rabl.render(user, 'test', :view_path => tmp_path))
158
+ end.equals JSON.parse("{\"user\":{\"age\":24,\"name\":\"irvine\"}}")
138
159
 
139
160
  asserts 'it renders collections' do
140
161
  File.open(tmp_path + "test.json.rabl", "w") do |f|
@@ -146,8 +167,8 @@ context "Rabl::Renderer" do
146
167
 
147
168
  scope = Object.new
148
169
  scope.instance_variable_set :@users, nil
149
- Rabl.render([], 'test', :view_path => tmp_path, :scope => scope).split("").sort
150
- end.equals "{\"users\":[]}".split("").sort
170
+ Rabl.render([], 'test', :view_path => tmp_path, :scope => scope)
171
+ end.equals "{\"users\":[]}"
151
172
 
152
173
  asserts 'it renders an array when given an empty collection' do
153
174
  File.open(tmp_path + "test.json.rabl", "w") do |f|
@@ -188,8 +209,8 @@ context "Rabl::Renderer" do
188
209
  stub(user).profile { stub!.gender { "male" } }
189
210
 
190
211
  renderer = Rabl::Renderer.new('user', user, :view_path => tmp_path)
191
- renderer.render.split("").sort
192
- end.equals "{\"user\":{\"name\":\"irvine\",\"object\":{\"gender\":\"male\"},\"gender\":\"male\"}}".split("").sort
212
+ JSON.parse(renderer.render)
213
+ end.equals JSON.parse("{\"user\":{\"name\":\"irvine\",\"object\":{\"gender\":\"male\"},\"gender\":\"male\"}}")
193
214
  end
194
215
 
195
216
  context '.json' do
@@ -202,8 +223,8 @@ context "Rabl::Renderer" do
202
223
  end
203
224
 
204
225
  user = User.new(:name => 'ivan')
205
- Rabl::Renderer.json(user, 'test', :view_path => tmp_path).split("").sort
206
- end.equals "{\"user\":{\"age\":24,\"name\":\"ivan\"}}".split("").sort
226
+ JSON.parse(Rabl::Renderer.json(user, 'test', :view_path => tmp_path))
227
+ end.equals JSON.parse("{\"user\":{\"age\":24,\"name\":\"ivan\"}}")
207
228
  end
208
229
 
209
230
  context '.msgpack' do
@@ -216,8 +237,8 @@ context "Rabl::Renderer" do
216
237
  end
217
238
 
218
239
  user = User.new(:name => 'ivan')
219
- Rabl::Renderer.msgpack(user, 'test', :view_path => tmp_path).split("").sort
220
- end.equals "\x81\xA4user\x82\xA3age\x18\xA4name\xA4ivan".split("").sort
240
+ Rabl::Renderer.msgpack(user, 'test', :view_path => tmp_path)
241
+ end.equals "\x81\xA4user\x82\xA3age\x18\xA4name\xA4ivan"
221
242
  end
222
243
 
223
244
  context '.plist' do
@@ -1,5 +1,5 @@
1
1
  require File.expand_path('../teststrap',__FILE__)
2
- require File.expand_path('../../lib/rabl/template', __FILE__)
2
+ require 'rabl/template'
3
3
 
4
4
  class Scope
5
5
  end
data/test/teststrap.rb CHANGED
@@ -1,3 +1,5 @@
1
+ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
2
+
1
3
  module Kernel
2
4
  def silence_warnings
3
5
  with_warnings(nil) { yield }
@@ -15,7 +17,8 @@ silence_warnings do
15
17
  require 'riot'
16
18
  require 'riot/rr'
17
19
  require 'tilt'
18
- require File.expand_path('../../lib/rabl',__FILE__)
20
+ require 'rabl'
21
+ require File.expand_path('../models/user', __FILE__)
19
22
  end
20
23
 
21
24
  Riot.pretty_dots
data/test/xml_test.rb ADDED
@@ -0,0 +1,287 @@
1
+ require 'active_support/core_ext/hash/conversions'
2
+ require File.expand_path('../teststrap', __FILE__)
3
+ require 'rabl/template'
4
+
5
+ context "Rabl::Engine" do
6
+ helper(:rabl) { |t| RablTemplate.new("code", :format => 'xml') { t } }
7
+
8
+ # TODO fix annoying warnings in this file:
9
+ # gems/builder-3.0.3/lib/builder/xmlbase.rb:181: warning: method redefined; discarding old user
10
+ setup { @old_verbose, $VERBOSE = $VERBOSE, nil }
11
+ teardown { $VERBOSE = @old_verbose }
12
+
13
+ context "with xml defaults" do
14
+ setup do
15
+ Rabl.configure do |config|
16
+ # Comment this line out because include_xml_root to false is default.
17
+ #config.include_xml_root = false
18
+ end
19
+ end
20
+
21
+ context "#object" do
22
+ asserts "that it sets data source" do
23
+ template = rabl %q{
24
+ object @user
25
+ }
26
+ scope = Object.new
27
+ scope.instance_variable_set :@user, User.new
28
+ template.render(scope)
29
+ end.matches ""
30
+
31
+ asserts "that it can set root node" do
32
+ template = rabl %q{
33
+ object @user => :person
34
+ }
35
+ scope = Object.new
36
+ scope.instance_variable_set :@user, User.new
37
+ template.render(scope)
38
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n</person>\n"
39
+ end
40
+
41
+ context "#collection" do
42
+ asserts "that it sets object to be casted as a simple array" do
43
+ template = rabl %{
44
+ collection @users
45
+ }
46
+ scope = Object.new
47
+ scope.instance_variable_set :@users, [User.new, User.new]
48
+ template.render(scope)
49
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<users type=\"array\">\n <user>\n </user>\n <user>\n </user>\n</users>\n"
50
+
51
+ asserts "that it sets root node for objects" do
52
+ template = rabl %{
53
+ collection @users => :person
54
+ }
55
+ scope = Object.new
56
+ scope.instance_variable_set :@users, [User.new, User.new]
57
+ template.render(scope)
58
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<people type=\"array\">\n <person>\n </person>\n <person>\n </person>\n</people>\n"
59
+ end
60
+
61
+ context "#attribute" do
62
+ asserts "that it adds an attribute or method to be included in output" do
63
+ template = rabl %{
64
+ object @user
65
+ attribute :name
66
+ }
67
+ scope = Object.new
68
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
69
+ template.render(scope)
70
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <name>irvine</name>\n</user>\n"
71
+
72
+ asserts "that it can add attribute under a different key name through :as" do
73
+ template = rabl %{
74
+ object @user
75
+ attribute :name, :as => 'city'
76
+ }
77
+ scope = Object.new
78
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
79
+ template.render(scope)
80
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <city>irvine</city>\n</user>\n"
81
+
82
+ asserts "that it can add attribute under a different key name through hash" do
83
+ template = rabl %{
84
+ object @user
85
+ attribute :name => :city
86
+ }
87
+ scope = Object.new
88
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
89
+ template.render(scope)
90
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <city>irvine</city>\n</user>\n"
91
+ end
92
+
93
+ context "#code" do
94
+ asserts "that it can create an arbitraty code node" do
95
+ template = rabl %{
96
+ code(:foo) { 'bar' }
97
+ }
98
+ template.render(Object.new)
99
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <foo>bar</foo>\n</hash>\n"
100
+
101
+ asserts "that it can be passed conditionals" do
102
+ template = rabl %{
103
+ code(:foo, :if => lambda { |i| false }) { 'bar' }
104
+ }
105
+ template.render(Object.new)
106
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n</hash>\n"
107
+ end
108
+
109
+ context "#child" do
110
+ asserts "that it can create a child node" do
111
+ template = rabl %{
112
+ object @user
113
+ child(@user) { attribute :city }
114
+ }
115
+ scope = Object.new
116
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
117
+ template.render(scope)
118
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <user>\n <city>LA</city>\n </user>\n</user>\n"
119
+
120
+ asserts "that it can create a child node with different key" do
121
+ template = rabl %{
122
+ object @user
123
+ attribute :name
124
+ child(@user => :person) { attribute :city }
125
+ }
126
+ scope = Object.new
127
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
128
+ template.render(scope)
129
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <name>leo</name>\n <person>\n <city>LA</city>\n </person>\n</user>\n"
130
+ end
131
+
132
+ context "#glue" do
133
+ asserts "that it glues data from a child node" do
134
+ template = rabl %{
135
+ object @user
136
+ attribute :name
137
+ glue(@user) { attribute :city }
138
+ glue(@user) { attribute :age }
139
+ }
140
+ scope = Object.new
141
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
142
+ template.render(scope)
143
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <name>leo</name>\n <city>LA</city>\n <age type=\"integer\">12</age>\n</user>\n"
144
+ end
145
+
146
+ teardown do
147
+ Rabl.reset_configuration!
148
+ end
149
+ end
150
+
151
+ context "with xml root" do
152
+ setup do
153
+ Rabl.configure do |config|
154
+ config.include_xml_root = true
155
+ end
156
+ end
157
+
158
+ context "#object" do
159
+ asserts "that it sets data source" do
160
+ template = rabl %q{
161
+ object @user
162
+ }
163
+ scope = Object.new
164
+ scope.instance_variable_set :@user, User.new
165
+ template.render(scope)
166
+ end.matches "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <user>\n </user>\n</user>\n"
167
+
168
+ asserts "that it can set root node" do
169
+ template = rabl %q{
170
+ object @user => :person
171
+ }
172
+ scope = Object.new
173
+ scope.instance_variable_set :@user, User.new
174
+ template.render(scope)
175
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <person>\n </person>\n</person>\n"
176
+ end
177
+
178
+ context "#collection" do
179
+ asserts "that it sets object to be casted as a simple array" do
180
+ template = rabl %{
181
+ collection @users
182
+ }
183
+ scope = Object.new
184
+ scope.instance_variable_set :@users, [User.new, User.new]
185
+ template.render(scope)
186
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<users type=\"array\">\n <user>\n <user>\n </user>\n </user>\n <user>\n <user>\n </user>\n </user>\n</users>\n"
187
+
188
+ asserts "that it sets root node for objects" do
189
+ template = rabl %{
190
+ collection @users => :person
191
+ }
192
+ scope = Object.new
193
+ scope.instance_variable_set :@users, [User.new, User.new]
194
+ template.render(scope)
195
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<people type=\"array\">\n <person>\n <person>\n </person>\n </person>\n <person>\n <person>\n </person>\n </person>\n</people>\n"
196
+ end
197
+
198
+ context "#attribute" do
199
+ asserts "that it adds an attribute or method to be included in output" do
200
+ template = rabl %{
201
+ object @user
202
+ attribute :name
203
+ }
204
+ scope = Object.new
205
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
206
+ template.render(scope)
207
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <user>\n <name>irvine</name>\n </user>\n</user>\n"
208
+
209
+ asserts "that it can add attribute under a different key name through :as" do
210
+ template = rabl %{
211
+ object @user
212
+ attribute :name, :as => 'city'
213
+ }
214
+ scope = Object.new
215
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
216
+ template.render(scope)
217
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <user>\n <city>irvine</city>\n </user>\n</user>\n"
218
+
219
+ asserts "that it can add attribute under a different key name through hash" do
220
+ template = rabl %{
221
+ object @user
222
+ attribute :name => :city
223
+ }
224
+ scope = Object.new
225
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
226
+ template.render(scope)
227
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <user>\n <city>irvine</city>\n </user>\n</user>\n"
228
+ end
229
+
230
+ context "#code" do
231
+ asserts "that it can create an arbitrary code node" do
232
+ template = rabl %{
233
+ code(:foo) { 'bar' }
234
+ }
235
+ template.render(Object.new)
236
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <foo>bar</foo>\n</hash>\n"
237
+
238
+ asserts "that it can be passed conditionals" do
239
+ template = rabl %{
240
+ code(:foo, :if => lambda { |i| false }) { 'bar' }
241
+ }
242
+ template.render(Object.new)
243
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n</hash>\n"
244
+ end
245
+
246
+ context "#child" do
247
+ asserts "that it can create a child node" do
248
+ template = rabl %{
249
+ object @user
250
+ child(@user) { attribute :city }
251
+ }
252
+ scope = Object.new
253
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
254
+ template.render(scope)
255
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <user>\n <user>\n <city>LA</city>\n </user>\n </user>\n</user>\n"
256
+
257
+ asserts "that it can create a child node with different key" do
258
+ template = rabl %{
259
+ object @user
260
+ attribute :name
261
+ child(@user => :person) { attribute :city }
262
+ }
263
+ scope = Object.new
264
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
265
+ template.render(scope)
266
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <user>\n <name>leo</name>\n <person>\n <city>LA</city>\n </person>\n </user>\n</user>\n"
267
+ end
268
+
269
+ context "#glue" do
270
+ asserts "that it glues data from a child node" do
271
+ template = rabl %{
272
+ object @user
273
+ attribute :name
274
+ glue(@user) { attribute :city }
275
+ glue(@user) { attribute :age }
276
+ }
277
+ scope = Object.new
278
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
279
+ template.render(scope)
280
+ end.equals "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n <user>\n <name>leo</name>\n <city>LA</city>\n <age type=\"integer\">12</age>\n </user>\n</user>\n"
281
+ end
282
+
283
+ teardown do
284
+ Rabl.reset_configuration!
285
+ end
286
+ end
287
+ end
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.1
4
+ version: 0.7.2
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-08-07 00:00:00.000000000 Z
12
+ date: 2012-09-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -146,7 +146,7 @@ dependencies:
146
146
  requirements:
147
147
  - - ~>
148
148
  - !ruby/object:Gem::Version
149
- version: 1.5.2
149
+ version: 1.7.0
150
150
  type: :development
151
151
  prerelease: false
152
152
  version_requirements: !ruby/object:Gem::Requirement
@@ -154,7 +154,7 @@ dependencies:
154
154
  requirements:
155
155
  - - ~>
156
156
  - !ruby/object:Gem::Version
157
- version: 1.5.2
157
+ version: 1.7.0
158
158
  - !ruby/object:Gem::Dependency
159
159
  name: plist
160
160
  requirement: !ruby/object:Gem::Requirement
@@ -398,6 +398,7 @@ files:
398
398
  - lib/rabl/configuration.rb
399
399
  - lib/rabl/engine.rb
400
400
  - lib/rabl/helpers.rb
401
+ - lib/rabl/json_engine.rb
401
402
  - lib/rabl/partials.rb
402
403
  - lib/rabl/railtie.rb
403
404
  - lib/rabl/renderer.rb
@@ -424,6 +425,7 @@ files:
424
425
  - test/silence.rb
425
426
  - test/template_test.rb
426
427
  - test/teststrap.rb
428
+ - test/xml_test.rb
427
429
  homepage: https://github.com/nesquena/rabl
428
430
  licenses: []
429
431
  post_install_message:
@@ -468,3 +470,4 @@ test_files:
468
470
  - test/silence.rb
469
471
  - test/template_test.rb
470
472
  - test/teststrap.rb
473
+ - test/xml_test.rb