nero 0.6.0 → 0.7.0.rc3

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,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nero
4
+ module Rails
5
+ class DurationTag < BaseTag
6
+ UNITS = %w[seconds minutes hours days weeks months years].freeze
7
+
8
+ def resolve(args, context:)
9
+ amount = args[0]
10
+ unit = args[1]&.to_s
11
+
12
+ unless amount.is_a?(Numeric)
13
+ context.add_error("duration requires a numeric amount, got #{amount.inspect}")
14
+ return nil
15
+ end
16
+
17
+ unless UNITS.include?(unit)
18
+ context.add_error("duration unknown unit #{unit.inspect}, expected one of: #{UNITS.join(", ")}")
19
+ return nil
20
+ end
21
+
22
+ amount.public_send(unit)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nero
4
+ module Rails
5
+ class StringInquirerTag < BaseTag
6
+ def resolve(args, context:)
7
+ value = args[0]&.to_s
8
+ if value.nil? || value.empty?
9
+ context.add_error("str/inquirer requires a non-empty string argument")
10
+ return nil
11
+ end
12
+ ActiveSupport::StringInquirer.new(value)
13
+ end
14
+ end
15
+ end
16
+ end
data/lib/nero/rails.rb ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rails/credentials_tag"
4
+ require_relative "rails/duration_tag"
5
+ require_relative "rails/string_inquirer_tag"
6
+
7
+ module Nero
8
+ module Rails
9
+ module ParserExtension
10
+ def initialize(environ: ENV, root: nil, **opts, &block)
11
+ # &nil: the user's block runs once, below, after the Rails tags, so it can override them.
12
+ super(environ:, root:, **opts, &nil)
13
+ add_tag("credentials", Nero::Rails::CredentialsTag.new)
14
+ add_tag("path/rails_root", RootPathTag.new(containing: "config.ru"))
15
+ add_tag("str/inquirer", Nero::Rails::StringInquirerTag.new)
16
+ add_tag("duration", Nero::Rails::DurationTag.new)
17
+ block&.call(self)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Nero::Parser.prepend(Nero::Rails::ParserExtension)
24
+
25
+ module Nero
26
+ module Rails
27
+ module DefaultEnv
28
+ def config_for(file, env: ::Rails.env, **opts, &block)
29
+ super
30
+ end
31
+
32
+ # Rails.root/config, so config_for works from any directory (specs, rake tasks).
33
+ # Falls back to ./config before the app has a root.
34
+ def config_dir
35
+ ::Rails.root ? Pathname(::Rails.root) / "config" : super
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ Nero.singleton_class.prepend(Nero::Rails::DefaultEnv)
data/lib/nero/railtie.rb CHANGED
@@ -1,10 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rails"
4
+
1
5
  module Nero
2
6
  # @private
3
7
  class Railtie < ::Rails::Railtie
4
- config.before_configuration do
5
- Nero.configure do |nero|
6
- nero.config_dir = Rails.application.paths["config"].existent.first
7
- end
8
- end
9
8
  end
10
9
  end
