babl-json 0.5.3 → 0.5.4

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
  SHA1:
3
- metadata.gz: 14b097a0829fb4d3a127ec290a6768576c9d20ed
4
- data.tar.gz: 4753dead38682900489fff11c25abd23e72d1515
3
+ metadata.gz: 310b4377dc2e8962bd245804fd9ca8ede567748a
4
+ data.tar.gz: 39aa36ae900752cabc18f484d1dc047439f60391
5
5
  SHA512:
6
- metadata.gz: '0809d686259d83c42c7053e049427767216ca3be7282f3a427a45125a7fc5f8e848b6b20771a700cea70a9684663eb9cf209c22128b9e33744d27e00f9944928'
7
- data.tar.gz: 5f5eff014fd6ea456a1518c6a9dbf3681be309b414ad5f306ea5005cf011c2b001177b836c6b3bbcc18658bbbf5581b7bbb85abf9336fb6dc96b802c35facd3a
6
+ metadata.gz: 88716be5c080dd4a545234561898aa6c6ae63194d3b5be8de37721a2473934b185ba2f7bb0accf07a811111250c07fd05ffbab2838bda63dbb1e79e9c11bef59
7
+ data.tar.gz: 7a993254d2dede29615e414a9c82ad1d3023a03aeecd1d6ba512bf3465d61f7f1f2d2addc1ca8c0758e9c11cab9aa356b21ec2276a36e4730a0dd27aaf525d23
@@ -22,6 +22,8 @@ module Babl
22
22
  # Compute dependencies & schema on the non-simplified node tree in order
23
23
  # to catch all errors.
24
24
  tree = precompile(lookup_context: lookup_context)
25
+ validate(tree)
26
+
25
27
  dependencies = tree.dependencies
26
28
  schema = tree.schema
27
29
 
@@ -48,6 +50,10 @@ module Babl
48
50
 
49
51
  protected
50
52
 
53
+ def validate(tree)
54
+ # NOOP
55
+ end
56
+
51
57
  def precompile(node = Nodes::TerminalValue.instance, **context)
52
58
  builder.precompile(node, **context)
53
59
  end
@@ -18,14 +18,14 @@ module Babl
18
18
  nodes.map(&:schema).reduce(Schema::FixedArray::EMPTY) { |a, b| merge_doc(a, b) }
19
19
  end
20
20
 
21
- def render(context, frame)
21
+ def render(frame)
22
22
  out = []
23
23
  nodes.each { |node|
24
- values = node.render(context, frame)
24
+ values = node.render(frame)
25
25
  case values
26
26
  when ::NilClass then nil
27
27
  when ::Array then out.concat(values)
28
- else raise Errors::RenderingError, "Only arrays can be concatenated\n" + context.formatted_stack(frame)
28
+ else raise Errors::RenderingError, "Only arrays can be concatenated\n" + frame.formatted_stack
29
29
  end
30
30
  }
31
31
  out
@@ -5,7 +5,7 @@ require 'babl/utils'
5
5
  module Babl
6
6
  module Nodes
7
7
  class Constant < Utils::Value.new(:value, :schema)
8
- def render(_context, _frame)
8
+ def render(_frame)
9
9
  value
10
10
  end
11
11
 
@@ -4,9 +4,9 @@ require 'babl/utils'
4
4
  module Babl
5
5
  module Nodes
6
6
  class CreatePin < Utils::Value.new(:node, :ref)
7
- def render(context, frame)
8
- context.create_pin(frame, ref) do |new_frame|
9
- node.render(context, new_frame)
7
+ def render(frame)
8
+ frame.create_pin(ref) do |new_frame|
9
+ node.render(new_frame)
10
10
  end
11
11
  end
12
12
 
@@ -4,8 +4,8 @@ require 'babl/utils'
4
4
  module Babl
5
5
  module Nodes
6
6
  class Dep < Utils::Value.new(:node, :path)
