rabl 0.11.1 → 0.11.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rabl/template.rb CHANGED
@@ -11,8 +11,8 @@ if defined?(Tilt)
11
11
  @engine = ::Rabl::Engine.new(data, options)
12
12
  end
13
13
 
14
- def evaluate(scope, locals, &block)
15
- @engine.apply(scope, locals, &block).render
14
+ def evaluate(context_scope, locals, &block)
15
+ @engine.apply(context_scope, locals, &block).render
16
16
  end
17
17
  end
18
18
 
data/lib/rabl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.11.1"
2
+ VERSION = "0.11.2"
3
3
  end
data/test/builder_test.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  require File.expand_path('../teststrap', __FILE__)
2
2
 
3
3
  context "Rabl::Builder" do
4
- helper(:builder) { |opt| Rabl::Builder.new(opt) }
5
- helper(:build_hash) { |obj, opt| builder(opt).build(obj) }
4
+ helper(:builder) { |*args| Rabl::Builder.new(*args) }
5
+ helper(:build_hash) { |*args| builder(*args).to_hash }
6
6
 
7
7
  setup do
8
8
  @users = [User.new, User.new]
9
9
  @user = User.new
10
- builder({:view_path => '/path/to/views'})
10
+ builder(nil, nil, {:view_path => '/path/to/views'})
11
11
  end
12
12
 
13
13
  context "#initialize" do
@@ -15,46 +15,40 @@ context "Rabl::Builder" do
15
15
  asserts_topic.assigns :_view_path
16
16
  end
17
17
 
18
- context "#build" do
19
- setup { b = builder({}); b.build(User.new); b }
20
- asserts_topic.assigns :_object
21
- asserts_topic.assigns :_result
22
- end
23
-
24
18
  context "#to_hash" do
25
19
  context "when given a simple object" do
26
- setup { builder({ :attributes => { :name => {} } }) }
20
+ setup { builder(nil, { :attributes => [ { :name => :name } ] }) }
27
21
  asserts "that the object is set properly" do
28
- topic.build(User.new, :root_name => "user")
22
+ topic.to_hash(User.new, nil, :root_name => "user")
29
23
  end.equivalent_to({ "user" => { :name => "rabl" } })
30
24
  end
31
25
 
32
26
  context "when given an object alias" do
33
- setup { builder({ :attributes => { :name => { :as => :foo } } }) }
27
+ setup { builder(nil, { :attributes => [ { :name => :name, :options => { :as => :foo } } ] }) }
34
28
  asserts "that the object is set properly" do
35
- topic.build(User.new, :root_name => "person")
29
+ topic.to_hash(User.new, nil, :root_name => "person")
36
30
  end.equivalent_to({ "person" => { :foo => "rabl" } })
37
31
  end
38
32
 
39
33
  context "when specified with no root" do
40
- setup { builder({ :attributes => { :name => { :as => :name } } }) }
34
+ setup { builder(nil, { :attributes => [ { :name => :name, :options => { :as => :name } } ] }) }
41
35
  asserts "that the object is set properly" do
42
- topic.build(User.new, :root => false)
36
+ topic.to_hash(User.new, nil, :root => false)
43
37
  end.equivalent_to({ :name => "rabl" })
44
38
  end
45
39
 
46
40
  context "when nil values are replaced with empty strings" do
47
41
  setup do
48
42
  Rabl.configuration.replace_nil_values_with_empty_strings = true
49
- builder({ :attributes => { :name => {} }, :node => [{ :name => :extra, :options => {}, :block => lambda { |u| { :twitter => u.twitter } } }] })
43
+ builder(nil, { :attributes => [ { :name => :name } ], :node => [{ :name => :extra, :options => {}, :block => lambda { |u| { :twitter => u.twitter } } }] })
50
44
  end
51
45
 
52
46
  asserts "that an empty string is returned as the value" do
53
- topic.build(User.new(:name => nil, :twitter => nil))
47
+ topic.to_hash(User.new(:name => nil, :twitter => nil))
54
48
  end.equivalent_to({ :name => '', :extra => { :twitter => '' } })
55
49
 
56
50
  asserts "that it handles existing non nil values correctly" do
57
- topic.build(User.new(:name => 10, :twitter => 'twitter'))
51
+ topic.to_hash(User.new(:name => 10, :twitter => 'twitter'))
58
52
  end.equivalent_to({ :name => 10, :extra => { :twitter => 'twitter' } })
59
53
 
60
54
  teardown do
@@ -65,19 +59,19 @@ context "Rabl::Builder" do
65
59
  context "when empty string values are replaced with nil values" do
