rabl 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -326,6 +326,7 @@ Thanks to [Miso](http://gomiso.com) for allowing me to create this for our appli
326
326
  * [Tim Lee](https://github.com/timothy1ee) - RABL is an awesome name and was chosen by the Miso CTO.
327
327
  * [Rick Thomas](https://github.com/rickthomasjr) - Added options passing for extends and Sinatra testing
328
328
  * [Marjun](https://github.com/mpagalan) - Added xml option configurations
329
+ * [Chris Kimpton](https://github.com/kimptoc) - Helping with documentation and wiki
329
330
 
330
331
  More to come hopefully! Please fork and contribute, any help is appreciated!
331
332
 
data/lib/rabl.rb CHANGED
@@ -3,6 +3,7 @@ require 'rabl/helpers'
3
3
  require 'rabl/engine'
4
4
  require 'rabl/builder'
5
5
  require 'rabl/configuration'
6
+ require 'rabl/railtie' if defined?(Rails) && Rails.version =~ /^3/
6
7
 
7
8
  # Rabl.register!
8
9
  module Rabl
@@ -34,6 +35,4 @@ if defined?(Padrino)
34
35
  Padrino.after_load { Rabl.register! }
35
36
  elsif defined?(Rails) && Rails.version =~ /^2/
36
37
  Rabl.register!
37
- elsif defined?(Rails) && Rails.version =~ /^3/
38
- Rabl.register!
39
38
  end
data/lib/rabl/engine.rb CHANGED
@@ -162,5 +162,10 @@ module Rabl
162
162
  def method_missing(name, *args, &block)
163
163
  @_scope.respond_to?(name) ? @_scope.send(name, *args, &block) : super
164
164
  end
165
+
166
+ def copy_instance_variables_from(object, exclude = []) #:nodoc:
167
+ vars = object.instance_variables.map(&:to_s) - exclude.map(&:to_s)
168
+ vars.each { |name| instance_variable_set(name, object.instance_variable_get(name)) }
169
+ end
165
170
  end
166
171
  end
data/lib/rabl/helpers.rb CHANGED
@@ -4,7 +4,7 @@ module Rabl
4
4
  # data_object(@user => :person) => @user
5
5
  # data_object(:user => :person) => @_object.send(:user)
6
6
  def data_object(data)
7
- data = (data.is_a?(Hash) && data.keys.one?) ? data.keys.first : data
7
+ data = (data.is_a?(Hash) && data.keys.size == 1) ? data.keys.first : data
8
8
  data.is_a?(Symbol) && @_object ? @_object.send(data) : data
9
9
  end
10
10
 
@@ -0,0 +1,10 @@
1
+ module Rabl
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer "rabl.initialize" do |app|
5
+ ActiveSupport.on_load(:action_view) do
6
+ Rabl.register!
7
+ end
8
+ end
9
+ end
10
+ end
data/lib/rabl/template.rb CHANGED
@@ -42,23 +42,26 @@ end
42
42
 
43
43
  # Rails 3.X Template
44
44
  if defined?(Rails) && Rails.version =~ /^3/
45
- require 'action_view/base'
46
- require 'action_view/template'
47
-
48
45
  module ActionView
49
46
  module Template::Handlers
50
- class RablHandler < Template::Handler
51
- include Compilable
47
+ class Rabl
52
48
 
49
+ class_attribute :default_format
53
50
  self.default_format = Mime::JSON
54
51
 
55
- def compile(template) %{
56
- ::Rabl::Engine.new(#{template.source.inspect}).
57
- render(self, assigns.merge(local_assigns))
58
- } end
59
- end
60
- end
52
+ def self.call(template)
53
+ source = if template.source.empty?
54
+ File.read(template.identifier)
55
+ else # use source
56
+ template.source
57
+ end
58
+
59
+ %{ ::Rabl::Engine.new(#{source.inspect}).
60
+ render(self, assigns.merge(local_assigns)) }
61
+ end # call
62
+ end # rabl class
63
+ end # handlers
61
64
  end
62
65
 
63
- ActionView::Template.register_template_handler :rabl, ActionView::TemplateHandlers::RablHandler
66
+ ActionView::Template.register_template_handler :rabl, ActionView::TemplateHandlers::Rabl
64
67
  end
data/lib/rabl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.8"
3
3
  end
data/rabl.gemspec CHANGED
@@ -22,5 +22,6 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency 'riot', '~>0.12.3'
23
23
  s.add_development_dependency 'rr', '~>1.0.2'
24
24
  s.add_development_dependency 'mongoid'
25
+ s.add_development_dependency 'tilt'
25
26
  s.add_development_dependency 'bson_ext'
26
27
  end
@@ -0,0 +1,29 @@
1
+ require File.expand_path('../teststrap', __FILE__)
2
+ require File.expand_path('../../lib/rabl', __FILE__)
3
+
4
+ context "Rabl::Configuration" do
5
+
6
+ context "with defaults" do
7
+ setup { Rabl.configuration }
8
+
9
+ asserts(:include_json_root).equals true
10
+ asserts(:include_xml_root).equals false
11
+ asserts(:enable_json_callbacks).equals false
12
+ end
13
+
14
+ context "with configuration" do
15
+ setup do
16
+ Rabl.configure do |config|
17
+ config.include_json_root = false
18
+ config.include_xml_root = true
19
+ config.enable_json_callbacks = true
20
+ end
21
+ Rabl.configuration
22
+ end
23
+
24
+ asserts(:include_json_root).equals false
25
+ asserts(:include_xml_root).equals true
26
+ asserts(:enable_json_callbacks).equals true
27
+ end
28
+
29
+ end
data/test/engine_test.rb CHANGED
@@ -1,2 +1,312 @@
1
- require File.expand_path('../teststrap',__FILE__)
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
5
 
6
+ context "Rabl::Engine" do
7
+
8
+ helper(:rabl) { |t| RablTemplate.new { t } }
9
+
10
+ context "#initialize" do
11
+ setup do
12
+ Rabl::Engine.new("...source...", { :format => 'xml', :root => true, :view_path => '/path/to/views' })
13
+ end
14
+
15
+ asserts_topic.assigns :_source
16
+ asserts_topic.assigns :_options
17
+ end
18
+
19
+
20
+ context "with defaults" do
21
+ setup do
22
+ Rabl.configure do |config|
23
+ config.include_json_root = true
24
+ config.include_xml_root = false
25
+ config.enable_json_callbacks = false
26
+ end
27
+ end
28
+
29
+ context "#object" do
30
+
31
+ asserts "that it sets data source" do
32
+ template = rabl %q{
33
+ object @user
34
+ }
35
+ scope = Object.new
36
+ scope.instance_variable_set :@user, User.new
37
+ template.render(scope)
38
+ end.matches "{\"user\":{}}"
39
+
40
+ asserts "that it can set root node" do
41
+ template = rabl %q{
42
+ object @user => :person
43
+ }
44
+ scope = Object.new
45
+ scope.instance_variable_set :@user, User.new
46
+ template.render(scope)
47
+ end.equals "{\"person\":{}}"
48
+ end
49
+
50
+ context "#collection" do
51
+
52
+ asserts "that it sets object to be casted as a simple array" do
53
+ template = rabl %{
54
+ collection @users
55
+ }
56
+ scope = Object.new
57
+ scope.instance_variable_set :@users, [User.new, User.new]
58
+ template.render(scope)
59
+ end.equals "[{\"user\":{}},{\"user\":{}}]"
60
+
61
+ asserts "that it sets root node for objects" do
62
+ template = rabl %{
63
+ collection @users => :person
64
+ }
65
+ scope = Object.new
66
+ scope.instance_variable_set :@users, [User.new, User.new]
67
+ template.render(scope)
68
+ end.equals "{\"person\":[{\"person\":{}},{\"person\":{}}]}"
69
+
70
+ end
71
+
72
+ context "#attribute" do
73
+
74
+ asserts "that it adds an attribute or method to be included in output" do
75
+ template = rabl %{
76
+ object @user
77
+ attribute :name
78
+ }
79
+ scope = Object.new
80
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
81
+ template.render(scope)
82
+ end.equals "{\"user\":{\"name\":\"irvine\"}}"
83
+
84
+ asserts "that it can add attribute under a different key name through :as" do
85
+ template = rabl %{
86
+ object @user
87
+ attribute :name, :as => 'city'
88
+ }
89
+ scope = Object.new
90
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
91
+ template.render(scope)
92
+ end.equals "{\"user\":{\"city\":\"irvine\"}}"
93
+
94
+ asserts "that it can add attribute under a different key name through hash" do
95
+ template = rabl %{
96
+ object @user
97
+ attribute :name => :city
98
+ }
99
+ scope = Object.new
100
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
101
+ template.render(scope)
102
+ end.equals "{\"user\":{\"city\":\"irvine\"}}"
103
+
104
+ end
105
+
106
+ context "#code" do
107
+
108
+ asserts "that it can create an arbitraty code node" do
109
+ template = rabl %{
110
+ code(:foo) { 'bar' }
111
+ }
112
+ template.render(Object.new)
113
+ end.equals "{\"foo\":\"bar\"}"
114
+
115
+ asserts "that it can be passed conditionals" do
116
+ template = rabl %{
117
+ code(:foo, :if => lambda { |i| false }) { 'bar' }
118
+ }
119
+ template.render(Object.new)
120
+ end.equals "{}"
121
+
122
+ end
123
+
124
+ context "#child" do
125
+
126
+ asserts "that it can create a child node" do
127
+ template = rabl %{
128
+ object @user
129
+ attribute :name
130
+ child(@user) { attribute :city }
131
+ }
132
+ scope = Object.new
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\"}}}"
136
+
137
+ asserts "that it can create a child node with different key" do
138
+ template = rabl %{
139
+ object @user
140
+ attribute :name
141
+ child(@user => :person) { attribute :city }
142
+ }
143
+ scope = Object.new
144
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
145
+ template.render(scope)
146
+
147
+ end.equals "{\"user\":{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}}"
148
+ end
149
+
150
+ context "#glue" do
151
+
152
+ asserts "that it glues data from a child node" do
153
+ template = rabl %{
154
+ object @user
155
+ attribute :name
156
+ glue(@user) { attribute :city }
157
+ glue(@user) { attribute :age }
158
+ }
159
+ scope = Object.new
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}}"
163
+ end
164
+ end
165
+
166
+
167
+ context "without json root" do
168
+ setup do
169
+ Rabl.configure do |config|
170
+ config.include_json_root = false
171
+ config.include_xml_root = false
172
+ config.enable_json_callbacks = false
173
+ end
174
+ end
175
+
176
+ context "#object" do
177
+
178
+ asserts "that it sets data source" do
179
+ template = rabl %q{
180
+ object @user
181
+ }
182
+ scope = Object.new
183
+ scope.instance_variable_set :@user, User.new
184
+ template.render(scope)
185
+ end.matches "{}"
186
+
187
+ asserts "that it can set root node" do
188
+ template = rabl %q{
189
+ object @user => :person
190
+ }
191
+ scope = Object.new
192
+ scope.instance_variable_set :@user, User.new
193
+ template.render(scope)
194
+ end.equals "{}"
195
+ end
196
+
197
+ context "#collection" do
198
+
199
+ asserts "that it sets object to be casted as a simple array" do
200
+ template = rabl %{
201
+ collection @users
202
+ }
203
+ scope = Object.new
204
+ scope.instance_variable_set :@users, [User.new, User.new]
205
+ template.render(scope)
206
+ end.equals "[{},{}]"
207
+
208
+ asserts "that it sets root node for objects" do
209
+ template = rabl %{
210
+ collection @users => :person
211
+ }
212
+ scope = Object.new
213
+ scope.instance_variable_set :@users, [User.new, User.new]
214
+ template.render(scope)
215
+ end.equals "{\"person\":[{},{}]}"
216
+
217
+ end
218
+
219
+ context "#attribute" do
220
+
221
+ asserts "that it adds an attribute or method to be included in output" do
222
+ template = rabl %{
223
+ object @user
224
+ attribute :name
225
+ }
226
+ scope = Object.new
227
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
228
+ template.render(scope)
229
+ end.equals "{\"name\":\"irvine\"}"
230
+
231
+ asserts "that it can add attribute under a different key name through :as" do
232
+ template = rabl %{
233
+ object @user
234
+ attribute :name, :as => 'city'
235
+ }
236
+ scope = Object.new
237
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
238
+ template.render(scope)
239
+ end.equals "{\"city\":\"irvine\"}"
240
+
241
+ asserts "that it can add attribute under a different key name through hash" do
242
+ template = rabl %{
243
+ object @user
244
+ attribute :name => :city
245
+ }
246
+ scope = Object.new
247
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
248
+ template.render(scope)
249
+ end.equals "{\"city\":\"irvine\"}"
250
+
251
+ end
252
+
253
+ context "#code" do
254
+
255
+ asserts "that it can create an arbitraty code node" do
256
+ template = rabl %{
257
+ code(:foo) { 'bar' }
258
+ }
259
+ template.render(Object.new)
260
+ end.equals "{\"foo\":\"bar\"}"
261
+
262
+ asserts "that it can be passed conditionals" do
263
+ template = rabl %{
264
+ code(:foo, :if => lambda { |i| false }) { 'bar' }
265
+ }
266
+ template.render(Object.new)
267
+ end.equals "{}"
268
+
269
+ end
270
+
271
+ context "#child" do
272
+
273
+ asserts "that it can create a child node" do
274
+ template = rabl %{
275
+ object @user
276
+ attribute :name
277
+ child(@user) { attribute :city }
278
+ }
279
+ scope = Object.new
280
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
281
+ template.render(scope)
282
+ end.equals "{\"name\":\"leo\",\"user\":{\"city\":\"LA\"}}"
283
+
284
+ asserts "that it can create a child node with different key" do
285
+ template = rabl %{
286
+ object @user
287
+ attribute :name
288
+ child(@user => :person) { attribute :city }
289
+ }
290
+ scope = Object.new
291
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :city => 'LA')
292
+ template.render(scope)
293
+
294
+ end.equals "{\"name\":\"leo\",\"person\":{\"city\":\"LA\"}}"
295
+ end
296
+
297
+ context "#glue" do
298
+
299
+ asserts "that it glues data from a child node" do
300
+ template = rabl %{
301
+ object @user
302
+ attribute :name
303
+ glue(@user) { attribute :city }
304
+ glue(@user) { attribute :age }
305
+ }
306
+ scope = Object.new
307
+ 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}"
310
+ end
311
+ end
312
+ end
@@ -1,2 +1,68 @@
1
1
  require File.expand_path('../teststrap',__FILE__)
2
+ require File.expand_path('../../lib/rabl/template', __FILE__)
2
3
 
4
+ class Scope
5
+ end
6
+
7
+ context "RablTemplate" do
8
+
9
+ asserts "that it registers for .rabl files" do
10
+ Tilt['test.rabl']
11
+ end.equals RablTemplate
12
+
13
+ context "#render" do
14
+ setup do
15
+ RablTemplate.new { |t| "code(:lol) { 'wut' }" }
16
+ end
17
+
18
+ asserts "preparing and evaluating templates on #render" do
19
+ topic.render
20
+ end.matches %r{"lol":"wut"}
21
+
22
+ 3.times do |n|
23
+ asserts "can be rendered #{n} time(s)" do
24
+ topic.render
25
+ end.matches %r{"lol":"wut"}
26
+ end
27
+
28
+ # asserts "that it can be passed locals" do
29
+ # template = RablTemplate.new { "code(:name) { @name }" }
30
+ # template.render(Object.new, :object => 'Bob')
31
+ # end.matches %r{"name":"Bob"}
32
+
33
+ asserts "that it evaluates in object scope" do
34
+ template = RablTemplate.new { "code(:lol) { @name }" }
35
+ scope = Object.new
36
+ scope.instance_variable_set :@name, 'Joe'
37
+ template.render(scope)
38
+ end.matches %r{"lol":"Joe"}
39
+
40
+ asserts "that it can pass a block for yield" do
41
+ template = RablTemplate.new { "code(:lol) { 'Hey ' + yield + '!' }" }
42
+ template.render { 'Joe' }
43
+ end.matches %r{"lol":"Hey Joe!"}
44
+ end
45
+
46
+ context "#render compiled" do
47
+
48
+ # asserts "that it can be passed locals" do
49
+ # template = RablTemplate.new { "code(:name) { @name }" }
50
+ # template.render(Scope.new, :object => 'Bob')
51
+ # end.matches %r{"name":"Bob"}
52
+
53
+ asserts "that it evaluates in an object scope" do
54
+ template = RablTemplate.new { "code(:lol) { @name }" }
55
+ scope = Scope.new
56
+ scope.instance_variable_set :@name, 'Joe'
57
+ template.render(scope)
58
+ end.matches %r{"lol":"Joe"}
59
+
60
+ asserts "that it can pass a block for yield" do
61
+ template = RablTemplate.new { "code(:lol) { 'Hey ' + yield + '!' }" }
62
+ template.render(Scope.new) { 'Joe' }
63
+ end.matches %r{"lol":"Hey Joe!"}
64
+
65
+ end
66
+
67
+
68
+ end
data/test/teststrap.rb CHANGED
@@ -2,6 +2,7 @@ require 'riot'
2
2
  require 'riot/rr'
3
3
  require 'mongo'
4
4
  require 'mongoid'
5
+ require 'tilt'
5
6
  require File.expand_path('../../lib/rabl',__FILE__)
6
7
 
7
8
  Riot.pretty_dots
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rabl
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.7
5
+ version: 0.2.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nathan Esquenazi
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-19 00:00:00 -07:00
13
+ date: 2011-06-03 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -47,7 +47,7 @@ dependencies:
47
47
  type: :development
48
48
  version_requirements: *id003
49
49
  - !ruby/object:Gem::Dependency
50
- name: bson_ext
50
+ name: tilt
51
51
  prerelease: false
52
52
  requirement: &id004 !ruby/object:Gem::Requirement
53
53
  none: false
@@ -57,6 +57,17 @@ dependencies:
57
57
  version: "0"
58
58
  type: :development
59
59
  version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: bson_ext
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ type: :development
70
+ version_requirements: *id005
60
71
  description: General ruby templating for json or xml
61
72
  email:
62
73
  - nesquena@gmail.com
@@ -81,11 +92,13 @@ files:
81
92
  - lib/rabl/configuration.rb
82
93
  - lib/rabl/engine.rb
83
94
  - lib/rabl/helpers.rb
95
+ - lib/rabl/railtie.rb
84
96
  - lib/rabl/template.rb
85
97
  - lib/rabl/version.rb
86
98
  - rabl.gemspec
87
99
  - test.watchr
88
100
  - test/builder_test.rb
101
+ - test/configuration_test.rb
89
102
  - test/engine_test.rb
90
103
  - test/models/user.rb
91
104
  - test/template_test.rb
@@ -120,6 +133,7 @@ specification_version: 3
120
133
  summary: General ruby templating for json or xml
121
134
  test_files:
122
135
  - test/builder_test.rb
136
+ - test/configuration_test.rb
123
137
  - test/engine_test.rb
124
138
  - test/models/user.rb
125
139
  - test/template_test.rb