figjam 1.6.0 → 1.6.2

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: efb30c09f6e2c3d1b9dd649679dfba6dbabd5cbbc5e56c8e9f51fd18082cfa9c
4
- data.tar.gz: b95972fc469e8bfdb651406925fc3af859c0639662fd4464c8b367de1fc23c87
3
+ metadata.gz: e236e7eb38b51e701dca58eac07605257ec0bc3137350900a121ab0b495f1d23
4
+ data.tar.gz: 85c6da2948ff153d87ccca54be070d391b2b5058f905745215af433d27ccadd5
5
5
  SHA512:
6
- metadata.gz: ba4c3648b7ed4c8f1cdc0a8e7df2a8db996511301b626f02ca0921547a996e4d21fddfbe2aabcb4643df6b14277026efa2a00c148ec25d0db93746fb3d978865
7
- data.tar.gz: 00c61bc16d26dc502a4948d986ab528994a00a2aab51f45f0b9150ed17d8be7037660cb4d5b9990d50215c9f50dd44f49c32097af188f590b0384fe3c8c0f3da
6
+ metadata.gz: 58447e8d292762a98f9ff4ae0323eb05c8e49e8856e9ce4650856237f13b4824795742a1edf53a76abc14b7aa2af9512a0b0935b519a376e9c8534799ccd2187
7
+ data.tar.gz: 16131be1397a1764bf9f339cd5fbb227a39d5db1b9ccb22d2b40ae7296889a6209e3d5007536f23b8ae88b928d96eefd7686a47d888bde563a604ae66802383a
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,69 +48,67 @@ 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) && load_yaml(ERB.new(File.read(path)).result) || {}
63
+ private def parse(path)
64
+ (File.exist?(path) && load_yaml(ERB.new(File.read(path)).result)) || {} # nosemgrep
66
65
  end
67
66
 
68
- def load_yaml(source)
67
+ # rubocop:disable Security/YAMLLoad
68
+ private def load_yaml(source)
69
69
  # https://bugs.ruby-lang.org/issues/17866
70
70
  # https://github.com/rails/rails/commit/179d0a1f474ada02e0030ac3bd062fc653765dbe
71
- begin
72
- YAML.load(source, aliases: true) || {}
73
- rescue ArgumentError
74
- YAML.load(source) || {}
75
- end
71
+ YAML.load(source, aliases: true) || {}
72
+ rescue ArgumentError
73
+ YAML.load(source) || {}
76
74
  end
75
+ # rubocop:enable Security/YAMLLoad
77
76
 
78
- def global_configuration
77
+ private def global_configuration
79
78
  raw_configuration.reject { |_, value| value.is_a?(Hash) }
80
79
  end
81
80
 
82
- def environment_configuration
81
+ private def environment_configuration
83
82
  raw_configuration[environment] || {}
84
83
  end
85
84
 
86
- def set(key, value)
85
+ private def set(key, value)
87
86
  unless non_string_warnings_silenced?
88
- non_string_configuration!(key) unless key.is_a?(String)
89
- 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?
90
89
  end
91
90
 
92
91
  ::ENV[key.to_s] = value.nil? ? nil : value.to_s
93
- ::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
94
93
  end
95
94
 
96
- def skip?(key)
95
+ private def skip?(key)
97
96
  ::ENV.key?(key.to_s) && !::ENV.key?(FIGARO_ENV_PREFIX + key.to_s)
98
97
  end
99
98
 
100
- def non_string_warnings_silenced?
99
+ private def non_string_warnings_silenced?
101
100
  SILENCE_STRING_WARNINGS_KEYS.any? { |key|
102
101
  # Allow the silence configuration itself to use non-string keys/values.
103
- 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" }
104
103
  }
105
104
  end
106
105
 
107
- def non_string_configuration!(value)
108
- 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
109
108
  end
110
109
 
111
- def key_skipped!(key)
112
- warn "WARNING: Skipping key #{key.inspect}. Already set in ENV."
110
+ private def key_skipped(key)
111
+ puts "INFO: Skipping key #{key.inspect}. Already set in ENV."
113
112
  end
114
113
  end
115
114
  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.6.0"
2
+ VERSION = "1.6.2".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.6.0
4
+ version: 1.6.2
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-12-20 00:00:00.000000000 Z
11
+ date: 2024-09-10 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.11
282
+ signing_key:
255
283
  specification_version: 4
256
284
  summary: ENV configuration for ruby using yaml files
257
285
  test_files: []