data/lib/nero/ref.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nero
4
+ class Ref
5
+ attr_reader :path
6
+
7
+ # path: the keys to follow, e.g. ["a", "c"] for `!ref a.c`.
8
+ def initialize(path) = @path = path
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nero
4
+ # `!ref a.c` (preferred) or the sequence form `!ref [a, c]` (as in Nero < 0.7).
5
+ # Segments are split on ".", so `!ref [a.b, c]` is `a.b.c`.
6
+ class RefTag < BaseTag
7
+ def resolve(args, context:)
8
+ ref_path = args.flat_map { _1.to_s.split(".") }
9
+ context.deprecate "!ref [#{args.join(", ")}] is deprecated", use: "!ref #{ref_path.join(".")}", removed_in: "0.8" if args.size > 1
10
+
11
+ Ref.new(ref_path)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nero
4
+ class Result
5
+ attr_reader :value, :errors, :warnings
6
+
7
+ def initialize(value, errors: [], warnings: [])
8
+ @value = value
9
+ @errors = errors
10
+ @warnings = warnings
11
+ end
12
+
13
+ def ok? = errors.empty?
14
+ def warnings? = warnings.any?
15
+
16
+ def value!
17
+ raise ParseError, errors unless ok?
18
+ value
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nero
4
+ # A Pathname below the nearest ancestor directory (of the YAML file) that contains
5
+ # `containing`, e.g. `.git` for `!path/git_root`.
6
+ #
7
+ # root: !path/git_root # the root itself
8
+ # app: !path/git_root config/app.yml # one relative path
9
+ # up: !path/git_root [storage, !env TENANT] # segments, joined
10
+ #
11
+ # Segments are strings or numbers. A nil segment (e.g. a missing `!env?`), an absolute
12
+ # segment or a mapping is an error. The root must exist, the resulting path needn't.
13
+ class RootPathTag < BaseTag
14
+ def initialize(containing:)
15
+ @containing = containing
16
+ end
17
+
18
+ def resolve(args, context:)
19
+ error = invalid_args(args)
20
+ return context.add_error("#{label}: #{error}") && nil if error
21
+
22
+ segments = Array(args).map(&:to_s).reject(&:empty?)
23
+ root = find_root(context) or return
24
+
25
+ segments.empty? ? root : root.join(*segments)
26
+ end
27
+
28
+ private
29
+
30
+ def invalid_args(args)
31
+ return "expected a path or a sequence of path segments, got a mapping" if args.is_a?(Hash)
32
+
33
+ Array(args).each.with_index(1) do |segment, i|
34
+ case segment
35
+ when nil
36
+ return "segment #{i} is nil (use a fallback, e.g. !env [VAR, default])"
37
+ when String, Numeric
38
+ return "segment #{i} (#{segment.inspect}) is absolute; segments are relative to the root" if File.absolute_path?(segment.to_s)
39
+ else
40
+ return "segment #{i} must be a string, got #{segment.class}"
41
+ end
42
+ end
43
+ nil
44
+ end
45
+
46
+ def find_root(context)
47
+ dir = context.dir
48
+ loop do
49
+ return Pathname.new(dir) if File.exist?(File.join(dir, @containing))
50
+
51
+ parent = File.dirname(dir)
52
+ if parent == dir
53
+ context.add_error("could not find #{@containing} in any ancestor of #{context.dir}")
54
+ return
55
+ end
56
+ dir = parent
57
+ end
58
+ end
59
+
60
+ def label = "path relative to the directory containing #{@containing}"
61
+ end
62
+ 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.rc3"
7
7
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nero
4
+ class Visitor < ::Psych::Visitors::ToRuby
5
+ # Like YAML.load (safe_load): only `permitted_classes` (class names or classes) can be
6
+ # instantiated, so `!ruby/object:...` raises Psych::DisallowedClass. Aliases are allowed.
7
+ def self.build(tags, ctx, permitted_classes: Parser::PERMITTED_CLASSES, symbolize_names: false)
8
+ loader = ::Psych::ClassLoader::Restricted.new(permitted_classes.map(&:to_s), [])
9
+ visitor = new(::Psych::ScalarScanner.new(loader), loader, symbolize_names:)
10
+ visitor.instance_variable_set(:@nero_tags, tags)
11
+ visitor.instance_variable_set(:@nero_ctx, ctx)
12
+ visitor
13
+ end
14
+
15
+ def visit_Psych_Nodes_Scalar(o)
16
+ handler = find_nero_tag(o.tag)
17
+ return super unless handler
18
+
19
+ o.tag = nil
20
+ # A bare tag (`a: !tag`) has no args. A quoted scalar stays a string: with an explicit
21
+ # tag Psych doesn't mark it quoted, so `!tag "123"` would become 123 and `!tag ""` nil.
22
+ args = case o.style
23
+ when ::Psych::Nodes::Scalar::SINGLE_QUOTED, ::Psych::Nodes::Scalar::DOUBLE_QUOTED then [o.value]
24
+ else o.value.empty? ? [] : [super]
25
+ end
26
+ handler.resolve(args, context: @nero_ctx)
27
+ end
28
+
29
+ def visit_Psych_Nodes_Sequence(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
+ def visit_Psych_Nodes_Mapping(o)
39
+ handler = find_nero_tag(o.tag)
40
+ return super unless handler
41
+
42
+ o.tag = nil
43
+ args = super
44
+ contains_ref?(args) ? Deferred.new(handler, args) : handler.resolve(args, context: @nero_ctx)
45
+ end
46
+
47
+ private
48
+
49
+ def find_nero_tag(tag)
50
+ return nil unless tag&.start_with?("!")
51
+ @nero_tags[tag[1..]]
52
+ end
53
+
54
+ def contains_ref?(value)
55
+ case value
56
+ when Ref, Deferred then true
57
+ when Hash then value.values.any? { |v| contains_ref?(v) }
58
+ when Array then value.any? { |v| contains_ref?(v) }
59
+ else false
60
+ end
61
+ end
62
+ end
63
+ end