rabl-rails 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa8ac0eda1634eb6ff41e9f4f264a73c54e9ca83403120447ab9759f821a668f
4
- data.tar.gz: 6e84dd781d0de2df1300b0d92061c0433561171fab1c16430c519fe3d0d74227
3
+ metadata.gz: ee0aee27278d987f9cca5d2c0a1660ae9cb4e0d6ae51734fa120f09d7c66449c
4
+ data.tar.gz: 64d9d559ba02a59de347391031bc16bb21ace05d54d293c3b302125668b76f7e
5
5
  SHA512:
6
- metadata.gz: 7143acbf87219454974205552c66c78b29065505983ea645f8df0993a0512716f26a373b3b01844b48590bc1cf6be839509a9b2f9fb7939f6da03008a92c57ed
7
- data.tar.gz: f06d660507ade0b1dee990eb700cdb00bf3a7b899004def96d6e56c6190837bf0b3b2ca3487c0377fde71625d28747423333458ede69273d4cc3b492f07353db
6
+ metadata.gz: c5b87c14f4c3f356f00c47ea35d3506475bdfd109b4144b60194b151e8b04f180844d1e9ebf5acb6755b834d886bc4a80141a3cba240740c8aeabe2893875d59
7
+ data.tar.gz: 9eca1d27951909e3f309240a46d0408de07379c91f21c8366d4620ef0bc4d090b3365e61f942f5a8c076f258472edb86ed74594f0336b0ef847497a81cb4a5c2
@@ -5,8 +5,8 @@ env:
5
5
  - "RAILS_VERSION=4.2.6"
6
6
  - "RAILS_VERSION=5.2.0"
7
7
  rvm:
8
- - 2.2.7
9
- - 2.4.1
8
+ - 2.5.3
9
+ - 2.6.0
10
10
  - jruby
11
11
  before_install:
12
12
  - gem update bundler
@@ -1,7 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.5.5
4
+ * Add `lookup` node
5
+
3
6
  ## 0.5.4
4
- * Relax concurrent-ruby version dependency
7
+ * Relax concurrent-ruby version dependency (javierjulio)
5
8
 
6
9
  ## 0.5.3
7
10
  * Allow `extends` to accept lambdas
data/Gemfile CHANGED
@@ -8,7 +8,7 @@ rails = case rails_version
8
8
  when 'master'
9
9
  { github: 'rails/rails' }
10
10
  when "default"
11
- '~> 4.2.5'
11
+ '~> 5.2.1'
12
12
  else
13
13
  "~> #{rails_version}"
14
14
  end
@@ -107,6 +107,16 @@ module RablRails
107
107
  @template.add_node Nodes::Const.new(name, value)
108
108
  end
109
109
 
110
+ #
111
+ # Create a node `name` by looking the current resource being rendered in the
112
+ # `object` hash using, by default, the resource's id.
113
+ # Example:
114
+ # lookup(:favorite, :@user_favorites, cast: true)
115
+ #
116
+ def lookup(name, object, field: :id, cast: false)
117
+ @template.add_node Nodes::Lookup.new(name, object, field, cast)
118
+ end
119
+
110
120
  #
111
121
  # Merge arbitrary data into json output. Given block should
112
122
  # return a hash.
@@ -6,3 +6,4 @@ require 'rabl-rails/nodes/code'
6
6
  require 'rabl-rails/nodes/condition'
7
7
  require 'rabl-rails/nodes/extend'
8
8
  require 'rabl-rails/nodes/polymorphic'
9
+ require 'rabl-rails/nodes/lookup'
@@ -0,0 +1,23 @@
1
+ module RablRails
2
+ module Nodes
3
+ class Lookup
4
+ attr_reader :name, :data, :field
5
+
6
+ def initialize(name, data, field, cast = false)
7
+ @name = name
8
+ @data = data
9
+ @field = field
10
+ @cast = cast
11
+ @is_var = @data.to_s.start_with?('@')
12
+ end
13
+
14
+ def instance_variable_data?
15
+ @is_var
16
+ end
17
+
18
+ def cast_to_boolean?
19
+ @cast
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module RablRails
2
- VERSION = '0.5.4'
2
+ VERSION = '0.5.5'
3
3
  end
@@ -56,6 +56,15 @@ module Visitors
56
56
  @_result[n.name] = n.value
57
57
  end
58
58
 
59
+ def visit_Lookup n
60
+ object = object_from_data(nil, n)
61
+ key = _resource.public_send(n.field)
62
+ value = object[key]
63
+ value = !!value if n.cast_to_boolean?
64
+
65
+ @_result[n.name] = value
66
+ end
67
+
59
68
  def visit_Condition n
60
69
  @_result.merge!(sub_visit(_resource, n.nodes)) if instance_exec _resource, &(n.condition)
61
70
  end
@@ -14,7 +14,9 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.required_ruby_version = '>= 2.2.0'
16
16
 
17
- s.files = `git ls-files`.split("\n")
17
+ s.files = `git ls-files -z`.split("\x0").reject do |file|
18
+ file.start_with?('test/')
19
+ end
18
20
  s.test_files = `git ls-files -- test/*`.split("\n")
19
21
  s.require_paths = ["lib"]
20
22
 
@@ -227,6 +227,18 @@ class TestCompiler < Minitest::Test
227
227
  assert_equal 'fr_FR', const_node.value
228
228
  end
229
229
 
230
+ it "compiles lookup node" do
231
+ t = @compiler.compile_source(%{
232
+ lookup(:favorite, :@user_favorites, cast: true)
233
+ })
234
+
235
+ lookup_node = t.nodes.first
236
+ assert_equal :favorite, lookup_node.name
237
+ assert_equal :@user_favorites, lookup_node.data
238
+ assert_equal :id, lookup_node.field
239
+ assert lookup_node.cast_to_boolean?
240
+ end
241
+
230
242
  it "extends other template" do
231
243
  t = @compiler.compile_source(%{ extends 'user' })
232
244
  assert_equal([{ :id => :id }], extract_attributes(t.nodes))
@@ -141,6 +141,20 @@ class TestHashVisitor < Minitest::Test
141
141
  assert_equal({ locale: 'fr_FR' }, visitor_result)
142
142
  end
143
143
 
144
+ it 'renders a positive lookup node' do
145
+ @nodes << RablRails::Nodes::Lookup.new(:favorite, :@user_favorites, :id, true)
146
+ @context.assigns['user_favorites'] = { 1 => true }
147
+
148
+ assert_equal({ favorite: true }, visitor_result)
149
+ end
150
+
151
+ it 'renders a negative lookup node' do
152
+ @nodes << RablRails::Nodes::Lookup.new(:favorite, :@user_favorites, :id, false)
153
+ @context.assigns['user_favorites'] = { 2 => true }
154
+
155
+ assert_equal({ favorite: nil }, visitor_result)
156
+ end
157
+
144
158
  describe 'with a condition node' do
145
159
  before do
146
160
  @ns = [RablRails::Nodes::Attribute.new(name: :name)]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabl-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Cocchi-Perrier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-16 00:00:00.000000000 Z
11
+ date: 2019-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -115,6 +115,7 @@ files:
115
115
  - lib/rabl-rails/nodes/const.rb
116
116
  - lib/rabl-rails/nodes/extend.rb
117
117
  - lib/rabl-rails/nodes/glue.rb
118
+ - lib/rabl-rails/nodes/lookup.rb
118
119
  - lib/rabl-rails/nodes/polymorphic.rb
119
120
  - lib/rabl-rails/railtie.rb
120
121
  - lib/rabl-rails/renderers/hash.rb