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 +4 -4
- data/.travis.yml +2 -2
- data/CHANGELOG.md +4 -1
- data/Gemfile +1 -1
- data/lib/rabl-rails/compiler.rb +10 -0
- data/lib/rabl-rails/nodes.rb +1 -0
- data/lib/rabl-rails/nodes/lookup.rb +23 -0
- data/lib/rabl-rails/version.rb +1 -1
- data/lib/rabl-rails/visitors/to_hash.rb +9 -0
- data/rabl-rails.gemspec +3 -1
- data/test/test_compiler.rb +12 -0
- data/test/test_hash_visitor.rb +14 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee0aee27278d987f9cca5d2c0a1660ae9cb4e0d6ae51734fa120f09d7c66449c
|
4
|
+
data.tar.gz: 64d9d559ba02a59de347391031bc16bb21ace05d54d293c3b302125668b76f7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5b87c14f4c3f356f00c47ea35d3506475bdfd109b4144b60194b151e8b04f180844d1e9ebf5acb6755b834d886bc4a80141a3cba240740c8aeabe2893875d59
|
7
|
+
data.tar.gz: 9eca1d27951909e3f309240a46d0408de07379c91f21c8366d4620ef0bc4d090b3365e61f942f5a8c076f258472edb86ed74594f0336b0ef847497a81cb4a5c2
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/lib/rabl-rails/compiler.rb
CHANGED
@@ -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.
|
data/lib/rabl-rails/nodes.rb
CHANGED
@@ -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
|
data/lib/rabl-rails/version.rb
CHANGED
@@ -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
|
data/rabl-rails.gemspec
CHANGED
@@ -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
|
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
|
|
data/test/test_compiler.rb
CHANGED
@@ -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))
|
data/test/test_hash_visitor.rb
CHANGED
@@ -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
|
+
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:
|
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
|