7
- def render(context, frame)
8
- node.render(context, frame)
7
+ def render(frame)
8
+ node.render(frame)
9
9
  end
10
10
 
11
11
  def schema
@@ -24,15 +24,15 @@ module Babl
24
24
  node.pinned_dependencies
25
25
  end
26
26
 
27
- def render(context, frame)
27
+ def render(frame)
28
28
  collection = frame.object
29
29
  unless Enumerable === collection
30
- raise Errors::RenderingError, "Not enumerable : #{collection.inspect}\n#{context.formatted_stack(frame)}"
30
+ raise Errors::RenderingError, "Not enumerable : #{collection.inspect}\n#{frame.formatted_stack}"
31
31
  end
32
32
 
33
- collection.each_with_index.map do |value, idx|
34
- context.move_forward(frame, value, idx) do |new_frame|
35
- node.render(context, new_frame)
33
+ collection.map.with_index do |value, idx|
34
+ frame.move_forward(value, idx) do |new_frame|
35
+ node.render(new_frame)
36
36
  end
37
37
  end
38
38
  end
@@ -20,8 +20,8 @@ module Babl
20
20
  nodes.map(&:pinned_dependencies).reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
21
21
  end
22
22
 
23
- def render(context, frame)
24
- nodes.map { |node| node.render(context, frame) }
23
+ def render(frame)
24
+ nodes.map { |node| node.render(frame) }
25
25
  end
26
26
 
27
27
  def optimize
@@ -17,9 +17,9 @@ module Babl
17
17
  node.schema
18
18
  end
19
19
 
20
- def render(context, frame)
21
- context.goto_pin(frame, ref) do |new_frame|
22
- node.render(context, new_frame)
20
+ def render(frame)
21
+ frame.goto_pin(ref) do |new_frame|
22
+ node.render(new_frame)
23
23
  end
24
24
  end
25
25
 
@@ -27,7 +27,7 @@ module Babl
27
27
  Utils::Hash::EMPTY
28
28
  end
29
29
 
30
- def render(_context, frame)
30
+ def render(frame)
31
31
  frame.object
32
32
  end
33
33
 
@@ -20,7 +20,7 @@ module Babl
20
20
  Utils::Hash::EMPTY
21
21
  end
22
22
 
23
- def render(_context, frame)
23
+ def render(frame)
24
24
  ::NilClass === frame.object
25
25
  end
26
26
 
@@ -19,10 +19,10 @@ module Babl
19
19
  nodes.map(&:schema).reduce(Schema::Object::EMPTY) { |a, b| merge_doc(a, b) }
20
20
  end
21
21
 
