rabl 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NWZjOGQ4ZGRlYmFiMDBhNmZjODdjZDcyZTM3ZjBjNWQ3ODBjYjAzYQ==
4
+ ZjAzMDNhYTIyNjk1OTg1YjE5N2NiNzNhYThmMGIyMTQzY2Y2MTk1NA==
5
5
  data.tar.gz: !binary |-
6
- MjM1ZjE1YWJjZGQzYTk5YmU1N2UxYTU2YTM0ODRmNDU0NDcxMzgxMA==
6
+ NzI2ZGQ3MDQ0ZjY0NDk5NTFjZmRhZmJkYjEzMjNiYjMzMWM1N2MzNw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MjhiYTM0M2E1ZGE4OWM5ZDA5OTg4OTg4MzdkNDk3NjZkMmU2NDgwY2E2YWMy
10
- NWQ3NjE2MzA4OTc1NTVhY2JkMTQwNTAxNmI1MzA3ZWQzMDVmMTczNjBjYWZl
11
- MjUyM2Q1NWJjMGNhYmFhM2FiMzIyOWM3YjJlNWVjMjcyOGQzNDk=
9
+ OWViYTRkNWFjZTQ1MWU2NWRlMTUwNTlkNzQ5NzBkNjQyZGJlZjQ3OGU5Yjcy
10
+ NzhkNWE5YTM0MmYzMTEzZDE0OWIxN2E0YzYzOTUwNzg4OTQ5MDNmYjhmMDI3
11
+ YWUxMjNmZTIyZjE2NGRmMzE4N2VhYmYxYmI0YmJiMWM1MGMzYmQ=
12
12
  data.tar.gz: !binary |-
13
- MjI5ZGFiOGE1OTBkOGUyNmMzMTllMDM2MDhmMWVjZmVjOTMzOTg4ZWFhMzE5
14
- ZjU0ZDgxNjYwNDU3NWI3NjliYTYwZmNiMTAzMDhhNGZmZmM5OGYyNDg1NDg5
15
- ZTA3MmZlYTdkNDI1NWNhODhhN2E5Yjk2ZjUzOGNkYTQ5MjYyZTI=
13
+ OTRiYTVjMTkxZTVkOWNmMGI4MjgyYTA2MTcwNzcxOGZlMzdkYjAwZGNjZGI3
14
+ NDEyYjRlMTM0MGI0YWI0ZjhhMWM4ZDhjYjI0ZGIxNjlhZmQxNDlkZDljYjgz
15
+ NzcwOGM0OThiZDI4ODFjZTc5ZDkwM2FjZWVjNTU2MDU4NDdjNmQ=
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.9.1 (November 17 2013)
4
+
5
+ * NEW #507 Resolve conditionals for extends (Thanks @micahcraig)
6
+ * NEW #453 Support for symbol conditionals (Thanks @leoc)
7
+
3
8
  ## 0.9.0 (October 14 2013)
4
9
 
5
10
  * FIX #497 Renderer should support partial caching and a digestor bug fixed (Thanks @siong1987)
data/README.md CHANGED
@@ -364,15 +364,6 @@ node :full_name do |u|
364
364
  end
365
365
  ```
366
366
 
367
- or a custom node that exists only if a condition is true:
368
-
369
- ```ruby
370
- # m is the object being rendered, also supports :unless
371
- node(:foo, :if => lambda { |m| m.has_foo? }) do |m|
372
- m.foo
373
- end
374
- ```
375
-
376
367
  or don't pass a name and have the node block merged into the response:
377
368
 
378
369
  ```ruby
@@ -460,6 +451,45 @@ node(:comments) { |post| post.comments } unless locals[:hide_comments]
460
451
 
461
452
  This can be useful as an advanced tool when extending or rendering partials.
462
453
 
454
+ ### Conditions ###
455
+
456
+ You can provide conditions to all kinds of nodes, attributes, extends, etc. which includes a given element only if the specified condition is true.
457
+
458
+ ```ruby
459
+ collection @posts
460
+ # m is the object being rendered, also supports :unless
461
+ node(:coolness, :if => lambda { |m| m.coolness > 5 }) do |m|
462
+ m.coolness
463
+ end
464
+ ```
465
+
466
+ Because attributes take conditional options as well, we could simplify the example with:
467
+
468
+ ```ruby
469
+ collection @posts
470
+ # m is the object being rendered, also supports :unless
471
+ attribute(:coolness, :if => lambda { |m| m.coolness > 5 })
472
+ ```
473
+
474
+ The value for the `:if` and `:unless` options may be a simple `Boolean`, `Proc` or a `Symbol`. If it is a `Symbol` and the specific `@object` responds to its, the method will be called. Thus the example above can be rewritten as:
475
+
476
+ ```ruby
477
+ class Post
478
+ def cool?
479
+ coolness > 5
480
+ end
481
+ end
482
+ ```
483
+
484
+ and then:
485
+
486
+ ```ruby
487
+ collection @posts
488
+ attribute :coolness, if: :cool?
489
+ ```
490
+
491
+ Using conditions allows for easy control over when certain elements render.
492
+
463
493
  ### Template Scope ###
464
494
 
465
495
  In RABL, you have access to everything you need to build an API response. Each RABL template has full access to the controllers
@@ -667,5 +697,5 @@ Thanks again for all of these great projects.
667
697
 
668
698
  ## Copyright ##
669
699
 
