babl-json 0.5.5 → 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/babl/builder/template_base.rb +3 -5
- data/lib/babl/nodes/concat.rb +11 -11
- data/lib/babl/nodes/create_pin.rb +11 -5
- data/lib/babl/nodes/dep.rb +6 -5
- data/lib/babl/nodes/each.rb +8 -7
- data/lib/babl/nodes/fixed_array.rb +16 -10
- data/lib/babl/nodes/goto_pin.rb +14 -8
- data/lib/babl/nodes/merge.rb +12 -12
- data/lib/babl/nodes/nav.rb +11 -10
- data/lib/babl/nodes/object.rb +12 -10
- data/lib/babl/nodes/parent.rb +10 -8
- data/lib/babl/nodes/switch.rb +9 -9
- data/lib/babl/nodes/with.rb +14 -12
- data/lib/babl/operators/call.rb +5 -1
- data/lib/babl/rendering/compiled_template.rb +5 -1
- data/lib/babl/utils/dsl_proxy.rb +2 -2
- data/lib/babl/utils/value.rb +24 -6
- data/lib/babl/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea8f407140ea61dc83be26fece70b85e47280b09
|
4
|
+
data.tar.gz: d2503155b43b8ae0064d30808c30843c56b25702
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0725f1502d153f9de8dbd126dd3b5c03c9211c696b39a778aa756f9d8bc85f6db4fe10260bd39626edaf71b59d074af20b9af6aba49cba736e0eb72a70aca759
|
7
|
+
data.tar.gz: 4e213c4de1d6bcd517e7b945b6eb3cd0004891c1356065a2c4716269f669b8fee63049897cbc4c4fc7be504ee8a6b02c209e3ba0a282fe1cd541331efaef2d9f
|
@@ -11,8 +11,6 @@ module Babl
|
|
11
11
|
# Since the BABL code is run via #instance_exec within an instance of this class, we want to
|
12
12
|
# define as few methods as possible here.
|
13
13
|
class TemplateBase
|
14
|
-
attr_reader :builder
|
15
|
-
|
16
14
|
def initialize(builder = ChainBuilder.new(&:itself))
|
17
15
|
@builder = builder
|
18
16
|
freeze
|
@@ -40,7 +38,7 @@ module Babl
|
|
40
38
|
pretty: pretty,
|
41
39
|
node: tree,
|
42
40
|
dependencies: dependencies,
|
43
|
-
|
41
|
+
schema: schema
|
44
42
|
)
|
45
43
|
end
|
46
44
|
|
@@ -50,6 +48,8 @@ module Babl
|
|
50
48
|
|
51
49
|
protected
|
52
50
|
|
51
|
+
attr_reader :builder
|
52
|
+
|
53
53
|
def validate(tree)
|
54
54
|
# NOOP
|
55
55
|
end
|
@@ -65,8 +65,6 @@ module Babl
|
|
65
65
|
def construct_terminal(&block)
|
66
66
|
self.class.new builder.construct_terminal(&block)
|
67
67
|
end
|
68
|
-
|
69
|
-
protected :builder
|
70
68
|
end
|
71
69
|
end
|
72
70
|
end
|
data/lib/babl/nodes/concat.rb
CHANGED
@@ -6,18 +6,26 @@ require 'babl/schema'
|
|
6
6
|
module Babl
|
7
7
|
module Nodes
|
8
8
|
class Concat < Utils::Value.new(:nodes)
|
9
|
-
def dependencies
|
9
|
+
memoize def dependencies
|
10
10
|
Babl::Utils::Hash.deep_merge(*nodes.map(&:dependencies))
|
11
11
|
end
|
12
12
|
|
13
|
-
def pinned_dependencies
|
13
|
+
memoize def pinned_dependencies
|
14
14
|
Babl::Utils::Hash.deep_merge(*nodes.map(&:pinned_dependencies))
|
15
15
|
end
|
16
16
|
|
17
|
-
def schema
|
17
|
+
memoize def schema
|
18
18
|
nodes.map(&:schema).reduce(Schema::FixedArray::EMPTY) { |a, b| merge_doc(a, b) }
|
19
19
|
end
|
20
20
|
|
21
|
+
memoize def optimize
|
22
|
+
optimize_empty ||
|
23
|
+
optimize_single ||
|
24
|
+
optimize_concatenated_arrays ||
|
25
|
+
optimize_preconcat_constant ||
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
21
29
|
def render(frame)
|
22
30
|
out = []
|
23
31
|
nodes.each { |node|
|
@@ -31,14 +39,6 @@ module Babl
|
|
31
39
|
out
|
32
40
|
end
|
33
41
|
|
34
|
-
def optimize
|
35
|
-
optimize_empty ||
|
36
|
-
optimize_single ||
|
37
|
-
optimize_concatenated_arrays ||
|
38
|
-
optimize_preconcat_constant ||
|
39
|
-
self
|
40
|
-
end
|
41
|
-
|
42
42
|
private
|
43
43
|
|
44
44
|
def optimize_empty
|
@@ -10,21 +10,27 @@ module Babl
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def schema
|
13
|
+
memoize def schema
|
14
14
|
node.schema
|
15
15
|
end
|
16
16
|
|
17
|
-
def dependencies
|
17
|
+
memoize def dependencies
|
18
18
|
Babl::Utils::Hash.deep_merge(node.dependencies, node.pinned_dependencies[ref] || Utils::Hash::EMPTY)
|
19
19
|
end
|
20
20
|
|
21
|
-
def pinned_dependencies
|
21
|
+
memoize def pinned_dependencies
|
22
22
|
node.pinned_dependencies.reject { |k, _v| k == ref }
|
23
23
|
end
|
24
24
|
|
25
|
-
def optimize
|
25
|
+
memoize def optimize
|
26
26
|
optimized = node.optimize
|
27
|
-
optimized.pinned_dependencies[ref]
|
27
|
+
if !optimized.pinned_dependencies[ref]
|
28
|
+
optimized
|
29
|
+
elsif optimized.equal?(node)
|
30
|
+
self
|
31
|
+
else
|
32
|
+
CreatePin.new(optimized, ref)
|
33
|
+
end
|
28
34
|
end
|
29
35
|
end
|
30
36
|
end
|
data/lib/babl/nodes/dep.rb
CHANGED
@@ -8,20 +8,21 @@ module Babl
|
|
8
8
|
node.render(frame)
|
9
9
|
end
|
10
10
|
|
11
|
-
def schema
|
11
|
+
memoize def schema
|
12
12
|
node.schema
|
13
13
|
end
|
14
14
|
|
15
|
-
def pinned_dependencies
|
15
|
+
memoize def pinned_dependencies
|
16
16
|
node.pinned_dependencies
|
17
17
|
end
|
18
18
|
|
19
|
-
def dependencies
|
19
|
+
memoize def dependencies
|
20
20
|
Babl::Utils::Hash.deep_merge(node.dependencies, path)
|
21
21
|
end
|
22
22
|
|
23
|
-
def optimize
|
24
|
-
|
23
|
+
memoize def optimize
|
24
|
+
optimized = node.optimize
|
25
|
+
optimized.equal?(node) ? self : Dep.new(optimized, path)
|
25
26
|
end
|
26
27
|
end
|
27
28
|
end
|
data/lib/babl/nodes/each.rb
CHANGED
@@ -6,7 +6,7 @@ require 'babl/utils'
|
|
6
6
|
module Babl
|
7
7
|
module Nodes
|
8
8
|
class Each < Utils::Value.new(:node)
|
9
|
-
def dependencies
|
9
|
+
memoize def dependencies
|
10
10
|
node_deps = node.dependencies
|
11
11
|
child_deps = node_deps.reject { |key, _| key == Parent::PARENT_MARKER }
|
12
12
|
|
@@ -16,14 +16,19 @@ module Babl
|
|
16
16
|
)
|
17
17
|
end
|
18
18
|
|
19
|
-
def schema
|
19
|
+
memoize def schema
|
20
20
|
Schema::DynArray.new(node.schema)
|
21
21
|
end
|
22
22
|
|
23
|
-
def pinned_dependencies
|
23
|
+
memoize def pinned_dependencies
|
24
24
|
node.pinned_dependencies
|
25
25
|
end
|
26
26
|
|
27
|
+
memoize def optimize
|
28
|
+
optimized = node.optimize
|
29
|
+
optimized.equal?(node) ? self : Each.new(node.optimize)
|
30
|
+
end
|
31
|
+
|
27
32
|
def render(frame)
|
28
33
|
collection = frame.object
|
29
34
|
unless Enumerable === collection
|
@@ -36,10 +41,6 @@ module Babl
|
|
36
41
|
end
|
37
42
|
end
|
38
43
|
end
|
39
|
-
|
40
|
-
def optimize
|
41
|
-
Each.new(node.optimize)
|
42
|
-
end
|
43
44
|
end
|
44
45
|
end
|
45
46
|
end
|
@@ -8,27 +8,33 @@ module Babl
|
|
8
8
|
class FixedArray < Utils::Value.new(:nodes)
|
9
9
|
EMPTY = new(Utils::Array::EMPTY)
|
10
10
|
|
11
|
-
def schema
|
11
|
+
memoize def schema
|
12
12
|
Schema::FixedArray.new(nodes.map(&:schema))
|
13
13
|
end
|
14
14
|
|
15
|
-
def dependencies
|
15
|
+
memoize def dependencies
|
16
16
|
Babl::Utils::Hash.deep_merge(*nodes.map(&:dependencies))
|
17
17
|
end
|
18
18
|
|
19
|
-
def pinned_dependencies
|
19
|
+
memoize def pinned_dependencies
|
20
20
|
Babl::Utils::Hash.deep_merge(*nodes.map(&:pinned_dependencies))
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
24
|
-
nodes.map
|
23
|
+
memoize def optimize
|
24
|
+
optimized_nodes = nodes.map(&:optimize)
|
25
|
+
|
26
|
+
fixed_array = self if optimized_nodes.each_with_index.all? { |v, idx| v.equal?(nodes[idx]) }
|
27
|
+
fixed_array ||= FixedArray.new(optimized_nodes)
|
28
|
+
|
29
|
+
if optimized_nodes.all? { |node| Constant === node }
|
30
|
+
Constant.new(optimized_nodes.map(&:value).freeze, fixed_array.schema)
|
31
|
+
else
|
32
|
+
fixed_array
|
33
|
+
end
|
25
34
|
end
|
26
35
|
|
27
|
-
def
|
28
|
-
|
29
|
-
fixed_array = FixedArray.new(optimized_nodes)
|
30
|
-
return fixed_array unless optimized_nodes.all? { |node| Constant === node }
|
31
|
-
Constant.new(fixed_array.nodes.map(&:value).freeze, fixed_array.schema)
|
36
|
+
def render(frame)
|
37
|
+
nodes.map { |node| node.render(frame) }
|
32
38
|
end
|
33
39
|
end
|
34
40
|
end
|
data/lib/babl/nodes/goto_pin.rb
CHANGED
@@ -5,28 +5,34 @@ require 'babl/nodes/constant'
|
|
5
5
|
module Babl
|
6
6
|
module Nodes
|
7
7
|
class GotoPin < Utils::Value.new(:node, :ref)
|
8
|
-
def dependencies
|
8
|
+
memoize def dependencies
|
9
9
|
Utils::Hash::EMPTY
|
10
10
|
end
|
11
11
|
|
12
|
-
def pinned_dependencies
|
12
|
+
memoize def pinned_dependencies
|
13
13
|
Babl::Utils::Hash.deep_merge(node.pinned_dependencies, ref => node.dependencies)
|
14
14
|
end
|
15
15
|
|
16
|
-
def schema
|
16
|
+
memoize def schema
|
17
17
|
node.schema
|
18
18
|
end
|
19
19
|
|
20
|
+
memoize def optimize
|
21
|
+
optimized = node.optimize
|
22
|
+
if Constant === optimized
|
23
|
+
optimized
|
24
|
+
elsif optimized.equal?(node)
|
25
|
+
self
|
26
|
+
else
|
27
|
+
GotoPin.new(optimized, ref)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
20
31
|
def render(frame)
|
21
32
|
frame.goto_pin(ref) do |new_frame|
|
22
33
|
node.render(new_frame)
|
23
34
|
end
|
24
35
|
end
|
25
|
-
|
26
|
-
def optimize
|
27
|
-
optimized = node.optimize
|
28
|
-
Constant === optimized ? optimized : GotoPin.new(optimized, ref)
|
29
|
-
end
|
30
36
|
end
|
31
37
|
end
|
32
38
|
end
|
data/lib/babl/nodes/merge.rb
CHANGED
@@ -7,27 +7,19 @@ require 'babl/nodes/constant'
|
|
7
7
|
module Babl
|
8
8
|
module Nodes
|
9
9
|
class Merge < Utils::Value.new(:nodes)
|
10
|
-
def dependencies
|
10
|
+
memoize def dependencies
|
11
11
|
Babl::Utils::Hash.deep_merge(*nodes.map(&:dependencies))
|
12
12
|
end
|
13
13
|
|
14
|
-
def pinned_dependencies
|
14
|
+
memoize def pinned_dependencies
|
15
15
|
Babl::Utils::Hash.deep_merge(*nodes.map(&:pinned_dependencies))
|
16
16
|
end
|
17
17
|
|
18
|
-
def schema
|
18
|
+
memoize def schema
|
19
19
|
nodes.map(&:schema).reduce(Schema::Object::EMPTY) { |a, b| merge_doc(a, b) }
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
nodes.map { |node| node.render(frame) }.compact.reduce({}) { |acc, val|
|
24
|
-
raise Errors::RenderingError, "Only objects can be merged\n" +
|
25
|
-
frame.formatted_stack unless ::Hash === val
|
26
|
-
acc.merge!(val)
|
27
|
-
}
|
28
|
-
end
|
29
|
-
|
30
|
-
def optimize
|
22
|
+
memoize def optimize
|
31
23
|
optimize_empty ||
|
32
24
|
optimize_single ||
|
33
25
|
optimize_nested_merges ||
|
@@ -36,6 +28,14 @@ module Babl
|
|
36
28
|
self
|
37
29
|
end
|
38
30
|
|
31
|
+
def render(frame)
|
32
|
+
nodes.map { |node| node.render(frame) }.compact.reduce({}) { |acc, val|
|
33
|
+
raise Errors::RenderingError, "Only objects can be merged\n" +
|
34
|
+
frame.formatted_stack unless ::Hash === val
|
35
|
+
acc.merge!(val)
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
39
|
private
|
40
40
|
|
41
41
|
def optimize_empty
|
data/lib/babl/nodes/nav.rb
CHANGED
@@ -6,7 +6,7 @@ require 'babl/nodes/parent'
|
|
6
6
|
module Babl
|
7
7
|
module Nodes
|
8
8
|
class Nav < Utils::Value.new(:property, :node)
|
9
|
-
def dependencies
|
9
|
+
memoize def dependencies
|
10
10
|
node_deps = node.dependencies
|
11
11
|
child_deps = node_deps.reject { |key, _| key == Parent::PARENT_MARKER }
|
12
12
|
|
@@ -16,14 +16,22 @@ module Babl
|
|
16
16
|
)
|
17
17
|
end
|
18
18
|
|
19
|
-
def schema
|
19
|
+
memoize def schema
|
20
20
|
node.schema
|
21
21
|
end
|
22
22
|
|
23
|
-
def pinned_dependencies
|
23
|
+
memoize def pinned_dependencies
|
24
24
|
node.pinned_dependencies
|
25
25
|
end
|
26
26
|
|
27
|
+
memoize def optimize
|
28
|
+
optimized = node.optimize
|
29
|
+
return optimized if Constant === optimized
|
30
|
+
return optimized.node if Parent === optimized
|
31
|
+
return self if optimized.equal?(node)
|
32
|
+
Nav.new(property, optimized)
|
33
|
+
end
|
34
|
+
|
27
35
|
def render(frame)
|
28
36
|
value = begin
|
29
37
|
object = frame.object
|
@@ -36,13 +44,6 @@ module Babl
|
|
36
44
|
node.render(new_frame)
|
37
45
|
end
|
38
46
|
end
|
39
|
-
|
40
|
-
def optimize
|
41
|
-
optimized = node.optimize
|
42
|
-
return optimized if Constant === optimized
|
43
|
-
return optimized.node if Parent === optimized
|
44
|
-
Nav.new(property, optimized)
|
45
|
-
end
|
46
47
|
end
|
47
48
|
end
|
48
49
|
end
|
data/lib/babl/nodes/object.rb
CHANGED
@@ -8,31 +8,33 @@ module Babl
|
|
8
8
|
class Object < Utils::Value.new(:nodes)
|
9
9
|
EMPTY = new(Utils::Hash::EMPTY)
|
10
10
|
|
11
|
-
def dependencies
|
11
|
+
memoize def dependencies
|
12
12
|
Babl::Utils::Hash.deep_merge(*nodes.values.map(&:dependencies))
|
13
13
|
end
|
14
14
|
|
15
|
-
def pinned_dependencies
|
15
|
+
memoize def pinned_dependencies
|
16
16
|
Babl::Utils::Hash.deep_merge(*nodes.values.map(&:pinned_dependencies))
|
17
17
|
end
|
18
18
|
|
19
|
-
def schema
|
19
|
+
memoize def schema
|
20
20
|
properties = nodes.map { |k, v| Schema::Object::Property.new(k, v.schema, true) }
|
21
21
|
Schema::Object.new(properties, false)
|
22
22
|
end
|
23
23
|
|
24
|
+
memoize def optimize
|
25
|
+
optimized_nodes = nodes.map { |k, v| [k, v.optimize] }.to_h
|
26
|
+
object = self if optimized_nodes.all? { |k, v| v.equal?(nodes[k]) }
|
27
|
+
object ||= Object.new(optimized_nodes)
|
28
|
+
|
29
|
+
return object unless optimized_nodes.values.all? { |node| Constant === node }
|
30
|
+
Constant.new(optimized_nodes.map { |k, v| [k, v.value] }.to_h.freeze, object.schema)
|
31
|
+
end
|
32
|
+
|
24
33
|
def render(frame)
|
25
34
|
out = {}
|
26
35
|
nodes.each { |k, v| out[k] = v.render(frame) }
|
27
36
|
out
|
28
37
|
end
|
29
|
-
|
30
|
-
def optimize
|
31
|
-
optimized_nodes = nodes.map { |k, v| [k, v.optimize] }.to_h
|
32
|
-
optimized_object = Object.new(optimized_nodes)
|
33
|
-
return optimized_object unless optimized_nodes.values.all? { |node| Constant === node }
|
34
|
-
Constant.new(optimized_object.nodes.map { |k, v| [k, v.value] }.to_h.freeze, optimized_object.schema)
|
35
|
-
end
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|
data/lib/babl/nodes/parent.rb
CHANGED
@@ -7,28 +7,30 @@ module Babl
|
|
7
7
|
class Parent < Utils::Value.new(:node)
|
8
8
|
PARENT_MARKER = Utils::Ref.new
|
9
9
|
|
10
|
-
def schema
|
10
|
+
memoize def schema
|
11
11
|
node.schema
|
12
12
|
end
|
13
13
|
|
14
|
-
def pinned_dependencies
|
14
|
+
memoize def pinned_dependencies
|
15
15
|
node.pinned_dependencies
|
16
16
|
end
|
17
17
|
|
18
|
-
def dependencies
|
18
|
+
memoize def dependencies
|
19
19
|
{ PARENT_MARKER => node.dependencies }
|
20
20
|
end
|
21
21
|
|
22
|
+
memoize def optimize
|
23
|
+
optimized = node.optimize
|
24
|
+
return optimized if Constant === optimized
|
25
|
+
return self if optimized.equal?(node)
|
26
|
+
Parent.new(optimized)
|
27
|
+
end
|
28
|
+
|
22
29
|
def render(frame)
|
23
30
|
frame.move_backward do |new_frame|
|
24
31
|
node.render(new_frame)
|
25
32
|
end
|
26
33
|
end
|
27
|
-
|
28
|
-
def optimize
|
29
|
-
optimized = node.optimize
|
30
|
-
Constant === optimized ? optimized : Parent.new(optimized)
|
31
|
-
end
|
32
34
|
end
|
33
35
|
end
|
34
36
|
end
|
data/lib/babl/nodes/switch.rb
CHANGED
@@ -13,24 +13,19 @@ module Babl
|
|
13
13
|
super
|
14
14
|
end
|
15
15
|
|
16
|
-
def dependencies
|
16
|
+
memoize def dependencies
|
17
17
|
Babl::Utils::Hash.deep_merge(*nodes.flatten(1).map(&:dependencies))
|
18
18
|
end
|
19
19
|
|
20
|
-
def pinned_dependencies
|
20
|
+
memoize def pinned_dependencies
|
21
21
|
Babl::Utils::Hash.deep_merge(*nodes.flatten(1).map(&:pinned_dependencies))
|
22
22
|
end
|
23
23
|
|
24
|
-
def schema
|
24
|
+
memoize def schema
|
25
25
|
Schema::AnyOf.canonicalized(nodes.map(&:last).map(&:schema))
|
26
26
|
end
|
27
27
|
|
28
|
-
def
|
29
|
-
nodes.each { |cond, value| return value.render(frame) if cond.render(frame) }
|
30
|
-
raise Errors::RenderingError, 'A least one switch() condition must be taken'
|
31
|
-
end
|
32
|
-
|
33
|
-
def optimize
|
28
|
+
memoize def optimize
|
34
29
|
optimize_condition_and_values ||
|
35
30
|
optimize_falsy_conditions ||
|
36
31
|
optimize_truthy_conditions ||
|
@@ -40,6 +35,11 @@ module Babl
|
|
40
35
|
self
|
41
36
|
end
|
42
37
|
|
38
|
+
def render(frame)
|
39
|
+
nodes.each { |cond, value| return value.render(frame) if cond.render(frame) }
|
40
|
+
raise Errors::RenderingError, 'A least one switch() condition must be taken'
|
41
|
+
end
|
42
|
+
|
43
43
|
private
|
44
44
|
|
45
45
|
def optimize_continue_to_switch
|
data/lib/babl/nodes/with.rb
CHANGED
@@ -5,25 +5,34 @@ require 'babl/nodes/constant'
|
|
5
5
|
module Babl
|
6
6
|
module Nodes
|
7
7
|
class With < Utils::Value.new(:node, :nodes, :block)
|
8
|
-
def schema
|
8
|
+
memoize def schema
|
9
9
|
node.schema
|
10
10
|
end
|
11
11
|
|
12
|
-
def dependencies
|
12
|
+
memoize def dependencies
|
13
13
|
Babl::Utils::Hash.deep_merge(
|
14
14
|
*nodes.map(&:dependencies),
|
15
15
|
node.dependencies[Parent::PARENT_MARKER] || Utils::Hash::EMPTY
|
16
16
|
)
|
17
17
|
end
|
18
18
|
|
19
|
-
def pinned_dependencies
|
19
|
+
memoize def pinned_dependencies
|
20
20
|
Babl::Utils::Hash.deep_merge(*(nodes + [node]).map(&:pinned_dependencies))
|
21
21
|
end
|
22
22
|
|
23
|
+
memoize def optimize
|
24
|
+
optimized = node.optimize
|
25
|
+
return optimized if Constant === optimized
|
26
|
+
return optimized.node if Parent === optimized
|
27
|
+
optimized_nodes = nodes.map(&:optimize)
|
28
|
+
return self if optimized.equal?(node) && optimized_nodes.each_with_index.all? { |n, idx| n.equal?(nodes[idx]) }
|
29
|
+
With.new(optimized, optimized_nodes, block)
|
30
|
+
end
|
31
|
+
|
23
32
|
def render(frame)
|
24
|
-
values = nodes.map { |n| n.render(frame) }
|
33
|
+
values = nodes.empty? ? nil : nodes.map { |n| n.render(frame) }
|
25
34
|
value = begin
|
26
|
-
|
35
|
+
nodes.empty? ? frame.object.instance_exec(&block) : block.call(*values)
|
27
36
|
rescue StandardError => e
|
28
37
|
raise Errors::RenderingError, "#{e.message}\n" + frame.formatted_stack(:__block__), e.backtrace
|
29
38
|
end
|
@@ -32,13 +41,6 @@ module Babl
|
|
32
41
|
node.render(new_frame)
|
33
42
|
end
|
34
43
|
end
|
35
|
-
|
36
|
-
def optimize
|
37
|
-
optimized = node.optimize
|
38
|
-
return optimized if Constant === optimized
|
39
|
-
return optimized.node if Parent === optimized
|
40
|
-
With.new(optimized, nodes.map(&:optimize), block)
|
41
|
-
end
|
42
44
|
end
|
43
45
|
end
|
44
46
|
end
|
data/lib/babl/operators/call.rb
CHANGED
@@ -8,7 +8,11 @@ module Babl
|
|
8
8
|
module DSL
|
9
9
|
# Interpret whatever is passed to this method as BABL template. It is idempotent.
|
10
10
|
def call(*args, &block)
|
11
|
-
|
11
|
+
if block
|
12
|
+
raise Errors::InvalidTemplate, 'call() expects no argument when a block is given' unless args.empty?
|
13
|
+
return with(&block)
|
14
|
+
end
|
15
|
+
|
12
16
|
raise Errors::InvalidTemplate, 'call() expects exactly 1 argument (unless block)' unless args.size == 1
|
13
17
|
|
14
18
|
arg = args.first
|
@@ -5,7 +5,7 @@ require 'babl/utils'
|
|
5
5
|
|
6
6
|
module Babl
|
7
7
|
module Rendering
|
8
|
-
class CompiledTemplate < Utils::Value.new(:node, :dependencies, :preloader, :pretty, :
|
8
|
+
class CompiledTemplate < Utils::Value.new(:node, :dependencies, :preloader, :pretty, :schema)
|
9
9
|
def json(root)
|
10
10
|
data = render(root)
|
11
11
|
::MultiJson.dump(data, pretty: pretty)
|
@@ -17,6 +17,10 @@ module Babl
|
|
17
17
|
frame.object = preloaded_data
|
18
18
|
node.render(frame)
|
19
19
|
end
|
20
|
+
|
21
|
+
memoize def json_schema
|
22
|
+
schema.json
|
23
|
+
end
|
20
24
|
end
|
21
25
|
end
|
22
26
|
end
|
data/lib/babl/utils/dsl_proxy.rb
CHANGED
@@ -17,7 +17,7 @@ module Babl
|
|
17
17
|
undef_method(method) unless NON_PROXIED_METHODS.include?(method)
|
18
18
|
end
|
19
19
|
|
20
|
-
# rubocop:disable Style/
|
20
|
+
# rubocop:disable Style/MethodMissingSuper
|
21
21
|
def method_missing(method, *args, &block)
|
22
22
|
if @__receiver__.respond_to?(method)
|
23
23
|
@__receiver__.__send__(method, *args, &block)
|
@@ -25,7 +25,7 @@ module Babl
|
|
25
25
|
@__fallback__.__send__(method, *args, &block)
|
26
26
|
end
|
27
27
|
end
|
28
|
-
# rubocop:enable Style/
|
28
|
+
# rubocop:enable Style/MethodMissingSuper
|
29
29
|
|
30
30
|
def respond_to_missing?(method, include_private = false)
|
31
31
|
@__receiver__.respond_to?(method, include_private) ||
|
data/lib/babl/utils/value.rb
CHANGED
@@ -8,7 +8,6 @@ module Babl
|
|
8
8
|
# - Properties are assumed deeply immutable (#hash is assumed constant & store permanently)
|
9
9
|
# - Constructor requires all arguments
|
10
10
|
# - #== has the same meaning as #eql?
|
11
|
-
# - The object is frozen
|
12
11
|
#
|
13
12
|
# Goals :
|
14
13
|
# - Create completely immutable value objects
|
@@ -21,14 +20,17 @@ module Babl
|
|
21
20
|
|
22
21
|
clazz = super(:_cached_hash, *fields)
|
23
22
|
clazz.const_set(:FIELDS, fields)
|
24
|
-
clazz.class_eval <<-RUBY
|
23
|
+
clazz.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
25
24
|
def initialize(#{field_aliases.join(',')})
|
26
25
|
super(#{['nil', field_aliases].join(',')})
|
27
26
|
hash
|
28
|
-
freeze
|
29
27
|
end
|
30
28
|
RUBY
|
31
29
|
|
30
|
+
fields.each { |field|
|
31
|
+
clazz.send(:define_method, :"#{field}=") { |*| raise ::RuntimeError, 'Object is immutable' }
|
32
|
+
}
|
33
|
+
|
32
34
|
clazz
|
33
35
|
end
|
34
36
|
|
@@ -40,9 +42,25 @@ module Babl
|
|
40
42
|
eql?(other)
|
41
43
|
end
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
45
|
+
class << self
|
46
|
+
def with(hash = Utils::Hash::EMPTY)
|
47
|
+
raise ::ArgumentError unless ::Hash === hash && (hash.keys - self::FIELDS).empty?
|
48
|
+
new(*self::FIELDS.map { |f| hash.fetch(f) })
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def memoize(method_name)
|
54
|
+
old_name = :"_unmemoized_#{method_name}"
|
55
|
+
alias_method old_name, method_name
|
56
|
+
|
57
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
58
|
+
def #{method_name}
|
59
|
+
return @#{old_name} if defined? @#{old_name}
|
60
|
+
@#{old_name} = #{old_name}
|
61
|
+
end
|
62
|
+
RUBY
|
63
|
+
end
|
46
64
|
end
|
47
65
|
end
|
48
66
|
end
|
data/lib/babl/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: babl-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frederic Terrazzoni
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: coveralls
|
@@ -100,14 +100,14 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - '='
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '0.
|
103
|
+
version: '0.57'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - '='
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '0.
|
110
|
+
version: '0.57'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: multi_json
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|