figjam 1.5.0 → 1.6.1

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
  SHA256:
3
- metadata.gz: a0ed678f68603cf0ac2b6a695ae0c0516626c229964edc3e1b8175e2a306e210
4
- data.tar.gz: 46b53becca0e9b613496b1c5699fc7ba4d486d3a95ac841751eafaa9c2fbd91b
3
+ metadata.gz: c19765a2d3f1c74d94a252a078c81ee48082fab4c652e5c2a110dfbe6e8a6a10
4
+ data.tar.gz: 9960fa166227677f8bca0349f73252affb76bb9294ad58a23604f992b35f5da0
5
5
  SHA512:
6
- metadata.gz: f8e492711470e7b4aaa1a999d00d2feedfb9e116d4f93a1393183ad9e4ff4f6c65283150d3ff3d0539b199be45c819db85f4b4f4132caac138ff69b5cd58d828
7
- data.tar.gz: 96f49716a0ba722587c3553b2f2463ed9fa239c5bf498a4e82dd1642ada3a3c7d3e4ab55f9fb57136ca7f041c1fa59d1726c9d696a9ed71444feb50c98c09343
6
+ metadata.gz: 7076ad53317d9110da3111427213c3aa63ba8489071864914bc9330256dcc5cde40ad5810c7effd56b2de917e16339d3b080b59a70839ea1be7868df24f20b2f
7
+ data.tar.gz: f3cb38979cca5dbbc4f5ea5afa72ebe2d34b9f9f085e431e2c73c16dc1a10548a24c082f6fc3fd4c58626f9317b2184b8c35b88c96e2f265ef64843990b252e7
data/README.md CHANGED
@@ -1,6 +1,13 @@
1
+ <img align="right" width="150" src="jamjar.png">
2
+
1
3
  figjam
2
4
  ================
3
5
 