670
- Copyright © 2011-2012 Nathan Esquenazi. See [MIT-LICENSE](https://github.com/nesquena/rabl/blob/master/MIT-LICENSE) for details.
700
+ Copyright © 2011-2013 Nathan Esquenazi. See [MIT-LICENSE](https://github.com/nesquena/rabl/blob/master/MIT-LICENSE) for details.
671
701
 
@@ -120,18 +120,35 @@ module Rabl
120
120
  # Extends an existing rabl template with additional attributes in the block
121
121
  # extends("users/show") { attribute :full_name }
122
122
  def extends(file, options={}, &block)
123
+ return unless resolve_condition(options)
123
124
  options = @options.slice(:child_root).merge(:object => @_object).merge(options)
124
125
  result = self.partial(file, options, &block)
125
126
  @_result.merge!(result) if result.is_a?(Hash)
126
127
  end
127
128
 
129
+ def call_condition_proc(condition, object, &blk)
130
+ blk = lambda { |v| v } unless block_given?
131
+ if condition.respond_to?(:call)
132
+ blk.call(condition.call(object))
133
+ elsif condition.is_a?(Symbol) && object.respond_to?(condition)
134
+ blk.call(object.send(condition))
135
+ else
136
+ false
137
+ end
138
+ end
139
+
128
140
  # resolve_condition(:if => true) => true
129
141
  # resolve_condition(:if => lambda { |m| false }) => false
130
142
  # resolve_condition(:unless => lambda { |m| true }) => true
131
143
  def resolve_condition(options)
132
144
  return true if options[:if].nil? && options[:unless].nil?
133
- result = options[:if] == true || (options[:if].respond_to?(:call) && options[:if].call(@_object)) if options.has_key?(:if)
134
- result = options[:unless] == false || (options[:unless].respond_to?(:call) && !options[:unless].call(@_object)) if options.has_key?(:unless)
145
+ result = nil
146
+ if options.has_key?(:if)
147
+ result = options[:if] == true || call_condition_proc(options[:if], @_object)
148
+ end
149
+ if options.has_key?(:unless)
150
+ result = options[:unless] == false || call_condition_proc(options[:unless], @_object, &:!)
151
+ end
135
152
  result
136
153
  end
137
154
 
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
@@ -180,4 +180,40 @@ context "Rabl::Builder" do
180
180
  b.build(false)
181
181
  end.equivalent_to({:user => 'xyz'})
182
182
  end
183
+
184
+ context "#resolve_conditionals" do
185
+ class ArbObj
186
+ def cool?
187
+ false
188
+ end
189
+
190
+ def smooth?
191
+ true
192
+ end
193
+ end
194
+
195
+ asserts "that it can use symbols on if condition and return false if method returns false" do
196
+ scope = Rabl::Builder.new
197
+ scope.instance_variable_set(:@_object, ArbObj.new)
198
+ scope.send(:resolve_condition, { :if => :cool? })
199
+ end.equals(false)
200
+
201
+ asserts "that it can use symbols on if condition and return true if method returns true" do
202
+ scope = Rabl::Builder.new
203
+ scope.instance_variable_set(:@_object, ArbObj.new)
204
+ scope.send :resolve_condition, { :if => :smooth? }
205
+ end.equals(true)
206
+
207
+ asserts "that it can use symbols as unless condition and return true if method returns false" do
208
+ scope = Rabl::Builder.new
209
+ scope.instance_variable_set(:@_object, ArbObj.new)
210
+ scope.send :resolve_condition, { :unless => :cool? }
211
+ end.equals(true)
212
+
213
+ asserts "that it can use symbols as unmless condition and return false if method returns true" do
214
+ scope = Rabl::Builder.new
215
+ scope.instance_variable_set(:@_object, ArbObj.new)
216
+ scope.send :resolve_condition, { :unless => :smooth? }
217
+ end.equals(false)
218
+ end
183
219
  end
@@ -680,6 +680,42 @@ context "Rabl::Engine" do
680
680
  end.equals JSON.parse("{\"name\":\"leo\"}")
681
681
  end
682
682
 
683
+ context "#extends" do
684
+ helper(:tmp_path) { @tmp_path ||= Pathname.new(Dir.mktmpdir) }
685
+ setup do
686
+ Rabl.configure do |config|
687
+ config.view_paths = tmp_path
688
+ end
689
+ File.open(tmp_path + "test.json.rabl", "w") do |f|
690
+ f.puts %q{
691
+ attributes :age
692
+ }
693
+ end
694
+ end
695
+
696
+ asserts "that it extends the template with attributes from the file" do
697
+ template = rabl %{
698
+ object @user
699
+ attribute :name
700
+ extends 'test'
701
+ }
702
+ scope = Object.new
703
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :age => 12)
704
+ JSON.parse(template.render(scope))
705
+ end.equals JSON.parse("{\"name\":\"leo\",\"age\":12}")
706
+
707
+ asserts "that it can be passed conditionals" do
708
+ template = rabl %{
709
+ object @user
710
+ attribute :name
711
+ extends('test', {:if => lambda { |i| false }})
712
+ }
713
+ scope = Object.new
714
+ scope.instance_variable_set :@user, User.new(:name => 'leo', :age => 12)
715
+ JSON.parse(template.render(scope))
716
+ end.equals JSON.parse("{\"name\":\"leo\"}")
717
+ end
718
+
683
719
  teardown do
684
720
  Rabl.reset_configuration!
685
721
  end
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.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Esquenazi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-14 00:00:00.000000000 Z
11
+ date: 2013-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport