babl-json 0.3.2 → 0.3.3

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: 1244f145154a35f410ff91f14de9dbacd4f679c9
4
- data.tar.gz: fdc4821e46e72c54c248b33c406850421ec548e4
3
+ metadata.gz: b410d536ca032602510a9af64d29a29dc91f536c
4
+ data.tar.gz: 9a002b4137992699842abce14054d9a21d48a92a
5
5
  SHA512:
6
- metadata.gz: 7f1256e9d5a21ba6da758a890eba6e8063b94e2a3d9e26d45c55f262ebc9db95eacbef29f7a920ec67d55626e0a23d3c0f76bcd312d41d3f4de729fc742bd9b7
7
- data.tar.gz: 47b8265e9078140b733cd81bd77baa1dcf40936362998dd8a85b7ad7a3b891a8ae1849f82be60c638fc6ccc3eb5a03d09b4eed137242575a453240d21e77c93b
6
+ metadata.gz: bd1979b476227686cd13d3b994629a36fa95fbd1d55ff7d7cec466fa2304c1a414f5f1cec0a816d879d7eeb667e12934f18561c4a4920aa9be4dabd87d7d4ee0
7
+ data.tar.gz: 6b23b6086b16a94a5594cb1390fc0307881fd3fefeb8ee4e59ee67aede28c564ae7cce2d0d2a069c1417a0f108e426c16d7294bc9f5b717217fc425a546897fd
@@ -11,25 +11,29 @@ module Babl
11
11
 
12
12
  def initialize
13
13
  @search_path = nil
14
- @preloader = Babl::Rendering::NoopPreloader
14
+ @preloader = Rendering::NoopPreloader
15
15
  @pretty = true
16
16
  @cache_templates = false
17
17
  end
18
18
  end
19
19
 
20
20
  class << self
21
- def compile(template: Babl::Template.new, &source)
22
- if config.search_path
23
- ctx = Babl::Operators::Partial::AbsoluteLookupContext.new(config.search_path)
24
- template = template.with_lookup_context(ctx)
25
- end
26
-
27
- template.source(&source).compile(
21
+ def compile(&block)
22
+ source(&block).compile(
28
23
  pretty: config.pretty,
29
24
  preloader: config.preloader
30
25
  )
31
26
  end
32
27
 
28
+ def source(&block)
29
+ template = Template.new
30
+ if config.search_path
31
+ ctx = Operators::Partial::AbsoluteLookupContext.new(config.search_path)
32
+ template = template.with_lookup_context(ctx)
33
+ end
34
+ template.source(&block)
35
+ end
36
+
33
37
  def configure
34
38
  yield(config)
35
39
  end
@@ -38,12 +38,12 @@ module Babl
38
38
  )
39
39
  end
40
40
 
41
- protected
42
-
43
41
  def unscoped
44
42
  self.class.new builder.rescope(&:itself)
45
43
  end
46
44
 
45
+ protected
46
+
47
47
  def precompile
48
48
  builder.precompile(Nodes::TerminalValue.instance)
49
49
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ require 'babl/utils'
2
3
  require 'babl/errors'
3
4
 
4
5
  module Babl
@@ -13,7 +14,8 @@ module Babl
13
14
  arg = args.first
14
15
 
15
16
  case arg
16
- when self.class then self.class.new(builder.wrap { |bound| arg.builder.bind(bound) })
17
+ when Template then self.class.new(builder.wrap { |bound| arg.builder.bind(bound) })
18
+ when Utils::DslProxy then call(arg.itself)
17
19
  when ::Symbol then nav(arg)
18
20
  when ::Proc then call(&arg)
19
21
  when ::Hash then object(**arg.map { |k, v| [:"#{k}", v] }.to_h)
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
+ require 'babl/utils'
2
3
  module Babl
3
4
  module Operators
4
5
  module Source
5
6
  module DSL
6
7
  # Parse BABL source into a Template
7
8
  def source(*args, &block)
8
- call(unscoped.instance_eval(*args, &block))
9
+ call(block ? Utils::DslProxy.eval(unscoped, &block) : unscoped.instance_eval(*args))
9
10
  end
10
11
  end
11
12
  end
@@ -2,3 +2,4 @@
2
2
  require 'babl/utils/hash'
3
3
  require 'babl/utils/ref'
4
4
  require 'babl/utils/value'
5
+ require 'babl/utils/dsl_proxy'
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+ require 'set'
3
+
4
+ module Babl
5
+ module Utils
6
+ # The idea is to make it possible to call method defined in block's context in addition to DSL methods.
7
+ # Inspired from https://github.com/ms-ati/docile/blob/master/lib/docile/fallback_context_proxy.rb, but
8
+ # here we do not try to handle instance variables, because as far as I know there is no way to do it
9
+ # correctly.
10
+ class DslProxy
11
+ NON_PROXIED_METHODS = Set[
12
+ :__send__, :send, :object_id, :__id__, :equal?, :instance_eval
13
+ ]
14
+
15
+ instance_methods.each do |method|
16
+ undef_method(method) unless NON_PROXIED_METHODS.include?(method)
17
+ end
18
+
19
+ # rubocop:disable Style/MethodMissing
20
+ def method_missing(method, *args, &block)
21
+ if @__receiver__.respond_to?(method)
22
+ @__receiver__.__send__(method, *args, &block)
23
+ else
24
+ @__fallback__.__send__(method, *args, &block)
25
+ end
26
+ end
27
+ # rubocop:enable Style/MethodMissing
28
+
29
+ def respond_to_missing?(method, include_private = false)
30
+ @__receiver__.respond_to?(method, include_private) ||
31
+ @__fallback__.respond_to?(method, include_private)
32
+ end
33
+
34
+ def self.eval(dsl, *args, &block)
35
+ new(dsl, block.binding.receiver).instance_eval(&block)
36
+ end
37
+
38
+ def initialize(receiver, fallback)
39
+ @__receiver__ = receiver
40
+ @__fallback__ = fallback
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Babl
3
- VERSION = '0.3.2'
3
+ VERSION = '0.3.3'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: babl-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederic Terrazzoni
@@ -189,6 +189,7 @@ files:
189
189
  - lib/babl/schema/typed.rb
190
190
  - lib/babl/template.rb
191
191
  - lib/babl/utils.rb
192
+ - lib/babl/utils/dsl_proxy.rb
192
193
  - lib/babl/utils/hash.rb
193
194
  - lib/babl/utils/ref.rb
194
195
  - lib/babl/utils/value.rb