6
+ [![Gem Version](https://img.shields.io/gem/v/figjam?color=green)](https://rubygems.org/gems/figjam)
7
+ [![specs workflow](https://github.com/hlascelles/figjam/actions/workflows/specs.yml/badge.svg)](https://github.com/hlascelles/figjam/actions)
8
+ [![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
9
+
10
+
4
11
  Figjam makes it easy to configure ruby and Rails apps `ENV` values by just using a single YAML file.
5
12
 
6
13
  PRs to applications often need to come with default configuration values, but hardcoding these into
@@ -11,16 +18,35 @@ Figjam encourages you to commit default ENV values to a yaml file in your PRs. T
11
18
  loaded at runtime, but, crucially, can be overridden at any time by supplying a real ENV. This
12
19
  brings the ease of PR creation with the flexibility of runtime ENV changes.
13
20
 
21
+ An example configuration file might look like this:
22
+
23
+ ```yaml
24
+ GOOGLE_TAG_MANAGER_ID: GTM-12345
25
+ PAYMENT_ENDPOINT: https://payments.localhost
26
+
27
+ prelive:
28
+ GOOGLE_TAG_MANAGER_ID: GTM-54321
29
+ PAYMENT_ENDPOINT: https://payments-prelive.example.com
30
+
31
+ production:
32
+ GOOGLE_TAG_MANAGER_ID: GTM-34343
33
+ PAYMENT_ENDPOINT: https://payments.example.com
34
+ ```
14
35
 
15
- ### Usage
36
+ ### Installation
16
37
 
17
- Add Figjam to your Gemfile and `bundle install`:
38
+ Figjam can be used with or without Rails. If using Bundler, add Figjam to your Gemfile and `bundle install`:
18
39
 
19
40
  ```ruby
20
41
  gem "figjam"
21
42
  ```
22
43
 
23
- Then:
44
+ ### Usage with Rails
45
+
46
+ Figjam will load itself automatically if you are using Rails. It will look for a
47
+ `config/application.yml` file for ENV configuration.
48
+
49
+ You can create a default configuration file by running:
24
50
 
25
51
  ```bash
26
52
  $ bundle exec figjam install
@@ -30,10 +56,39 @@ This creates a commented `config/application.yml` file which you can add default
30
56
  Add your own configuration to this file for all environments, and any specific environment
31
57
  overrides.
32
58
 
33
- ### Example
59
+ ### Usage in Rails engines
60
+
61
+ Figjam is perfect for Rails engines. They can have their own configuration file, and
62
+ they can be loaded independently or in addition of the main application. To do this,
63
+ you can create a `config/application.yml` file in your engine, and add this initializer:
64
+
65
+ ```ruby
66
+ Figjam::Application.new(
67
+ environment: ::Rails.env,
68
+ path: File.expand_path("../application.yml", __dir__)
69
+ ).load
70
+ ```
71
+
72
+ ### Usage without Rails
34
73
 
35
- Given the following configuration file, the default value at the top will be used unless
36
- RAILS_ENV matches any of the subsequent keys (like `test`, `prelive`, `produciton`).
74
+ If you are not using Rails, you can load Figjam in your gem. Note, you can
75
+ do this as many times as you like. If you have multiple internal gems, they can
76
+ all use `figjam` to load their own configuration independently.
77
+
78
+ ```ruby
79
+ Figjam::Application.new(
80
+ environment: "some_environment_key",
81
+ path: "#{__dir__}/application.yml"
82
+ ).load
83
+ ```
84
+
85
+ ### How the loaded configuration is used
86
+
87
+ The configuration file is loaded by `figjam` into `ENV` at runtime. If any of the keys are already
88
+ set in the process ENV, then the ENV value will take precedence.
89
+
90
+ In terms of the file itself, the non-indented values at the top are the default values and will be
91
+ used unless RAILS_ENV matches any of the subsequent keys (like `test`, `prelive`, `production`).
37
92
 
38
93
  ```yaml
39
94
  # config/application.yml
@@ -64,8 +119,35 @@ var gtm_id = <%= ENV.fetch("GOOGLE_TAG_MANAGER_ID") %>
64
119
 
65
120
  Note, secrets are not to be provided by figjam, so do not add them to your `application.yml`.
66
121
 
67
- **Please note:** `ENV` is a simple key/value store. All values will be converted
68
- to strings. Deeply nested configuration structures are not possible.
122
+ Deeply nested configuration structures are not possible.
123
+
124
+ ## Silencing type conversion warnings
125
+
126
+ Remember that `ENV` in ruby is a simple key/value store. All values will always be strings.
127
+ This means that the following code will produce string values:
128
+
129
+ ```yaml
130
+ SOME_NUMBER: 3
131
+ SOME_BOOLEAN: true
132
+ ```
133
+
134
+ ```ruby
135
+ # WARNING: Use strings for Figjam configuration. 3 was converted to "3"
136
+ # WARNING: Use strings for Figjam configuration. true was converted to "true"
137
+
138
+ ENV["SOME_NUMBER"] == 3 # => false
139
+ ENV["SOME_NUMBER"] == "3" # => true
140
+ ENV["SOME_BOOLEAN"] == true # => false
141
+ ENV["SOME_BOOLEAN"] == "true" # => true
142
+ ```
143
+
144
+ Because this is a possible cause of sleeper bugs, Figjam emits a warning when it does this
145
+ conversion to a string. If you are confident in your abilities, you can silence it by adding this
146
+ to the top of your `application.yml`:
147
+
148
+ ```yaml
149
+ FIGJAM_SILENCE_STRING_WARNINGS: true
150
+ ```
69
151
 
70
152
  ### Using `Figjam.env`
71
153
 
@@ -93,6 +175,10 @@ Figjam.env.something_else # => nil
93
175
  Figjam.env.something_else? # => false
94
176
  ```
95
177
 
178
+ If you wish to use `ENV` in specs and want to temporarily change what it returns, you should
179
+ definitely look at using the excellent [climate_control](https://github.com/thoughtbot/climate_control)
180
+ gem.
181
+
96
182
  ### Required Keys
97
183
 
98
184
  If a particular configuration value is required but not set, it's appropriate to
@@ -132,8 +218,8 @@ Figjam and dotenv are similar:
132
218
  ### Differences
133
219
 
134
220
  * Configuration File
135
- * Figjam expects a single file.
136
- * Dotenv supports separate files for each environment.
221
+ * Figjam only needs a single file.
222
+ * Dotenv must use separate files for each environment.
137
223
  * Configuration File Format
138
224
  * Figjam expects YAML containing key/value pairs.
139
225
  * Dotenv convention is a collection of `KEY=VALUE` pairs.
@@ -161,7 +247,8 @@ If you're using Spring add `config/application.yml` to the watch list:
161
247
 
162
248
  ## Figjam origins
163
249
 
164
- Figjam started as a direct fork of the [figaro](https://github.com/laserlemon/figaro) rubygem.
250
+ Figjam started as a direct fork of the [figaro](https://github.com/laserlemon/figaro) rubygem. Thanks
251
+ to @laserlemon for the great work on that gem.
165
252
 
166
253
  There are some key differences in philosophy:
167
254
 
@@ -182,3 +269,5 @@ It even aliases the `Figaro` to `Figjam` module namespace for drop-in compatibil
182
269
 
183
270
  Figjam is open source and contributions from the community are encouraged! No
184
271
  contribution is too small.
272
+
273
+ Jam jar image from https://vectorportal.com/
@@ -3,17 +3,18 @@ require "yaml"
3
3
  require "figjam/figaro_alias"
4
4
 
5
5
  module Figjam
6
+ # :reek:TooManyMethods
6
7
  class Application
7
- FIGARO_ENV_PREFIX = "_FIGARO_"
8
+ FIGARO_ENV_PREFIX = "_FIGARO_".freeze
8
9
  SILENCE_STRING_WARNINGS_KEYS = %i[
9
10
  FIGARO_SILENCE_STRING_WARNINGS
10
11
  FIGJAM_SILENCE_STRING_WARNINGS
11
- ]
12
+ ].freeze
12
13
 
13
14
  include Enumerable
14
15
 
15
16
  def initialize(options = {})
16
- @options = options.inject({}) { |m, (k, v)| m[k.to_sym] = v; m }
17
+ @options = options.transform_keys(&:to_sym)
17
18
  end
18
19
 
19
20
  def path
@@ -26,7 +27,7 @@ module Figjam
26
27
 
27
28
  def environment
28
29
  environment = @options.fetch(:environment) { default_environment }
29
- environment.nil? ? nil : environment.to_s
30
+ environment&.to_s
30
31
  end
31
32
 
32
33
  def environment=(environment)
@@ -39,7 +40,7 @@ module Figjam
39
40
 
40
41
  def load
41
42
  each do |key, value|
42
- skip?(key) ? key_skipped!(key) : set(key, value)
43
+ skip?(key) ? key_skipped(key) : set(key, value)
43
44
  end
44
45
  end
45
46
 
@@ -47,58 +48,66 @@ module Figjam
47
48
  configuration.each(&block)
48
49
  end
49
50
 
50
- private
51
-
52
- def default_path
51
+ private def default_path
53
52
  raise NotImplementedError
54
53
  end
55
54
 
56
- def default_environment
55
+ private def default_environment
57
56
  nil
58
57
  end
59
58
 
60
- def raw_configuration
59
+ private def raw_configuration
61
60
  (@parsed ||= Hash.new { |hash, path| hash[path] = parse(path) })[path]
62
61
  end
63
62
 
64
- def parse(path)
65
- File.exist?(path) && YAML.load(ERB.new(File.read(path)).result) || {}
63
+ private def parse(path)
64
+ (File.exist?(path) && load_yaml(ERB.new(File.read(path)).result)) || {} # nosemgrep
65
+ end
66
+
67
+ # rubocop:disable Security/YAMLLoad
68
+ private def load_yaml(source)
69
+ # https://bugs.ruby-lang.org/issues/17866
70
+ # https://github.com/rails/rails/commit/179d0a1f474ada02e0030ac3bd062fc653765dbe
71
+ YAML.load(source, aliases: true) || {}
72
+ rescue ArgumentError
73
+ YAML.load(source) || {}
66
74
  end
75
+ # rubocop:enable Security/YAMLLoad
67
76
 
68
- def global_configuration
77
+ private def global_configuration
69
78
  raw_configuration.reject { |_, value| value.is_a?(Hash) }
70
79
  end
71
80
 
72
- def environment_configuration
81
+ private def environment_configuration
73
82
  raw_configuration[environment] || {}
74
83
  end
75
84
 
76
- def set(key, value)
85
+ private def set(key, value)
77
86
  unless non_string_warnings_silenced?
78
- non_string_configuration!(key) unless key.is_a?(String)
79
- non_string_configuration!(value) unless value.is_a?(String) || value.nil?
87
+ non_string_configuration(key) unless key.is_a?(String)
88
+ non_string_configuration(value) unless value.is_a?(String) || value.nil?
80
89
  end
81
90
 
82
91
  ::ENV[key.to_s] = value.nil? ? nil : value.to_s
83
- ::ENV[FIGARO_ENV_PREFIX + key.to_s] = value.nil? ? nil: value.to_s
92
+ ::ENV[FIGARO_ENV_PREFIX + key.to_s] = value.nil? ? nil : value.to_s
84
93
  end
85
94
 
86
- def skip?(key)
95
+ private def skip?(key)
87
96
  ::ENV.key?(key.to_s) && !::ENV.key?(FIGARO_ENV_PREFIX + key.to_s)
88
97
  end
89
98
 
90
- def non_string_warnings_silenced?
99
+ private def non_string_warnings_silenced?
91
100
  SILENCE_STRING_WARNINGS_KEYS.any? { |key|
92
101
  # Allow the silence configuration itself to use non-string keys/values.
93
- configuration.values_at(key.to_s, key).any? { |cv| cv.to_s == 'true' }
102
+ configuration.values_at(key.to_s, key).any? { |cv| cv.to_s == "true" }
94
103
  }
95
104
  end
96
105
 
97
- def non_string_configuration!(value)
98
- warn "WARNING: Use strings for Figjam configuration. #{value.inspect} was converted to #{value.to_s.inspect}."
106
+ private def non_string_configuration(value)
107
+ warn "WARNING: Use strings for Figjam configuration. #{value.inspect} was converted to #{value.to_s.inspect}." # rubocop:disable Layout/LineLength
99
108
  end
100
109
 
101
- def key_skipped!(key)
110
+ private def key_skipped(key)
102
111
  warn "WARNING: Skipping key #{key.inspect}. Already set in ENV."
103
112
  end
104
113
  end
@@ -6,12 +6,12 @@ module Figjam
6
6
  include Thor::Actions
7
7
 
8
8
  class_option "path",
9
- aliases: ["-p"],
10
- default: "config/application.yml",
11
- desc: "Specify a configuration file path"
9
+ aliases: ["-p"],
10
+ default: "config/application.yml",
11
+ desc: "Specify a configuration file path"
12
12
 
13
13
  def self.source_root
14
- File.expand_path("../install", __FILE__)
14
+ File.expand_path("install", __dir__)
15
15
  end
16
16
 
17
17
  def create_configuration
@@ -13,17 +13,15 @@ module Figjam
13
13
  @options = options
14
14
  end
15
15
 
16
- private
17
-
18
- def env
16
+ private def env
19
17
  ENV.to_hash.update(configuration)
20
18
  end
21
19
 
22
- def configuration
20
+ private def configuration
23
21
  application.configuration
24
22
  end
25
23
 
26
- def application
24
+ private def application
27
25
  @application ||= Figjam::Application.new(options)
28
26
  end
29
27
 
data/lib/figjam/cli.rb CHANGED
@@ -8,9 +8,9 @@ module Figjam
8
8
  desc "install", "Install Figjam"
9
9
 
10
10
  method_option "path",
11
- aliases: ["-p"],
12
- default: "config/application.yml",
13
- desc: "Specify a configuration file path"
11
+ aliases: ["-p"],
12
+ default: "config/application.yml",
13
+ desc: "Specify a configuration file path"
14
14
 
15
15
  def install
16
16
  require "figjam/cli/install"
data/lib/figjam/env.rb CHANGED
@@ -6,15 +6,14 @@ module Figjam
6
6
  key, punctuation = extract_key_from_method(method)
7
7
 
8
8
  case punctuation
9
- when "!" then has_key?(key) || super
9
+ when "!" then has_key?(key) || super # rubocop:disable Style/PreferredHashMethods
10
10
  when "?", nil then true
11
11
  else super
12
12
  end
13
13
  end
14
14
 
15
- private
16
-
17
- def method_missing(method, *)
15
+ # rubocop:disable Style/MissingRespondToMissing
16
+ private def method_missing(method, *)
18
17
  key, punctuation = extract_key_from_method(method)
19
18
 
20
19
  case punctuation
@@ -24,20 +23,23 @@ module Figjam
24
23
  else super
25
24
  end
26
25
  end
26
+ # rubocop:enable Style/MissingRespondToMissing
27
27
 
28
- def extract_key_from_method(method)
28
+ private def extract_key_from_method(method)
29
29
  method.to_s.downcase.match(/^(.+?)([!?=])?$/).captures
30
30
  end
31
31
 
32
- def has_key?(key)
32
+ # rubocop:disable Naming/PredicateName
33
+ private def has_key?(key)
33
34
  ::ENV.any? { |k, _| k.downcase == key }
34
35
  end
36
+ # rubocop:enable Naming/PredicateName
35
37
 
36
- def missing_key!(key)
37
- raise MissingKey.new(key)
38
+ private def missing_key!(key)
39
+ raise MissingKey, key
38
40
  end
39
41
 
40
- def get_value(key)
42
+ private def get_value(key)
41
43
  _, value = ::ENV.detect { |k, _| k.downcase == key }
42
44
  value
43
45
  end
@@ -1,19 +1,17 @@
1
1
  module Figjam
2
2
  module Rails
3
3
  class Application < Figjam::Application
4
- private
5
-
6
- def default_path
7
- rails_not_initialized! unless ::Rails.root
4
+ private def default_path
5
+ rails_not_initialized unless ::Rails.root
8
6
 
9
7
  ::Rails.root.join("config", "application.yml")
10
8
  end
11
9
 
12
- def default_environment
10
+ private def default_environment
13
11
  ::Rails.env
14
12
  end
15
13
 
16
- def rails_not_initialized!
14
+ private def rails_not_initialized
17
15
  raise RailsNotInitialized
18
16
  end
19
17
  end
data/lib/figjam/rails.rb CHANGED
@@ -3,10 +3,10 @@ require "figjam/figaro_alias"
3
3
  begin
4
4
  require "rails"
5
5
  rescue LoadError
6
+ # Cater for no Rails
6
7
  else
7
8
  require "figjam/rails/application"
8
9
 
9
10
  Figjam.adapter = Figjam::Rails::Application
10
11
  require "figjam/rails/railtie"
11
12
  end
12
-
@@ -1,3 +1,3 @@
1
1
  module Figjam
2
- VERSION = "1.5.0"
2
+ VERSION = "1.6.1".freeze
3
3
  end
data/lib/figjam.rb CHANGED
@@ -7,7 +7,7 @@ require "figjam/env"
7
7
  require "figjam/application"
8
8
 
9
9
  module Figjam
10
- extend self
10
+ extend self # rubocop:disable Style/ModuleFunction
11
11
 
12
12
  attr_writer :adapter, :application
13
13
 
@@ -29,7 +29,7 @@ module Figjam
29
29
 
30
30
  def require_keys(*keys)
31
31
  missing_keys = keys.flatten - ::ENV.keys
32
- raise MissingKeys.new(missing_keys) if missing_keys.any?
32
+ raise MissingKeys, missing_keys if missing_keys.any?
33
33
  end
34
34
  end
35
35
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: figjam
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harry Lascelles
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-30 00:00:00.000000000 Z
11
+ date: 2024-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -114,6 +114,20 @@ dependencies:
114
114
  - - ">="
115
115
  - !ruby/object:Gem::Version
116
116
  version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rails
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
117
131
  - !ruby/object:Gem::Dependency
118
132
  name: rake
119
133
  requirement: !ruby/object:Gem::Requirement
@@ -129,7 +143,7 @@ dependencies:
129
143
  - !ruby/object:Gem::Version
130
144
  version: '0'
131
145
  - !ruby/object:Gem::Dependency
132
- name: rails
146
+ name: reek
133
147
  requirement: !ruby/object:Gem::Requirement
134
148
  requirements:
135
149
  - - ">="
@@ -170,6 +184,20 @@ dependencies:
170
184
  - - ">="
171
185
  - !ruby/object:Gem::Version
172
186
  version: '0'
187
+ - !ruby/object:Gem::Dependency
188
+ name: rubocop-performance
189
+ requirement: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ type: :development
195
+ prerelease: false
196
+ version_requirements: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ">="
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
173
201
  - !ruby/object:Gem::Dependency
174
202
  name: rubocop-rspec
175
203
  requirement: !ruby/object:Gem::Requirement
@@ -235,7 +263,7 @@ metadata:
235
263
  source_code_uri: https://github.com/hlascelles/figjam/
236
264
  bug_tracker_uri: https://github.com/hlascelles/figjam/issues
237
265
  rubygems_mfa_required: 'true'
238
- post_install_message:
266
+ post_install_message:
239
267
  rdoc_options: []
240
268
  require_paths:
241
269
  - lib
@@ -243,15 +271,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
243
271
  requirements:
244
272
  - - ">="
245
273
  - !ruby/object:Gem::Version
246
- version: '0'
274
+ version: '3.0'
247
275
  required_rubygems_version: !ruby/object:Gem::Requirement
248
276
  requirements:
249
277
  - - ">="
250
278
  - !ruby/object:Gem::Version
251
279
  version: '0'
252
280
  requirements: []
253
- rubygems_version: 3.1.6
254
- signing_key:
281
+ rubygems_version: 3.5.3
282
+ signing_key:
255
283
  specification_version: 4
256
284
  summary: ENV configuration for ruby using yaml files
257
285
  test_files: []