66
60
  setup do
67
61
  Rabl.configuration.replace_empty_string_values_with_nil_values = true
68
- builder({ :attributes => { :name => {} }, :node => [{ :name => :extra, :options => {}, :block => lambda { |u| { :twitter => u.twitter } } }] })
62
+ builder(nil, { :attributes => [ { :name => :name } ], :node => [{ :name => :extra, :options => {}, :block => lambda { |u| { :twitter => u.twitter } } }] })
69
63
  end
70
64
 
71
65
  asserts "that nil is returned as the value" do
72
- topic.build(User.new(:name => "", :twitter => ''))
66
+ topic.to_hash(User.new(:name => "", :twitter => ''))
73
67
  end.equivalent_to({ :name => nil, :extra => { :twitter => nil } })
74
68
 
75
69
  asserts "that it handles existing nil values correctly" do
76
- topic.build(User.new(:name => nil, :twitter => nil))
70
+ topic.to_hash(User.new(:name => nil, :twitter => nil))
77
71
  end.equivalent_to({ :name => nil, :extra => { :twitter => nil } })
78
72
 
79
73
  asserts "that it handles existing non nil values correctly" do
80
- topic.build(User.new(:name => 10, :twitter => 'twitter'))
74
+ topic.to_hash(User.new(:name => 10, :twitter => 'twitter'))
81
75
  end.equivalent_to({ :name => 10, :extra => { :twitter => 'twitter' } })
82
76
 
83
77
  teardown do
@@ -88,10 +82,10 @@ context "Rabl::Builder" do
88
82
  context "when nil values are excluded" do
89
83
  setup do
90
84
  Rabl.configuration.exclude_nil_values = true
91
- builder({ :attributes => { :name => {} } })
85
+ builder(nil, { :attributes => [ { :name => :name } ] })
92
86
  end
93
87
  asserts "that an nil attribute is not returned" do
94
- topic.build(User.new(:name => nil))
88
+ topic.to_hash(User.new(:name => nil))
95
89
  end.equivalent_to({ })
96
90
  teardown do
97
91
  Rabl.configuration.exclude_nil_values = false
@@ -101,7 +95,7 @@ context "Rabl::Builder" do
101
95
 
102
96
  context "#attribute" do
103
97
  asserts "that the node" do
104
- build_hash @user, :attributes => { :name => {}, :city => { :as => :city } }
98
+ build_hash @user, :attributes => [ { :name => :name }, { :name => :city, :options => { :as => :city } } ]
105
99
  end.equivalent_to({:name => 'rabl', :city => 'irvine'})
106
100
 
107
101
  context "that with a non-existent attribute" do
@@ -109,7 +103,7 @@ context "Rabl::Builder" do
109
103
  setup { stub(Rabl.configuration).raise_on_missing_attribute { false } }
110
104
 
111
105
  asserts "the node" do
112
- build_hash @user, :attributes => { :fake => :fake }
106
+ build_hash @user, :attributes => [ { :name => :fake } ]
113
107
  end.equals({})
114
108
  end
115
109
 
@@ -117,22 +111,22 @@ context "Rabl::Builder" do
117
111
  setup { stub(Rabl.configuration).raise_on_missing_attribute { true } }
118
112
 
119
113
  asserts "the node" do
120
- build_hash @user, :attributes => { :fake => :fake }
114
+ build_hash @user, :attributes => [ { :name => :fake } ]
121
115
  end.raises(RuntimeError)
122
116
  end
123
117
  end
124
118
 
125
119
  context "that with a string key" do
126
- setup { builder({ :attributes => { "name" => {} } }) }
120
+ setup { builder(nil, { :attributes => [ { :name => "name" } ] }) }
127
121
  asserts "the node name is converted to a symbol" do
128
- topic.build(User.new, :name => "user")
122
+ topic.to_hash(User.new, :name => "user")
129
123
  end.equivalent_to({ :name => "rabl" })
130
124
  end
131
125
 
132
126
  context "that with the same node names as strings and symbols" do
133
- setup { builder({ :attributes => { "name" => {}, :name => {} } }) }
127
+ setup { builder(nil, { :attributes => [ { :name => "name" }, { :name => :name } ] }) }
134
128
  asserts "the nodes aren't duplicated" do
135
- topic.build(User.new, :name => "user")
129
+ topic.to_hash(User.new, :name => "user")
136
130
  end.equivalent_to({ :name => "rabl" })
137
131
  end
138
132
  end
@@ -163,119 +157,119 @@ context "Rabl::Builder" do
163
157
 
