nitro_config 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: c7055701182010d5935ca398768db0f1d5dffeeda042bd7096278beaf6504da9
4
+ data.tar.gz: 3bd0acd7ea41fcf58dfdca6811bb50a9ccf87df0619633908d5bd150a0aecb86
5
+ SHA512:
6
+ metadata.gz: 7036c60be8e7b4ba9842220e5c8ff752d5d3d44a0256d20458f851aa77cd3cc1a9a76a546df0b0aa7536e1d55b92a6e92b641fd71db04471a19b4beaa473905c
7
+ data.tar.gz: f14127304b5d4880b09cd467406b1941d63aa68662789b3e5efac5ad48aa38cc6e6500c47f4609b61e3311ce800d7a773c5e55504f62aed7b256ce7ac4febc53
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env rake
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require "rspec/core/rake_task"
6
+ require "rubocop/rake_task"
7
+
8
+ begin
9
+ require "bundler/setup"
10
+ rescue LoadError
11
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
12
+ end
13
+ begin
14
+ require "yard"
15
+ YARD::Rake::YardocTask.new do |t|
16
+ t.files = ["lib/**/*.rb"]
17
+ t.options = [
18
+ "--no-private",
19
+ ]
20
+ end
21
+ rescue LoadError
22
+ warn "Could not require() YARD! Install with 'gem install yard' to get the 'yardoc' task"
23
+ end
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ RSpec::Core::RakeTask.new(:spec)
28
+
29
+ RuboCop::RakeTask.new(:rubocop)
30
+
31
+ task default: %i[spec rubocop]
data/docs/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # NitroConfig
2
+
3
+ When included in a Rails application, NitroConfig loads the configuration file at `config/config.yml` within the application directory and makes its values available at `NitroConfig.config`. Config values are loaded based on the Rails environment, permitting the specification of multiple environments' configurations in a single file.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'nitro_config'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ Given the following example config file, the following examples demonstrate how to obtain these values.
20
+
21
+ ```yaml
22
+ base: &base
23
+ some:
24
+ nested: value
25
+
26
+ development:
27
+ <<: *base
28
+ some:
29
+ nested: devvalue
30
+
31
+ test:
32
+ <<: *base
33
+ some:
34
+ nested: testvalue
35
+
36
+ ```
37
+
38
+ $ rails c
39
+ [1] pry(main)> NitroConfig.config.get('some/nested')
40
+ => "devvalue"
41
+ [2] pry(main)> NitroConfig.config.get('some/other')
42
+ => nil
43
+ [3] pry(main)> NitroConfig.config.get!('some/other')
44
+ NitroConfig::Error: some/other not found in app config!
45
+ from /Users/ben/code/power/nitro/components/nitro_config/lib/nitro_config/options.rb:20:in `block in get!'
46
+ [2] pry(main)> NitroConfig.config.get('some/other', 'default')
47
+ => "default"
48
+
49
+ $ RAILS_ENV=test rails c
50
+ [1] pry(main)> NitroConfig.config.get('some/nested')
51
+ => "testvalue"
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NitroConfig
4
+ # Raised when a path cannot be found in the config tree
5
+ class Error < StandardError
6
+ def initialize(path)
7
+ super "#{path} not found in app config! If you're working in development, you probably need to" \
8
+ "`cp config/config_sample.yml config/config.yml` or create a symlink for convenience."
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/hash/indifferent_access"
4
+ require "active_support/core_ext/object/try"
5
+ require "active_support/hash_with_indifferent_access"
6
+
7
+ require "nitro_config/error"
8
+
9
+ module NitroConfig
10
+ PATH_SEPARATOR = "/"
11
+
12
+ # Representation of a config key-value tree with path-based access
13
+ class Options < HashWithIndifferentAccess
14
+ # Preserves values in the global configuration which might be altered within the yielded block.
15
+ # This is useful for testing, where a test needs to assert behaviour with certain settings values,
16
+ # but you want them to be restored for the next test.
17
+ #
18
+ # @example Preserving configuration in all tests
19
+ # config.around(:each) do |example|
20
+ # NitroConfig.preserve! do
21
+ # example.run
22
+ # end
23
+ # end
24
+ # OR
25
+ # config.include NitroConfig::Rspec
26
+ #
27
+ # @yield A block within which configuration can safely be altered, being restored on return
28
+ def preserve!
29
+ tmp = clone
30
+ yield
31
+ replace(tmp)
32
+ end
33
+
34
+ # Returns a configuration value by path
35
+ #
36
+ # @param [String,Array] path the path within the configuration to return, with nested keys "separated/like/this"
37
+ # @param default a default value to return if the path is not found in the configuration
38
+ #
39
+ # @return The value extracted from configuration, of the type specified when declared.
40
+ def get(path, default = nil)
41
+ get!(path)
42
+ rescue NitroConfig::Error
43
+ default
44
+ end
45
+
46
+ # Returns a configuration value by path
47
+ #
48
+ # @param [String,Array] path the path within the configuration to return, with
49
+ # nested keys "separated/like/this", ['or', 'like', 'this']
50
+ #
51
+ # @return The value extracted from configuration, of the type specified when declared.
52
+ #
53
+ # @raise [NitroConfig::Error] when the configuration option is not found
54
+ def get!(path)
55
+ split_path = path.respond_to?(:split) ? path.split(PATH_SEPARATOR) : path
56
+ split_path.flatten.reduce(self) do |config, key|
57
+ raise(NitroConfig::Error, path) unless config.try(:has_key?, key)
58
+
59
+ config[key]
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NitroConfig
4
+ # Hook into Rails initialisation to make config available on application boot
5
+ # @private
6
+ class Railtie < Rails::Railtie
7
+ config.before_configuration do |app|
8
+ NitroConfig.load! app.root.join("config", "config.yml"), Rails.env
9
+ rescue Errno::ENOENT => e
10
+ abort("Failed to initialize Nitro Config for #{app}:\n#{e.message}")
11
+ end
12
+ end
13
+
14
+ private_constant :Railtie
15
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ require "nitro_config/options"
6
+
7
+ # When included in a Rails application, NitroConfig loads the
8
+ # configuration file at `config/config.yml` within the application
9
+ # directory and makes its values available at {NitroConfig.config}.
10
+ #
11
+ # Config values are loaded based on the Rails environment, permitting
12
+ # the specification of multiple environments' configurations in a
13
+ # single file.
14
+ module NitroConfig
15
+ module Rspec
16
+ def self.included(base)
17
+ base.around(:each) do |example|
18
+ NitroConfig.config.preserve! do
19
+ example.run
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NitroConfig
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ require "nitro_config/options"
6
+
7
+ # When included in a Rails application, NitroConfig loads the
8
+ # configuration file at `config/config.yml` within the application
9
+ # directory and makes its values available at {NitroConfig.config}.
10
+ #
11
+ # Config values are loaded based on the Rails environment, permitting
12
+ # the specification of multiple environments' configurations in a
13
+ # single file.
14
+ module NitroConfig
15
+ # Loads a configuration file as the global config, making it accessible via {NitroConfig.config}
16
+ #
17
+ # @param [String] file path to the config file to be loaded
18
+ # @param [String] env the environment to load from the file (top level key)
19
+ #
20
+ # @return [NitroConfig::Options] the loaded configuration
21
+ def self.load!(file, env)
22
+ @config = NitroConfig::Options.new(YAML.load_file(file)[env])
23
+ end
24
+
25
+ # Provides the loaded global configuration
26
+ #
27
+ # @return [NitroConfig::Options] the loaded configuration
28
+ def self.config
29
+ @config ||= NitroConfig::Options.new
30
+ end
31
+
32
+ # @see NitroConfig::Options#get
33
+ def self.get(*args)
34
+ config.get(*args)
35
+ end
36
+
37
+ # @see NitroConfig::Options#get
38
+ def self.get!(*args)
39
+ config.get!(*args)
40
+ end
41
+
42
+ # Returns a function that will return the requested config when invoked.
43
+ # This method is useful for fetching configurations in declarative contexts where the code will be invoked
44
+ # in boot time. This will defer the fetching of the config to when needed.
45
+ #
46
+ # @see NitroConfig::Options#get
47
+ # @return [Proc] a proc that takes any number of arguments and returns the requested config
48
+ def self.get_deferred!(*args)
49
+ ->(*_args) { config.get!(*args) }
50
+ end
51
+
52
+ require "nitro_config/railtie" if defined?(Rails)
53
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nitro_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Palhares
8
+ - Jill Klang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2022-07-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '='
19
+ - !ruby/object:Gem::Version
20
+ version: 5.2.8
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '='
26
+ - !ruby/object:Gem::Version
27
+ version: 5.2.8
28
+ - !ruby/object:Gem::Dependency
29
+ name: license_finder
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '7.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '7.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: pry-byebug
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '='
47
+ - !ruby/object:Gem::Version
48
+ version: 3.9.0
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '='
54
+ - !ruby/object:Gem::Version
55
+ version: 3.9.0
56
+ - !ruby/object:Gem::Dependency
57
+ name: rainbow
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '='
61
+ - !ruby/object:Gem::Version
62
+ version: 2.2.2
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: 2.2.2
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '13.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '13.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '='
89
+ - !ruby/object:Gem::Version
90
+ version: 3.9.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '='
96
+ - !ruby/object:Gem::Version
97
+ version: 3.9.0
98
+ - !ruby/object:Gem::Dependency
99
+ name: rubocop-powerhome
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '='
103
+ - !ruby/object:Gem::Version
104
+ version: 0.4.1
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '='
110
+ - !ruby/object:Gem::Version
111
+ version: 0.4.1
112
+ - !ruby/object:Gem::Dependency
113
+ name: simplecov
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '='
117
+ - !ruby/object:Gem::Version
118
+ version: 0.15.1
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '='
124
+ - !ruby/object:Gem::Version
125
+ version: 0.15.1
126
+ - !ruby/object:Gem::Dependency
127
+ name: yard
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '='
131
+ - !ruby/object:Gem::Version
132
+ version: 0.9.21
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '='
138
+ - !ruby/object:Gem::Version
139
+ version: 0.9.21
140
+ description: Loads Nitro configuration and makes it available to the application
141
+ email:
142
+ - chjunior@gmail.com
143
+ - jillian.emilie@gmail.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - Rakefile
149
+ - docs/README.md
150
+ - lib/nitro_config.rb
151
+ - lib/nitro_config/error.rb
152
+ - lib/nitro_config/options.rb
153
+ - lib/nitro_config/railtie.rb
154
+ - lib/nitro_config/rspec.rb
155
+ - lib/nitro_config/version.rb
156
+ homepage: https://github.com/powerhome/power-tools/blob/main/packages/nitro_config/docs/README.md
157
+ licenses:
158
+ - MIT
159
+ metadata:
160
+ rubygems_mfa_required: 'true'
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '2.7'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubygems_version: 3.3.7
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: Nitro Configuration Loader
180
+ test_files: []