rabl 0.2.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -30,7 +30,7 @@ gem 'rabl'
30
30
 
31
31
  and run `bundle install` to install the dependency.
32
32
 
33
- If you are using **Rails 2.X, Rails 3 or Padrino**, RABL works without configuration.
33
+ If you are using **Rails 2.X, Rails 3, Rails 3.X or Padrino**, RABL works without configuration.
34
34
 
35
35
  With Sinatra, or any other tilt-based framework, simply register:
36
36
 
@@ -86,6 +86,7 @@ RABL is intended to require little to no configuration to get working. This is t
86
86
  # config/initializers/rabl_init.rb
87
87
  Rabl.configure do |config|
88
88
  # Commented as these are the defaults
89
+ # config.json_engine = nil # Any multi\_json engines
89
90
  # config.include_json_root = true
90
91
  # config.include_xml_root = false
91
92
  # config.enable_json_callbacks = false
@@ -95,6 +96,15 @@ end
95
96
 
96
97
  Each option specifies behavior related to RABL's output. If `include_json_root` is disabled that removes the root node for each child in the output, and `enable_json_callbacks` enables support for 'jsonp' style callback output if the incoming request has a 'callback' parameter.
97
98
 
99
+ Note that the `json_engine` option uses the [multi_json](http://intridea.com/2010/6/14/multi-json-the-swappable-json-handler) intelligent engine defaults so in most cases you **don't need to configure this** directly. If you wish to use yajl as the primary JSON encoding engine simply add that to your Gemfile:
100
+
101
+ ```ruby
102
+ # Gemfile
103
+ gem 'yajl-ruby', :require => "yajl"
104
+ ```
105
+
106
+ and RABL will automatically start using that engine for encoding your JSON responses!
107
+
98
108
  ## Usage ##
99
109
 
100
110
  ### Object Assignment ###
@@ -327,6 +337,7 @@ Thanks to [Miso](http://gomiso.com) for allowing me to create this for our appli
327
337
  * [Rick Thomas](https://github.com/rickthomasjr) - Added options passing for extends and Sinatra testing
328
338
  * [Marjun](https://github.com/mpagalan) - Added xml option configurations
329
339
  * [Chris Kimpton](https://github.com/kimptoc) - Helping with documentation and wiki
340
+ * [Sasha Koss](https://github.com/kossnocorp) - Added multi_json support
330
341
 
331
342
  More to come hopefully! Please fork and contribute, any help is appreciated!
332
343
 
data/TODO CHANGED
@@ -1,3 +1 @@
1
- = TODO
2
-
3
- * Support xml and json rendering in Tilt template
1
+ = TODO
@@ -4,6 +4,7 @@ module Rabl
4
4
  attr_accessor :include_json_root
5
5
  attr_accessor :include_xml_root
6
6
  attr_accessor :enable_json_callbacks
7
+ attr_accessor :json_engine
7
8
  attr_writer :xml_options
8
9
 
9
10
  DEFAULT_XML_OPTIONS = { :dasherize => true, :skip_types => false }
@@ -12,6 +13,7 @@ module Rabl
12
13
  @include_json_root = true
13
14
  @include_xml_root = false
14
15
  @enable_json_callbacks = false
16
+ @json_engine = nil
15
17
  @xml_options = {}
16
18
  end
17
19
 
data/lib/rabl/engine.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'multi_json'
2
+
1
3
  module Rabl
2
4
  class Engine
3
5
  include Rabl::Helpers
@@ -7,6 +9,10 @@ module Rabl
7
9
  def initialize(source, options={})
8
10
  @_source = source
9
11
  @_options = options
12
+
13
+ if Rabl.configuration.json_engine
14
+ MultiJson.engine = Rabl.configuration.json_engine
15
+ end
10
16
  end
11
17
 
12
18
  # Renders the representation based on source, object, scope and locals
@@ -41,7 +47,7 @@ module Rabl
41
47
  include_root = Rabl.configuration.include_json_root
42
48
  options = options.reverse_merge(:root => include_root, :child_root => include_root)
43
49
  result = @_collection_name ? { @_collection_name => to_hash(options) } : to_hash(options)
44
- format_json(result.to_json)
50
+ format_json MultiJson.encode(result)
45
51
  end
46
52
 
47
53
  # Returns an xml representation of the data object
data/lib/rabl/template.rb CHANGED
@@ -63,5 +63,5 @@ if defined?(Rails) && Rails.version =~ /^3/
63
63
  end # handlers
64
64
  end
65
65
 
66
- ActionView::Template.register_template_handler :rabl, ActionView::TemplateHandlers::Rabl
66
+ ActionView::Template.register_template_handler :rabl, ActionView::Template::Handlers::Rabl
67
67
  end
data/lib/rabl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.2.8"
2
+ VERSION = "0.3.0"
3
3
  end
data/rabl.gemspec CHANGED
@@ -19,9 +19,12 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
+ s.add_dependency 'multi_json', '~> 1.0.3'
23
+
22
24
  s.add_development_dependency 'riot', '~>0.12.3'
23
25
  s.add_development_dependency 'rr', '~>1.0.2'
24
26
  s.add_development_dependency 'mongoid'
25
27
  s.add_development_dependency 'tilt'
26
28
  s.add_development_dependency 'bson_ext'
29
+ s.add_development_dependency 'yajl-ruby'
27
30
  end
@@ -9,6 +9,7 @@ context "Rabl::Configuration" do
9
9
  asserts(:include_json_root).equals true
10
10
  asserts(:include_xml_root).equals false
11
11
  asserts(:enable_json_callbacks).equals false
12
+ asserts(:json_engine).equals nil
12
13
  end
13
14
 
14
15
  context "with configuration" do
@@ -17,6 +18,7 @@ context "Rabl::Configuration" do
17
18
  config.include_json_root = false
18
19
  config.include_xml_root = true
19
20
  config.enable_json_callbacks = true
21
+ config.json_engine = :yajl
20
22
  end
21
23
  Rabl.configuration
22
24
  end
@@ -24,6 +26,13 @@ context "Rabl::Configuration" do
24
26
  asserts(:include_json_root).equals false
25
27
  asserts(:include_xml_root).equals true
26
28
  asserts(:enable_json_callbacks).equals true
29
+ asserts(:json_engine).equals :yajl
30
+
31
+ teardown do
32
+ Rabl.configure do |config|
33
+ config.json_engine = MultiJson.default_engine
34
+ end
35
+ end
27
36
  end
28
37
 
29
38
  end
data/test/engine_test.rb CHANGED
@@ -131,8 +131,8 @@ context "Rabl::Engine" do
131
131
  }
132
132
  scope = Object.new
133
133
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
134
- template.render(scope)
135
- end.equals "{\"user\":{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}}"
134
+ template.render(scope).split('').sort
135
+ end.equals "{\"user\":{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}}".split('').sort
136
136
 
137
137
  asserts "that it can create a child node with different key" do
138
138
  template = rabl %{
@@ -142,9 +142,9 @@ context "Rabl::Engine" do
142
142
  }
143
143
  scope = Object.new
144
144
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
145
- template.render(scope)
145
+ template.render(scope).split('').sort
146
146
 
147
- end.equals "{\"user\":{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}}"
147
+ end.equals "{\"user\":{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}}".split('').sort
148
148
  end
149
149
 
150
150
  context "#glue" do
@@ -158,11 +158,39 @@ context "Rabl::Engine" do
158
158
  }
159
159
  scope = Object.new
160
160
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
161
- template.render(scope)
162
- end.equals "{\"user\":{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}}"
161
+ template.render(scope).split('').sort
162
+ end.equals "{\"user\":{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}}".split('').sort
163
163
  end
164
164
  end
165
165
 
166
+ context "with json_engine" do
167
+ setup do
168
+ class CustomEncodeEngine
169
+ def self.encode string, options = {}
170
+ 42
171
+ end
172
+ end
173
+
174
+ Rabl.configure do |config|
175
+ config.json_engine = CustomEncodeEngine
176
+ end
177
+ end
178
+
179
+ asserts 'that it returns process by custom to_json' do
180
+ template = rabl %q{
181
+ object @user
182
+ }
183
+ scope = Object.new
184
+ scope.instance_variable_set :@user, User.new
185
+ template.render(scope)
186
+ end.equals 42
187
+
188
+ teardown do
189
+ Rabl.configure do |config|
190
+ config.json_engine = MultiJson.default_engine
191
+ end
192
+ end
193
+ end
166
194
 
167
195
  context "without json root" do
168
196
  setup do
@@ -289,9 +317,9 @@ context "Rabl::Engine" do
289
317
  }
290
318
  scope = Object.new
291
319
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
292
- template.render(scope)
320
+ template.render(scope).split('').sort
293
321
 
294
- end.equals "{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}"
322
+ end.equals "{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}".split('').sort
295
323
  end
296
324
 
297
325
  context "#glue" do
@@ -305,8 +333,8 @@ context "Rabl::Engine" do
305
333
  }
306
334
  scope = Object.new
307
335
  scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA', :age => 12)
308
- template.render(scope)
309
- end.equals "{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}"
336
+ template.render(scope).split('').sort
337
+ end.equals "{\"name\":\"leo\",\"city\":\"LA\",\"age\":12}".split('').sort
310
338
  end
311
339
  end
312
340
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rabl
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.8
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nathan Esquenazi
@@ -10,44 +10,43 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-03 00:00:00 -07:00
14
- default_executable:
13
+ date: 2011-07-01 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
- name: riot
16
+ name: multi_json
18
17
  prerelease: false
19
18
  requirement: &id001 !ruby/object:Gem::Requirement
20
19
  none: false
21
20
  requirements:
22
21
  - - ~>
23
22
  - !ruby/object:Gem::Version
24
- version: 0.12.3
25
- type: :development
23
+ version: 1.0.3
24
+ type: :runtime
26
25
  version_requirements: *id001
27
26
  - !ruby/object:Gem::Dependency
28
- name: rr
27
+ name: riot
29
28
  prerelease: false
30
29
  requirement: &id002 !ruby/object:Gem::Requirement
31
30
  none: false
32
31
  requirements:
33
32
  - - ~>
34
33
  - !ruby/object:Gem::Version
35
- version: 1.0.2
34
+ version: 0.12.3
36
35
  type: :development
37
36
  version_requirements: *id002
38
37
  - !ruby/object:Gem::Dependency
39
- name: mongoid
38
+ name: rr
40
39
  prerelease: false
41
40
  requirement: &id003 !ruby/object:Gem::Requirement
42
41
  none: false
43
42
  requirements:
44
- - - ">="
43
+ - - ~>
45
44
  - !ruby/object:Gem::Version
46
- version: "0"
45
+ version: 1.0.2
47
46
  type: :development
48
47
  version_requirements: *id003
49
48
  - !ruby/object:Gem::Dependency
50
- name: tilt
49
+ name: mongoid
51
50
  prerelease: false
52
51
  requirement: &id004 !ruby/object:Gem::Requirement
53
52
  none: false
@@ -58,7 +57,7 @@ dependencies:
58
57
  type: :development
59
58
  version_requirements: *id004
60
59
  - !ruby/object:Gem::Dependency
61
- name: bson_ext
60
+ name: tilt
62
61
  prerelease: false
63
62
  requirement: &id005 !ruby/object:Gem::Requirement
64
63
  none: false
@@ -68,6 +67,28 @@ dependencies:
68
67
  version: "0"
69
68
  type: :development
70
69
  version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: bson_ext
72
+ prerelease: false
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ type: :development
80
+ version_requirements: *id006
81
+ - !ruby/object:Gem::Dependency
82
+ name: yajl-ruby
83
+ prerelease: false
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ type: :development
91
+ version_requirements: *id007
71
92
  description: General ruby templating for json or xml
72
93
  email:
73
94
  - nesquena@gmail.com
@@ -103,7 +124,6 @@ files:
103
124
  - test/models/user.rb
104
125
  - test/template_test.rb
105
126
  - test/teststrap.rb
106
- has_rdoc: true
107
127
  homepage: https://github.com/nesquena/rabl
108
128
  licenses: []
109
129
 
@@ -127,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
147
  requirements: []
128
148
 
129
149
  rubyforge_project: rabl
130
- rubygems_version: 1.6.2
150
+ rubygems_version: 1.7.2
131
151
  signing_key:
132
152
  specification_version: 3
133
153
  summary: General ruby templating for json or xml