rabl-rails 0.2.1 → 0.2.2

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.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.2.2
4
+ * Add condition blocks
5
+
3
6
  ## 0.2.1
4
7
  * Avoid useless render on POST request with custom responder
5
8
  * Custom responder now fallback to Rails default in case the template is not found
@@ -5,7 +5,7 @@ module RablRails
5
5
  #
6
6
  class Compiler
7
7
  def initialize
8
- @glue_count = 0
8
+ @i = 0
9
9
  end
10
10
 
11
11
  #
@@ -80,8 +80,8 @@ module RablRails
80
80
  #
81
81
  def glue(data)
82
82
  return unless block_given?
83
- name = :"_glue#{@glue_count}"
84
- @glue_count += 1
83
+ name = :"_glue#{@i}"
84
+ @i += 1
85
85
  @template[name] = sub_compile(data) { yield }
86
86
  end
87
87
 
@@ -118,6 +118,20 @@ module RablRails
118
118
  @template.merge!(t.source)
119
119
  end
120
120
 
121
+ #
122
+ # Provide a conditionnal block
123
+ #
124
+ # condition(->(u) { u.is_a?(Admin) }) do
125
+ # attributes :secret
126
+ # end
127
+ #
128
+ def condition(proc)
129
+ return unless block_given?
130
+ name = :"_if#{@i}"
131
+ @i += 1
132
+ @template[name] = Condition.new(proc, sub_compile(nil) { yield })
133
+ end
134
+
121
135
  protected
122
136
 
123
137
  #
@@ -142,7 +156,7 @@ module RablRails
142
156
  return {} unless block_given?
143
157
  old_template, @template = @template, {}
144
158
  yield
145
- @template.merge!(:_data => data)
159
+ data ? @template.merge!(:_data => data) : @template
146
160
  ensure
147
161
  @template = old_template
148
162
  end
@@ -0,0 +1,10 @@
1
+ module RablRails
2
+ class Condition
3
+ attr_reader :proc, :source
4
+
5
+ def initialize(proc, source)
6
+ @proc = proc
7
+ @source = source
8
+ end
9
+ end
10
+ end
@@ -78,6 +78,11 @@ module RablRails
78
78
  else # child
79
79
  object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
80
80
  end
81
+ when Condition
82
+ if instance_exec data, &(value.proc)
83
+ output.merge!(render_resource(data, value.source))
84
+ end
85
+ next output
81
86
  end
82
87
  output[key] = out
83
88
  output
@@ -1,3 +1,3 @@
1
1
  module RablRails
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
data/lib/rabl-rails.rb CHANGED
@@ -5,6 +5,7 @@ require 'active_support/core_ext/class/attribute_accessors'
5
5
 
6
6
  require 'rabl-rails/version'
7
7
  require 'rabl-rails/template'
8
+ require 'rabl-rails/condition'
8
9
  require 'rabl-rails/compiler'
9
10
 
10
11
  require 'rabl-rails/renderer'
@@ -153,6 +153,12 @@ class CompilerTest < ActiveSupport::TestCase
153
153
  assert_equal 2, t.source[:foo].size
154
154
  end
155
155
 
156
+ test "conditionnal block compile nicely" do
157
+ t = @compiler.compile_source(%{ condition(->(u) {}) do attributes :secret end })
158
+ assert_instance_of RablRails::Condition, t.source[:_if0]
159
+ assert_equal({ :secret => :secret }, t.source[:_if0].source)
160
+ end
161
+
156
162
  test "compile with no object" do
157
163
  t = @compiler.compile_source(%{
158
164
  object false
@@ -127,6 +127,18 @@ class TestJsonRenderer < ActiveSupport::TestCase
127
127
  assert_equal %q({"users":[]}), render_json_output
128
128
  end
129
129
 
130
+ test "condition blocks are transparent if the condition passed" do
131
+ c = RablRails::Condition.new(->(u) { true }, { :name => :name })
132
+ @template.source = { :_if0 => c }
133
+ assert_equal %q({"name":"foobar"}), render_json_output
134
+ end
135
+
136
+ test "condition blocks are ignored if the condition is not met" do
137
+ c = RablRails::Condition.new(->(u) { false }, { :name => :name })
138
+ @template.source = { :_if0 => c }
139
+ assert_equal %q({}), render_json_output
140
+ end
141
+
130
142
  test "render object with root node" do
131
143
  @template.root_name = :author
132
144
  @template.source = { :id => :id, :name => :name }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabl-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.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-09-27 00:00:00.000000000 Z
12
+ date: 2012-10-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -74,6 +74,7 @@ files:
74
74
  - Rakefile
75
75
  - lib/rabl-rails.rb
76
76
  - lib/rabl-rails/compiler.rb
77
+ - lib/rabl-rails/condition.rb
77
78
  - lib/rabl-rails/handler.rb
78
79
  - lib/rabl-rails/library.rb
79
80
  - lib/rabl-rails/railtie.rb