164
158
  context "#child" do
165
159
  asserts "that it generates if no data present" do
166
- builder(:child => []).build(@user)
160
+ builder(nil, :child => []).to_hash(@user)
167
161
  end.equals({})
168
162
 
169
163
  asserts "that it generates with a hash" do
170
- b = builder(:child => [ { :data => { @user => :user }, :options => { }, :block => lambda { |u| attribute :name } } ])
171
- b.build(@user)
164
+ b = builder(nil, :child => [ { :data => { @user => :user }, :options => { }, :block => lambda { |u| attribute :name } } ])
165
+ b.to_hash(@user)
172
166
  end.equivalent_to({ :user => { :name => "rabl" } })
173
167
 
174
168
  asserts "that it generates with a hash alias" do
175
- b = builder :child => [{ :data => { @user => :person }, :options => {}, :block => lambda { |u| attribute :name } }]
176
- b.build(@user)
169
+ b = builder nil, :child => [{ :data => { @user => :person }, :options => {}, :block => lambda { |u| attribute :name } }]
170
+ b.to_hash(@user)
177
171
  end.equivalent_to({ :person => { :name => "rabl" } })
178
172
 
179
173
  asserts "that it generates with an object" do
180
- b = builder :child => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name } }]
174
+ b = builder nil, :child => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name } }]
181
175
  e = Rabl::Engine.new('')
182
176
  mock(b).data_name(@user) { :user }
183
177
  mock(e).render.returns('xyz')
184
178
  mock(b).object_to_engine(@user, { :root => false }).returns(e).subject
185
- b.build(@user)
179
+ b.to_hash(@user)
186
180
  end.equivalent_to({ :user => 'xyz'})
187
181
 
188
182
  asserts "that it generates with an collection and child_root" do
189
- b = builder :child => [{ :data => @users, :options => {}, :block => lambda { |u| attribute :name } }], :child_root => true
183
+ b = builder nil, { :child => [{ :data => @users, :options => {}, :block => lambda { |u| attribute :name } }] }, { :child_root => true }
190
184
  e = Rabl::Engine.new('')
191
185
  mock(b).data_name(@users) { :users }
192
186
  mock(e).render.returns('xyz')
193
187
  mock(b).object_to_engine(@users, { :root => true, :child_root => true }).returns(e).subject
194
- b.build(@user)
188
+ b.to_hash(@user)
195
189
  end.equivalent_to({ :users => 'xyz'})
196
190
 
197
191
  asserts "that it generates with an collection and no child root" do
198
- b = builder :child => [{ :data => @users, :options => {}, :block => lambda { |u| attribute :name } }], :child_root => false
192
+ b = builder nil, { :child => [{ :data => @users, :options => {}, :block => lambda { |u| attribute :name } }] }, { :child_root => false }
199
193
  e = Rabl::Engine.new('')
200
194
  mock(b).data_name(@users) { :users }
201
195
  mock(e).render.returns('xyz')
202
196
  mock(b).object_to_engine(@users, { :root => false, :child_root => false }).returns(e).subject
203
- b.build(@user)
197
+ b.to_hash(@user)
204
198
  end.equivalent_to({ :users => 'xyz'})
205
199
 
206
200
  asserts "that it generates with an collection and a specified object_root_name and root" do
207
201
  ops = { :object_root => "person", :root => :people }
208
- b = builder :child => [{ :data => @users, :options => ops, :block => lambda { |u| attribute :name } }], :child_root => true
202
+ b = builder nil, { :child => [{ :data => @users, :options => ops, :block => lambda { |u| attribute :name } }] }, { :child_root => true }
209
203
  e = Rabl::Engine.new('')
210
204
  mock(e).render.returns('xyz')
211
205
  mock(b).object_to_engine(@users, { :root => "person", :object_root_name => "person", :child_root => true }).returns(e).subject
212
- b.build(@user)
206
+ b.to_hash(@user)
213
207
  end.equivalent_to({ :people => 'xyz'})
214
208
 
215
209
  asserts "that it converts the child name to a symbol" do
216
- b = builder(:child => [ { :data => { @user => "user" }, :options => { }, :block => lambda { |u| attribute :name } } ])
217
- b.build(@user)
210
+ b = builder(nil, :child => [ { :data => { @user => "user" }, :options => { }, :block => lambda { |u| attribute :name } } ])
211
+ b.to_hash(@user)
218
212
  end.equivalent_to({ :user => { :name => "rabl" } })
219
213
 
220
214
  asserts "that it does't duplicate childs with the same name as a string and symbol" do
221
- b = builder(:child => [
215
+ b = builder(nil, :child => [
222
216
  { :data => { @user => "user" }, :options => { }, :block => lambda { |u| attribute :name } },
223
217
  { :data => { @user => :user }, :options => { }, :block => lambda { |u| attribute :name } }
224
218
  ])
225
- b.build(@user)
219
+ b.to_hash(@user)
226
220
  end.equivalent_to({ :user => { :name => "rabl" } })
227
221
  end
228
222
 
229
223
  context "#glue" do
230
224
  asserts "that it generates if no data present" do
231
- builder(:glue => []).build(@user)
225
+ builder(nil, :glue => []).to_hash(@user)
232
226
  end.equals({})
233
227
 
234
228
  asserts "that it generates the glue attributes" do
235
- b = builder :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name }}]
229
+ b = builder nil, :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name }}]
236
230
  e = Rabl::Engine.new('')
237
231
  mock(e).render.returns({:user => 'xyz'})
238
232
  mock(b).object_to_engine(@user, { :root => false }).returns(e).subject
239
- b.build(@user)
233
+ b.to_hash(@user)
240
234
  end.equivalent_to({ :user => 'xyz' })
241
235
 
242
236
  asserts "that it appends the glue attributes to result" do
243
- b = builder :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name => :user_name }}]
244
- b.build(@user)
237
+ b = builder nil, :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name => :user_name }}]
238
+ b.to_hash(@user)
245
239
  end.equivalent_to({ :user_name => 'rabl' })
246
240
 
247
241
  asserts "that it does not generate new attributes if no glue attributes are present" do
248
- b = builder :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name }}]
242
+ b = builder nil, :glue => [{ :data => @user, :options => {}, :block => lambda { |u| attribute :name }}]
249
243
  e = Rabl::Engine.new('')
250
244
  mock(e).render.returns({})
251
245
  mock(b).object_to_engine(@user,{ :root => false }).returns(e).subject
252
- b.build(@user)
246
+ b.to_hash(@user)
253
247
  end.equals({})
254
248
  end
255
249
 
256
250
  context "#extends" do
257
251
  asserts "that it does not generate if no data is present" do
258
- b = builder :extends => [{ :file => 'users/show', :options => {}, :block => lambda { |u| attribute :name }}]
252
+ b = builder nil, :extends => [{ :file => 'users/show', :options => {}, :block => lambda { |u| attribute :name }}]
259
253
  e = Rabl::Engine.new('users/show')
260
254
  mock(b).partial_as_engine('users/show',{ :object => @user}).returns(e)
261
255
  mock(e).render.returns({}).subject
262
- b.build(@user)
256
+ b.to_hash(@user)
263
257
  end.equals({})
264
258
 
265
259
  asserts "that it generates if data is present" do
266
- b = builder :extends => [{ :file => 'users/show', :options => {}, :block => lambda { |u| attribute :name }}]
260
+ b = builder nil, :extends => [{ :file => 'users/show', :options => {}, :block => lambda { |u| attribute :name }}]
267
261
  e = Rabl::Engine.new('users/show')
268
262
  mock(b).partial_as_engine('users/show',{ :object => @user}).returns(e)
269
263
  mock(e).render.returns({:user => 'xyz'}).subject
270
- b.build(@user)
264
+ b.to_hash(@user)
271
265
  end.equivalent_to({:user => 'xyz'})
272
266
 
273
267
  asserts "that it generates if local data is present but object is false" do
274
- b = builder :extends => [{ :file => 'users/show', :options => { :object => @user }, :block => lambda { |u| attribute :name }}]
268
+ b = builder nil, :extends => [{ :file => 'users/show', :options => { :object => @user }, :block => lambda { |u| attribute :name }}]
275
269
  e = Rabl::Engine.new('users/show')
276
270
  mock(b).partial_as_engine('users/show',{ :object => @user}).returns(e)
277
271
  mock(e).render.returns({:user => 'xyz'}).subject
278
- b.build(false)
272
+ b.to_hash(false)
279
273
  end.equivalent_to({:user => 'xyz'})
280
274
  end
281
275
 
@@ -291,26 +285,22 @@ context "Rabl::Builder" do
291
285
  end
292
286
 
293
287
  asserts "that it can use symbols on if condition and return false if method returns false" do
294
- scope = Rabl::Builder.new
295
- scope.instance_variable_set(:@_object, ArbObj.new)
288
+ scope = Rabl::Builder.new(ArbObj.new)
296
289
  scope.send(:resolve_condition, { :if => :cool? })
297
290
  end.equals(false)
298
291
 
299
292
  asserts "that it can use symbols on if condition and return true if method returns true" do
300
- scope = Rabl::Builder.new
301
- scope.instance_variable_set(:@_object, ArbObj.new)
293
+ scope = Rabl::Builder.new(ArbObj.new)
302
294
  scope.send :resolve_condition, { :if => :smooth? }
303
295
  end.equals(true)
304
296
 
305
297
  asserts "that it can use symbols as unless condition and return true if method returns false" do
306
- scope = Rabl::Builder.new
307
- scope.instance_variable_set(:@_object, ArbObj.new)
298
+ scope = Rabl::Builder.new(ArbObj.new)
308
299
  scope.send :resolve_condition, { :unless => :cool? }
309
300
  end.equals(true)
310
301
 
311
302
  asserts "that it can use symbols as unmless condition and return false if method returns true" do
312
- scope = Rabl::Builder.new
313
- scope.instance_variable_set(:@_object, ArbObj.new)
303
+ scope = Rabl::Builder.new(ArbObj.new)
314
304
  scope.send :resolve_condition, { :unless => :smooth? }
315
305
  end.equals(false)
316
306
  end
data/test/engine_test.rb CHANGED
@@ -57,7 +57,7 @@ context "Rabl::Engine" do
57
57
  template.instance_eval('@engine')
58
58
  end
59
59
 
60
- asserts_topic.assigns(:_cache) { ['foo', nil] }
60
+ asserts_topic.assigns(:_cache_key) { 'foo' }
61
61
  end
62
62
 
63
63
  context "with cache and options" do
@@ -69,7 +69,8 @@ context "Rabl::Engine" do
69
69
  template.instance_eval('@engine')
70
70
  end
71
71
 
72
- asserts_topic.assigns(:_cache) { ['foo', { :expires_in => 'bar' }] }
72
+ asserts_topic.assigns(:_cache_key) { 'foo' }
73
+ asserts_topic.assigns(:_cache_options) { { :expires_in => 'bar' } }
73
74
  end
74
75
 
75
76
  context "without cache" do
@@ -79,7 +80,7 @@ context "Rabl::Engine" do
79
80
  template.instance_eval('@engine')
80
81
  end
81
82
 
82
- denies(:instance_variable_defined?, :@_cache)
83
+ denies(:instance_variable_defined?, :@_cache_key)
83
84
  end
84
85
  end
85
86
 
data/test/models/user.rb CHANGED
@@ -10,7 +10,7 @@ unless defined?(User)
10
10
  DEFAULT_HOBBIES = ['Photography']
11
11
  DEFAULT_TWITTER = 'rablgem'
12
12
 
13
- def initialize(attributes={})
13
+ def initialize(attributes = {})
14
14
  %w(age city name first float hobbies twitter).each do |attr|
15
15
  self.send "#{attr}=", (attributes.has_key?(attr.to_sym) ? attributes[attr.to_sym] : self.class.const_get("DEFAULT_#{attr.upcase}"))
16
16
  end
@@ -5,7 +5,7 @@ require File.expand_path('../models/ormless', __FILE__)
5
5
 
6
6
  context "Rabl::MultiBuilder" do
7
7
  helper(:multi_builder) { |objects, options| Rabl::MultiBuilder.new(objects, options) }
8
- helper(:builder) { Rabl::Builder.new({}) }
8
+ helper(:builder) { Rabl::Builder.new(nil, {}) }
9
9
  helper(:engine) { |object| Rabl::Engine.new("").apply(nil, :object => object) }
10
10
 
11
11
  context "#initialize" do
@@ -32,7 +32,6 @@ context "Rabl::MultiBuilder" do
32
32
  context "#map_cache_key_to_engine" do
33
33
  asserts "maps the cache keys to the engines" do
34
34
  mb = multi_builder [], {}
35
- b = builder
36
35
  e = engine User.new
37
36
  mock(e).cache_key.returns(['cache key'])
38
37
  mb.send(:map_cache_key_to_engine, e)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.11.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Esquenazi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-20 00:00:00.000000000 Z
11
+ date: 2014-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -419,6 +419,7 @@ files:
419
419
  - lib/rabl/partials.rb
420
420
  - lib/rabl/railtie.rb
421
421
  - lib/rabl/renderer.rb
422
+ - lib/rabl/sources.rb
422
423
  - lib/rabl/template.rb
423
424
  - lib/rabl/tracker.rb
424
425
  - lib/rabl/version.rb