nero 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6da9456128ceb0fd0eda54c862c013035f17306b8bb08d39ce56dd79407711a2
4
+ data.tar.gz: 3640f4c3c54ff2ebee72698906287a5284a5194a40e30e1e6309d1f48b18d517
5
+ SHA512:
6
+ metadata.gz: d811147b8d78530b57babe751d7b9d95626d0ba691fbbe9baf4acc53dbb515a13520799b60ab5244ebec3bc373c7acd6b0969b821847e97edfe54d6eddc3fa2d
7
+ data.tar.gz: fc47ca669383bc056b6a6788fba0cc7079ef790371227f2054e3a2b9a0033c3ee4b5ee63efb7666a9fb92487426765435ea3e672069744951d7afe194da4b7fc
data/.envrc ADDED
@@ -0,0 +1 @@
1
+ PATH_add bin
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-01-24
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Gert Goet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Nero
2
+
3
+ Have some convenient tags in YAML config files:
4
+
5
+ ```yaml
6
+ development:
7
+ # required value from ENV
8
+ github_token: !env GH_TOKEN
9
+
10
+ # optional
11
+ sentry_dsn: !env? SENTRY_DSN
12
+
13
+ # env-value coerced to integer with default value
14
+ port: !env/integer [PORT, 3000]
15
+
16
+ # combining tags
17
+ assets_folder: !path
18
+ - !env PROJECT_ROOT
19
+ - public/upload/assets
20
+
21
+ option: !env/integer? PORT
22
+
23
+ # Allows for: config[:log].debug?
24
+ log: !str/inquirer
25
+ - !env LOG_LEVEL
26
+
27
+ payment_url: !uri
28
+ - https://
29
+ - !env STRIP_HOST
30
+ - /payment/setup
31
+
32
+ url: !str/format
33
+ - https://%s%s
34
+ - !env HOST
35
+ - /login
36
+
37
+ debug: !env/bool [DEBUG, false]
38
+
39
+ production:
40
+ # this won't raise an error when absent when selecting
41
+ # :development as
42
+ secret: !env SECRET
43
+ ```
44
+
45
+ Add one yourself:
46
+ ```ruby
47
+ Nero.configure do
48
+ add_resolver("foo") do |coder|
49
+ # coder.type is one of :scalar, :seq or :map
50
+ # e.g. respective YAML:
51
+ # ---
52
+ # !foo bar
53
+ # ---
54
+ # !foo
55
+ # - bar
56
+ # ---
57
+ # !foo
58
+ # bar: baz
59
+ #
60
+ # Find the value in the respective attribute, e.g. `coder.scalar`:
61
+ coder.scalar.upcase
62
+ end
63
+ end
64
+ ```
65
+
66
+
67
+
68
+ TODO: Delete this and the text below, and describe your gem
69
+
70
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/nero`. To experiment with that code, run `bin/console` for an interactive prompt.
71
+
72
+ ## Installation
73
+
74
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
75
+
76
+ Install the gem and add to the application's Gemfile by executing:
77
+
78
+ ```bash
79
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
80
+ ```
81
+
82
+ If bundler is not being used to manage dependencies, install the gem by executing:
83
+
84
+ ```bash
85
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
86
+ ```
87
+
88
+ ## Usage
89
+
90
+ TODO: Write usage instructions here
91
+
92
+ ## Development
93
+
94
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
95
+
96
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
97
+
98
+ ## Contributing
99
+
100
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eval/nero.
101
+
102
+ ## License
103
+
104
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
data/lib/nero/util.rb ADDED
@@ -0,0 +1,29 @@
1
+ module Nero
2
+ module Util
3
+ extend self
4
+
5
+ def deep_symbolize_keys(object)
6
+ case object
7
+ when Hash
8
+ object.each_with_object({}) do |(key, value), result|
9
+ result[key.to_sym] = deep_symbolize_keys(value)
10
+ end
11
+ when Array
12
+ object.map { |e| deep_symbolize_keys(e) }
13
+ else
14
+ object
15
+ end
16
+ end
17
+
18
+ def deep_transform_values(object, &block)
19
+ case object
20
+ when Hash
21
+ object.transform_values { |value| deep_transform_values(value, &block) }
22
+ when Array
23
+ object.map { |e| deep_transform_values(e, &block) }
24
+ else
25
+ yield(object)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nero
4
+ VERSION = "0.1.0"
5
+ end
data/lib/nero.rb ADDED
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+ loader = Zeitwerk::Loader.for_gem
5
+ loader.setup
6
+
7
+ require "uri" # why needed?
8
+
9
+ # TODO fail on unknown tag
10
+ # TODO show missing env's at once
11
+ module Nero
12
+ class Error < StandardError; end
13
+
14
+ module Resolvable
15
+ def try_resolve(ctx, object)
16
+ if object.respond_to?(:resolve)
17
+ object.resolve(ctx)
18
+ else
19
+ object
20
+ end
21
+ end
22
+
23
+ def gen_resolve_tryer(ctx)
24
+ method(:try_resolve).curry.call(ctx)
25
+ end
26
+
27
+ def deep_resolve(object, **ctx)
28
+ Util.deep_transform_values(object, &gen_resolve_tryer(ctx))
29
+ end
30
+ end
31
+ extend Resolvable
32
+ private_class_method :try_resolve, :gen_resolve_tryer, :deep_resolve
33
+
34
+ class TagResolver
35
+ include Resolvable
36
+
37
+ def init_with(coder)
38
+ @coder = coder
39
+ end
40
+
41
+ def resolve(ctx)
42
+ resolve_nested!(ctx)
43
+ ctx[:resolvers][@coder.tag].call(@coder)
44
+ end
45
+
46
+ def resolve_nested!(ctx)
47
+ case @coder.type
48
+ when :seq
49
+ @coder.seq.map!(&gen_resolve_tryer(ctx))
50
+ when :map
51
+ @coder.map = deep_resolve(@coder.map, **ctx)
52
+ end
53
+ end
54
+ end
55
+
56
+ def self.add_resolver(name, &block)
57
+ (@resolvers ||= {})["!#{name}"] = block
58
+ end
59
+
60
+ add_resolver("env/integer") do |coder|
61
+ Integer(ENV.fetch(*(coder.scalar || coder.seq)))
62
+ end
63
+
64
+ add_resolver("env/integer?") do |coder|
65
+ Integer(ENV[coder.scalar]) if ENV[coder.scalar]
66
+ end
67
+
68
+ add_resolver("env/bool") do |coder|
69
+ re_true = /y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON/
70
+ re_false = /n|N|no|No|NO|false|False|FALSE|off|Off|OFF/
71
+
72
+ coerce = ->(s) do
73
+ case s
74
+ when TrueClass, FalseClass then s
75
+ when re_true then true
76
+ when re_false then false
77
+ else
78
+ raise "bool value should be one of y(es)/n(o), on/off, true/false (got #{s.inspect})"
79
+ end
80
+ end
81
+
82
+ coerce[ENV.fetch(*(coder.scalar || coder.seq))]
83
+ end
84
+
85
+ add_resolver("env/bool?") do |coder|
86
+ re_true = /y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON/
87
+ re_false = /n|N|no|No|NO|false|False|FALSE|off|Off|OFF/
88
+
89
+ coerce = ->(s) do
90
+ case s
91
+ when TrueClass, FalseClass then s
92
+ when re_true then true
93
+ when re_false then false
94
+ else
95
+ raise "bool value should be one of y(es)/n(o), on/off, true/false (got #{s.inspect})"
96
+ end
97
+ end
98
+
99
+ ENV[coder.scalar] ? coerce[ENV[coder.scalar]] : false
100
+ end
101
+
102
+ add_resolver("env") do |coder|
103
+ ENV.fetch(*(coder.scalar || coder.seq))
104
+ end
105
+
106
+ add_resolver("env?") do |coder|
107
+ fetch_args = coder.scalar ? [coder.scalar, nil] : coder.seq
108
+ ENV.fetch(*fetch_args)
109
+ end
110
+
111
+ add_resolver("path") do |coder|
112
+ Pathname.new(coder.scalar || coder.seq.join("/"))
113
+ end
114
+
115
+ add_resolver("uri") do |coder|
116
+ URI(coder.scalar || coder.seq.join)
117
+ end
118
+
119
+ add_resolver("str/format") do |coder|
120
+ case coder.type
121
+ when :seq
122
+ sprintf(*coder.seq)
123
+ when :map
124
+ m = Util.deep_symbolize_keys(coder.map)
125
+ fmt = m.delete(:fmt)
126
+ sprintf(fmt, m)
127
+ else
128
+ coder.scalar
129
+ end
130
+ end
131
+
132
+ def self.load_config(file, root: nil)
133
+ add_tags!
134
+
135
+ if file.exist?
136
+ unresolved = Util.deep_symbolize_keys(YAML.load_file(file,
137
+ permitted_classes: [Symbol, TagResolver], aliases: true)).then do
138
+ root ? _1[root] : _1
139
+ end
140
+
141
+ deep_resolve(unresolved, resolvers: @resolvers)
142
+ else
143
+ raise "Can't find file #{file}"
144
+ end
145
+ end
146
+
147
+ def self.add_tags!
148
+ @resolvers.keys.each do
149
+ YAML.add_tag(_1, TagResolver)
150
+ end
151
+ end
152
+ private_class_method :add_tags!
153
+ end
154
+
155
+ loader.eager_load if ENV.key?("CI")
data/sig/nero.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Nero
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nero
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gert Goet
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-01-27 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: zeitwerk
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ description: |
27
+ Some convenient YAML-tags...
28
+ - to get environment values: env, env?, env/integer, env/integer?, env/bool, env/bool?.
29
+ - to create a Pathname (!path) or URI (!uri)
30
+ - format a string: !str/format
31
+
32
+ See README for examples.
33
+ email:
34
+ - gert@thinkcreate.dk
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - ".envrc"
40
+ - ".rspec"
41
+ - ".standard.yml"
42
+ - CHANGELOG.md
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - lib/nero.rb
47
+ - lib/nero/util.rb
48
+ - lib/nero/version.rb
49
+ - sig/nero.rbs
50
+ homepage: https://github.com/eval/nero
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/eval/nero
55
+ source_code_uri: https://github.com/eval/nero
56
+ changelog_uri: https://github.com/eval/nero/blob/main/CHANGELOG.md
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 3.1.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.6.3
72
+ specification_version: 4
73
+ summary: Convenience YAML-tags
74
+ test_files: []