22
- def render(context, frame)
23
- nodes.map { |node| node.render(context, frame) }.compact.reduce({}) { |acc, val|
22
+ def render(frame)
23
+ nodes.map { |node| node.render(frame) }.compact.reduce({}) { |acc, val|
24
24
  raise Errors::RenderingError, "Only objects can be merged\n" +
25
- context.formatted_stack(frame) unless ::Hash === val
25
+ frame.formatted_stack unless ::Hash === val
26
26
  acc.merge!(val)
27
27
  }
28
28
  end
@@ -24,16 +24,16 @@ module Babl
24
24
  node.pinned_dependencies
25
25
  end
26
26
 
27
- def render(context, frame)
27
+ def render(frame)
28
28
  value = begin
29
29
  object = frame.object
30
30
  ::Hash === object ? object.fetch(property) : object.send(property)
31
31
  rescue StandardError => e
32
- raise Errors::RenderingError, "#{e.message}\n" + context.formatted_stack(frame, property), e.backtrace
32
+ raise Errors::RenderingError, "#{e.message}\n" + frame.formatted_stack(property), e.backtrace
33
33
  end
34
34
 
35
- context.move_forward(frame, value, property) do |new_frame|
36
- node.render(context, new_frame)
35
+ frame.move_forward(value, property) do |new_frame|
36
+ node.render(new_frame)
37
37
  end
38
38
  end
39
39
 
@@ -22,9 +22,9 @@ module Babl
22
22
  Schema::Object.new(properties, false)
23
23
  end
24
24
 
25
- def render(context, frame)
25
+ def render(frame)
26
26
  out = {}
27
- nodes.each { |k, v| out[k] = v.render(context, frame) }
27
+ nodes.each { |k, v| out[k] = v.render(frame) }
28
28
  out
29
29
  end
30
30
 
@@ -7,30 +7,6 @@ module Babl
7
7
  class Parent < Utils::Value.new(:node)
8
8
  PARENT_MARKER = Utils::Ref.new
9
9
 
10
- class Verifier < Utils::Value.new(:node)
11
- def dependencies
12
- deps = node.dependencies
13
- raise Errors::InvalidTemplate, 'Out of context parent dependency' if deps.key? PARENT_MARKER
14
- deps
15
- end
16
-
17
- def schema
18
- node.schema
19
- end
20
-
21
- def pinned_dependencies
22
- node.pinned_dependencies
23
- end
24
-
25
- def render(context, frame)
26
- node.render(context, frame)
27
- end
28
-
29
- def optimize
30
- Verifier.new(node.optimize)
31
- end
32
- end
33
-
34
10
  def schema
35
11
  node.schema
36
12
  end
@@ -43,9 +19,9 @@ module Babl
43
19
  { PARENT_MARKER => node.dependencies }
44
20
  end
45
21
 
46
- def render(context, frame)
47
- context.move_backward(frame) do |new_frame|
48
- node.render(context, new_frame)
22
+ def render(frame)
23
+ frame.move_backward do |new_frame|
24
+ node.render(new_frame)
49
25
  end
50
26
  end
51
27
 
@@ -27,8 +27,8 @@ module Babl
27
27
  Schema::AnyOf.canonicalized(nodes.map(&:last).map(&:schema))
28
28
  end
29
29
 
30
- def render(context, frame)
31
- nodes.each { |cond, value| return value.render(context, frame) if cond.render(context, frame) }
30
+ def render(frame)
31
+ nodes.each { |cond, value| return value.render(frame) if cond.render(frame) }
32
32
  raise Errors::RenderingError, 'A least one switch() condition must be taken'
33
33
  end
34
34
 
@@ -24,10 +24,10 @@ module Babl
24
24
  Utils::Hash::EMPTY
25
25
  end
26
26
 
27
- def render(context, frame)
27
+ def render(frame)
28
28
  render_object(frame.object)
29
29
  rescue TerminalValueError => e
30
- raise Errors::RenderingError, "#{e.message}\n" + context.formatted_stack(frame, e.babl_stack), e.backtrace
30
+ raise Errors::RenderingError, "#{e.message}\n" + frame.formatted_stack(e.babl_stack), e.backtrace
31
31
  end
32
32
 
33
33
  def render_object(obj, stack = nil)
@@ -28,13 +28,13 @@ module Babl
28
28
  Schema::Typed::STRING
29
29
  end
30
30
 
31
- def render(context, frame)
31
+ def render(frame)
32
32
  value = frame.object
33
33
  return value if ::String === value
34
34
  return value.to_s if ::Symbol === value
35
35
 
36
36
  raise Errors::RenderingError,
37
- "Expected a string, got #{value.inspect}\n#{context.formatted_stack(frame)}"
37
+ "Expected a string, got #{value.inspect}\n#{frame.formatted_stack}"
38
38
  end
39
39
  end
40
40
 
@@ -43,12 +43,12 @@ module Babl
43
43
  Schema::Typed::INTEGER
44
44
  end
45
45
 
46
- def render(context, frame)
46
+ def render(frame)
47
47
  value = frame.object
48
48
  return value if ::Integer === value
49
49
 
50
50
  raise Errors::RenderingError,
51
- "Expected an integer, got #{value.inspect}\n#{context.formatted_stack(frame)}"
51
+ "Expected an integer, got #{value.inspect}\n#{frame.formatted_stack}"
52
52
  end
53
53
  end
54
54
 
@@ -57,13 +57,13 @@ module Babl
57
57
  Schema::Typed::NUMBER
58
58
  end
59
59
 
60
- def render(context, frame)
60
+ def render(frame)
61
61
  value = frame.object
62
62
  return value if ::Integer === value
63
63
  return value.to_f if ::Numeric === value
64
64
 
65
65
  raise Errors::RenderingError,
66
- "Expected a number, got #{value.inspect}\n#{context.formatted_stack(frame)}"
66
+ "Expected a number, got #{value.inspect}\n#{frame.formatted_stack}"
67
67
  end
68
68
  end
69
69
 
@@ -72,12 +72,12 @@ module Babl
72
72
  Schema::Typed::BOOLEAN
73
73
  end
74
74
 
75
- def render(context, frame)
75
+ def render(frame)
76
76
  value = frame.object
77
77
  return value if true == value || false == value
78
78
 
79
79
  raise Errors::RenderingError,
80
- "Expected a boolean, got #{value.inspect}\n#{context.formatted_stack(frame)}"
80
+ "Expected a boolean, got #{value.inspect}\n#{frame.formatted_stack}"
81
81
  end
82
82
  end
83
83
  end
@@ -23,16 +23,16 @@ module Babl
23
23
  .reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
24
24
  end
25
25
 
26
- def render(context, frame)
27
- values = nodes.map { |n| n.render(context, frame) }
26
+ def render(frame)
27
+ values = nodes.map { |n| n.render(frame) }
28
28
  value = begin
29
29
  block.arity.zero? ? frame.object.instance_exec(&block) : block.call(*values)
30
30
  rescue StandardError => e
31
- raise Errors::RenderingError, "#{e.message}\n" + context.formatted_stack(frame, :__block__), e.backtrace
31
+ raise Errors::RenderingError, "#{e.message}\n" + frame.formatted_stack(:__block__), e.backtrace
32
32
  end
33
33
 
34
- context.move_forward(frame, value, :__block__) do |new_frame|
35
- node.render(context, new_frame)
34
+ frame.move_forward(value, :__block__) do |new_frame|
35
+ node.render(new_frame)
36
36
  end
37
37
  end
38
38
 
@@ -12,9 +12,11 @@ module Babl
12
12
 
13
13
  protected
14
14
 
15
- # Override TemplateBase#precompile to add parent dependencies verification
16
- def precompile(*)
17
- Nodes::Parent::Verifier.new(super)
15
+ def validate(tree)
16
+ if tree.dependencies.key? Nodes::Parent::PARENT_MARKER
17
+ raise Errors::InvalidTemplate, 'Out of context parent dependency'
18
+ end
19
+ super
18
20
  end
19
21
  end
20
22
  end
@@ -8,25 +8,31 @@ module Babl
8
8
  module Pin
9
9
  module DSL
10
10
  # Create a pin
11
- def pin(navigation = nil, &block)
12
- return pin { |p| block[p.call(navigation)] } if navigation
11
+ def pin(navigation = unscoped, &block)
13
12
  ref = Utils::Ref.new
13
+ named_pin(ref).call(block[unscoped.goto_pin(ref).call(navigation)])
14
+ end
14
15
 
15
- referenced_scope = unscoped.construct_node(key: nil, continue: nil) { |node|
16
- Nodes::GotoPin.new(node, ref)
17
- }
18
-
16
+ def named_pin(ref)
17
+ check_pin_ref(ref)
19
18
  construct_node(continue: nil) { |node| Nodes::CreatePin.new(node, ref) }
20
- .call(block[referenced_scope])
19
+ end
20
+
21
+ def goto_pin(ref)
22
+ check_pin_ref(ref)
23
+ construct_node(key: nil, continue: nil) { |node| Nodes::GotoPin.new(node, ref) }
21
24
  end
22
25
 
23
26
  protected
24
27
 
25
- # Override TemplateBase#precompile to ensure that all pin dependencies are satisfied.
26
- def precompile(*)
27
- super.tap do |node|
28
- raise Errors::InvalidTemplate, 'Unresolved pin' unless node.pinned_dependencies.empty?
29
- end
28
+ def validate(tree)
29
+ name = tree.pinned_dependencies.keys.first
30
+ raise Errors::InvalidTemplate, "Unresolved pin: #{name}" if name
31
+ super
32
+ end
33
+
34
+ def check_pin_ref(ref)
35
+ raise Errors::InvalidTemplate, 'Pin name must be a symbol' unless Utils::Ref === ref || ::Symbol === ref
30
36
  end
31
37
  end
32
38
  end
@@ -18,23 +18,28 @@ module Babl
18
18
 
19
19
  def cached_call(template)
20
20
  cached_templates[template.identifier] ||= Babl.compile {
21
- source(template.source, template.identifier)
21
+ pin { |root|
22
+ nav(:context).named_pin(:rails_context).call(root.nav(:locals))
23
+ .source(template.source, template.identifier)
24
+ }
22
25
  }
23
26
 
24
27
  <<-RUBY
25
28
  compiled = ::Babl::ActionView::TemplateHandler.cached_templates[#{template.identifier.inspect}]
26
- compiled.json(local_assigns)
29
+ compiled.json(locals: local_assigns, context: self)
27
30
  RUBY
28
31
  end
29
32
 
30
33
  def uncached_call(template)
31
34
  <<-RUBY
32
35
  Babl.compile {
33
- source(
36
+ pin do |root|
37
+ nav(:context).named_pin(:rails_context).call(root.nav(:locals)).source(
34
38
  #{template.source.inspect},
35
39
  #{template.identifier.inspect}
36
- )
37
- }.json(local_assigns)
40
+ )
41
+ end
42
+ }.json(locals: local_assigns, context: self)
38
43
  RUBY
39
44
  end
40
45
  end
@@ -13,9 +13,9 @@ module Babl
13
13
 
14
14
  def render(root)
15
15
  preloaded_data = preloader.preload([root], dependencies).first
16
- context = Context.new
17
- frame = Context::Frame.new(preloaded_data)
18
- node.render(context, frame)
16
+ frame = Context.new
17
+ frame.object = preloaded_data
18
+ node.render(frame)
19
19
  end
20
20
  end
21
21
  end
@@ -5,86 +5,79 @@ require 'babl/utils'
5
5
  module Babl
6
6
  module Rendering
7
7
  class Context
8
- class Frame
9
- attr_accessor :object, :key, :parent, :pins
8
+ attr_accessor :object, :key, :parent, :pins, :freelist
10
9
 
11
- def initialize(object = nil)
12
- @object = object
13
- end
10
+ def initialize(freelist = [])
11
+ @freelist = freelist
14
12
  end
15
13
 
16
- def initialize
17
- @freelist = []
18
- end
14
+ def move_forward(new_object, key)
15
+ new_frame = @freelist.pop || Context.new(freelist)
19
16
 
20
- def move_forward(current_frame, new_object, key)
21
- with_frame do |new_frame|
22
- new_frame.object = new_object
23
- new_frame.key = key
24
- new_frame.parent = current_frame
25
- new_frame.pins = current_frame.pins
17
+ new_frame.object = new_object
18
+ new_frame.key = key
19
+ new_frame.parent = self
20
+ new_frame.pins = pins
26
21
 
27
- yield new_frame
28
- end
22
+ yield new_frame
23
+ ensure
24
+ @freelist << new_frame
29
25
  end
30
26
 
31
- def move_backward(current_frame)
32
- with_frame do |new_frame|
33
- parent_frame = current_frame.parent
34
- raise Errors::RenderingError, 'There is no parent element' unless parent_frame
27
+ def move_backward
28
+ new_frame = @freelist.pop || Context.new(freelist)
29
+
30
+ parent_frame = parent
31
+ raise Errors::RenderingError, 'There is no parent element' unless parent_frame
35
32
 
36
- new_frame.object = parent_frame.object
37
- new_frame.parent = parent_frame.parent
38
- new_frame.key = parent_frame.key
39
- new_frame.pins = current_frame.pins
33
+ new_frame.object = parent_frame.object
34
+ new_frame.parent = parent_frame.parent
35
+ new_frame.key = parent_frame.key
36
+ new_frame.pins = pins
40
37
 
41
- yield new_frame
42
- end
38
+ yield new_frame
39
+ ensure
40
+ @freelist << new_frame
43
41
  end
44
42
 
45
- def goto_pin(current_frame, ref)
46
- current_pins = current_frame.pins || Utils::Hash::EMPTY
43
+ def goto_pin(ref)
44
+ current_pins = pins || Utils::Hash::EMPTY
47
45
  pin_frame = current_pins[ref]
48
46
  raise Errors::RenderingError, 'Pin reference cannot be used here' unless pin_frame
49
47
 
50
- with_frame do |new_frame|
51
- new_frame.object = pin_frame.object
52
- new_frame.parent = pin_frame.parent
53
- new_frame.key = pin_frame.key
54
- new_frame.pins = (pin_frame.pins || Utils::Hash::EMPTY).merge(current_pins)
48
+ new_frame = @freelist.pop || Context.new(freelist)
49
+
50
+ new_frame.object = pin_frame.object
51
+ new_frame.parent = pin_frame.parent
52
+ new_frame.key = pin_frame.key
53
+ new_frame.pins = (pin_frame.pins || Utils::Hash::EMPTY).merge(current_pins)
55
54
 
56
- yield new_frame
57
- end
55
+ yield new_frame
56
+ ensure
57
+ @freelist << new_frame
58
58
  end
59
59
 
60
- def create_pin(current_frame, ref)
61
- with_frame do |new_frame|
62
- new_frame.parent = current_frame.parent
63
- new_frame.object = current_frame.object
64
- new_frame.key = current_frame.key
65
- new_frame.pins = (current_frame.pins || Utils::Hash::EMPTY).merge(ref => new_frame)
60
+ def create_pin(ref)
61
+ new_frame = @freelist.pop || Context.new(freelist)
66
62
 
67
- yield new_frame
68
- end
69
- end
63
+ new_frame.parent = parent
64
+ new_frame.object = object
65
+ new_frame.key = key
66
+ new_frame.pins = (pins || Utils::Hash::EMPTY).merge(ref => new_frame)
70
67
 
71
- def stack(current_frame)
72
- parent_frame = current_frame.parent
73
- (parent_frame ? stack(parent_frame) : Utils::Array::EMPTY) + [current_frame.key].compact
68
+ yield new_frame
69
+ ensure
70
+ @freelist << new_frame
74
71
  end
75
72
 
76
- def formatted_stack(current_frame, *additional_stack_items)
77
- stack_trace = ([:__root__] + stack(current_frame) + additional_stack_items).join('.')
78
- "BABL @ #{stack_trace}"
73
+ def stack
74
+ parent_frame = parent
75
+ (parent_frame ? parent_frame.stack : Utils::Array::EMPTY) + [key].compact
79
76
  end
80
77
 
81
- private
82
-
83
- def with_frame
84
- frame = @freelist.pop || Frame.new
85
- yield frame
86
- ensure
87
- @freelist << frame
78
+ def formatted_stack(*additional_stack_items)
79
+ stack_trace = ([:__root__] + stack + additional_stack_items).join('.')
80
+ "BABL @ #{stack_trace}"
88
81
  end
89
82
  end
90
83
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Babl
3
- VERSION = '0.5.3'
3
+ VERSION = '0.5.4'
4
4
  end
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.3
4
+ version: 0.5.4
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-04-24 00:00:00.000000000 Z
11
+ date: 2018-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coveralls