nero 0.6.0 → 0.7.0.rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nero
4
+ class Result
5
+ attr_reader :value, :errors
6
+
7
+ def initialize(value, errors = [])
8
+ @value = value
9
+ @errors = errors
10
+ end
11
+
12
+ def ok? = errors.empty?
13
+
14
+ def value!
15
+ raise ParseError, errors unless ok?
16
+ value
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nero
4
+ class RootPathTag < BaseTag
5
+ def initialize(containing:)
6
+ @containing = containing
7
+ end
8
+
9
+ def resolve(args, context:)
10
+ relative_path = args[0]&.then { |p| p.empty? ? nil : p }
11
+ dir = context.dir
12
+
13
+ loop do
14
+ if File.exist?(File.join(dir, @containing))
15
+ return relative_path ? Pathname.new(File.join(dir, relative_path)) : Pathname.new(dir)
16
+ end
17
+ parent = File.dirname(dir)
18
+ if parent == dir
19
+ context.add_error("could not find #{@containing} in any ancestor of #{context.dir}")
20
+ return nil
21
+ end
22
+ dir = parent
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/nero/version.rb CHANGED
@@ -3,5 +3,5 @@
3
3
  module Nero
4
4
  # NOTE this is written upon release via:
5
5
  # $ rake gem:build[version=0.3.0]
6
- VERSION = "0.6.0"
6
+ VERSION = "0.7.0.rc2"
7
7
  end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nero
4
+ class Visitor < ::Psych::Visitors::ToRuby
5
+ def self.build(tags, ctx)
6
+ visitor = create
7
+ visitor.instance_variable_set(:@nero_tags, tags)
8
+ visitor.instance_variable_set(:@nero_ctx, ctx)
9
+ visitor
10
+ end
11
+
12
+ def visit_Psych_Nodes_Scalar(o)
13
+ handler = find_nero_tag(o.tag)
14
+ return super unless handler
15
+
16
+ o.tag = nil
17
+ handler.resolve([super], context: @nero_ctx)
18
+ end
19
+
20
+ def visit_Psych_Nodes_Sequence(o)
21
+ handler = find_nero_tag(o.tag)
22
+ return super unless handler
23
+
24
+ o.tag = nil
25
+ args = super
26
+ contains_ref?(args) ? Deferred.new(handler, args) : handler.resolve(args, context: @nero_ctx)
27
+ end
28
+
29
+ def visit_Psych_Nodes_Mapping(o)
30
+ handler = find_nero_tag(o.tag)
31
+ return super unless handler
32
+
33
+ o.tag = nil
34
+ args = super
35
+ contains_ref?(args) ? Deferred.new(handler, args) : handler.resolve(args, context: @nero_ctx)
36
+ end
37
+
38
+ private
39
+
40
+ def find_nero_tag(tag)
41
+ return nil unless tag&.start_with?("!")
42
+ @nero_tags[tag[1..]]
43
+ end
44
+
45
+ def contains_ref?(value)
46
+ case value
47
+ when Ref, Deferred then true
48
+ when Hash then value.values.any? { |v| contains_ref?(v) }
49
+ when Array then value.any? { |v| contains_ref?(v) }
50
+ else false
51
+ end
52
+ end
53
+ end
54
+ end