configurable_from_env 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +1 -1
- data/lib/configurable_from_env/configurable.rb +181 -0
- data/lib/configurable_from_env/version.rb +1 -1
- data/lib/configurable_from_env.rb +2 -2
- metadata +4 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27d7f547fe61a40ab9ca09b0a5cf7761956425ce49393a0dbea15843442f46b8
|
4
|
+
data.tar.gz: 8b6f943e438a4e26372d2cd80f6398e9efa33625d9ced03dc6467aea73caaeed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44c6c8da991c806f85f65946ac0fa8adf105e0f88102c332727b170703dc7b1e3e5954ead71ed0b86f6db090da6d7cce24c35dd853434d0c02b8002ec591b1ec
|
7
|
+
data.tar.gz: 43d2c803fb7ea64bd34dd8f7e3ab37f4672bc789c05f6ea1843600cf9d9a8c462af7797234ed944e763da4da3a4e7e0ef22286329e56a9c1917554eec7ba0359
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
[](https://github.com/mattbrictson/configurable_from_env/actions/workflows/ci.yml)
|
6
6
|
[](https://codeclimate.com/github/mattbrictson/configurable_from_env)
|
7
7
|
|
8
|
-
The `configurable_from_env` gem allows you to define accessors that automatically populate via environment variables. It
|
8
|
+
The `configurable_from_env` gem allows you to define accessors that automatically populate via environment variables. It brings back Active Support's [`config_accessor`](https://github.com/rails/rails/blob/819a94934966eafb6bee6990b18372e1eb91159d/activesupport/lib/active_support/configurable.rb#L111) – which was [deprecated](https://github.com/rails/rails/pull/53970) in Rails 8.1 – and enhances it with a new `:from_env` option.
|
9
9
|
|
10
10
|
> [!NOTE]
|
11
11
|
> This project is experimental. Please open an issue or send me an email and let me know what you think!
|
@@ -0,0 +1,181 @@
|
|
1
|
+
# Copied from https://github.com/rails/rails/blob/8-0-stable/activesupport/lib/active_support/configurable.rb
|
2
|
+
#
|
3
|
+
# Copyright (c) David Heinemeier Hansson
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
require "active_support"
|
25
|
+
require "active_support/concern"
|
26
|
+
require "active_support/ordered_options"
|
27
|
+
|
28
|
+
module ConfigurableFromEnv
|
29
|
+
# = Active Support \Configurable
|
30
|
+
#
|
31
|
+
# Configurable provides a <tt>config</tt> method to store and retrieve
|
32
|
+
# configuration options as an OrderedOptions.
|
33
|
+
module Configurable
|
34
|
+
extend ActiveSupport::Concern
|
35
|
+
|
36
|
+
class Configuration < ActiveSupport::InheritableOptions
|
37
|
+
def compile_methods!
|
38
|
+
self.class.compile_methods!(keys)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Compiles reader methods so we don't have to go through method_missing.
|
42
|
+
def self.compile_methods!(keys)
|
43
|
+
keys.reject { |m| method_defined?(m) }.each do |key|
|
44
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
45
|
+
def #{key}; _get(#{key.inspect}); end
|
46
|
+
RUBY
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module ClassMethods
|
52
|
+
def config
|
53
|
+
@_config ||= if respond_to?(:superclass) && superclass.respond_to?(:config)
|
54
|
+
superclass.config.inheritable_copy
|
55
|
+
else
|
56
|
+
# create a new "anonymous" class that will host the compiled reader methods
|
57
|
+
Class.new(Configuration).new
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def configure
|
62
|
+
yield config
|
63
|
+
end
|
64
|
+
|
65
|
+
# Allows you to add shortcut so that you don't have to refer to attribute
|
66
|
+
# through config. Also look at the example for config to contrast.
|
67
|
+
#
|
68
|
+
# Defines both class and instance config accessors.
|
69
|
+
#
|
70
|
+
# class User
|
71
|
+
# include ActiveSupport::Configurable
|
72
|
+
# config_accessor :allowed_access
|
73
|
+
# end
|
74
|
+
#
|
75
|
+
# User.allowed_access # => nil
|
76
|
+
# User.allowed_access = false
|
77
|
+
# User.allowed_access # => false
|
78
|
+
#
|
79
|
+
# user = User.new
|
80
|
+
# user.allowed_access # => false
|
81
|
+
# user.allowed_access = true
|
82
|
+
# user.allowed_access # => true
|
83
|
+
#
|
84
|
+
# User.allowed_access # => false
|
85
|
+
#
|
86
|
+
# The attribute name must be a valid method name in Ruby.
|
87
|
+
#
|
88
|
+
# class User
|
89
|
+
# include ActiveSupport::Configurable
|
90
|
+
# config_accessor :"1_Badname"
|
91
|
+
# end
|
92
|
+
# # => NameError: invalid config attribute name
|
93
|
+
#
|
94
|
+
# To omit the instance writer method, pass <tt>instance_writer: false</tt>.
|
95
|
+
# To omit the instance reader method, pass <tt>instance_reader: false</tt>.
|
96
|
+
#
|
97
|
+
# class User
|
98
|
+
# include ActiveSupport::Configurable
|
99
|
+
# config_accessor :allowed_access, instance_reader: false, instance_writer: false
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# User.allowed_access = false
|
103
|
+
# User.allowed_access # => false
|
104
|
+
#
|
105
|
+
# User.new.allowed_access = true # => NoMethodError
|
106
|
+
# User.new.allowed_access # => NoMethodError
|
107
|
+
#
|
108
|
+
# Or pass <tt>instance_accessor: false</tt>, to omit both instance methods.
|
109
|
+
#
|
110
|
+
# class User
|
111
|
+
# include ActiveSupport::Configurable
|
112
|
+
# config_accessor :allowed_access, instance_accessor: false
|
113
|
+
# end
|
114
|
+
#
|
115
|
+
# User.allowed_access = false
|
116
|
+
# User.allowed_access # => false
|
117
|
+
#
|
118
|
+
# User.new.allowed_access = true # => NoMethodError
|
119
|
+
# User.new.allowed_access # => NoMethodError
|
120
|
+
#
|
121
|
+
# Also you can pass <tt>default</tt> or a block to set up the attribute with a default value.
|
122
|
+
#
|
123
|
+
# class User
|
124
|
+
# include ActiveSupport::Configurable
|
125
|
+
# config_accessor :allowed_access, default: false
|
126
|
+
# config_accessor :hair_colors do
|
127
|
+
# [:brown, :black, :blonde, :red]
|
128
|
+
# end
|
129
|
+
# end
|
130
|
+
#
|
131
|
+
# User.allowed_access # => false
|
132
|
+
# User.hair_colors # => [:brown, :black, :blonde, :red]
|
133
|
+
def config_accessor(*names, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil) # :doc:
|
134
|
+
names.each do |name|
|
135
|
+
raise NameError.new("invalid config attribute name") unless /\A[_A-Za-z]\w*\z/.match?(name)
|
136
|
+
|
137
|
+
reader, reader_line = "def #{name}; config.#{name}; end", __LINE__
|
138
|
+
writer, writer_line = "def #{name}=(value); config.#{name} = value; end", __LINE__
|
139
|
+
|
140
|
+
singleton_class.class_eval reader, __FILE__, reader_line
|
141
|
+
singleton_class.class_eval writer, __FILE__, writer_line
|
142
|
+
|
143
|
+
if instance_accessor
|
144
|
+
class_eval reader, __FILE__, reader_line if instance_reader
|
145
|
+
class_eval writer, __FILE__, writer_line if instance_writer
|
146
|
+
end
|
147
|
+
|
148
|
+
send("#{name}=", block_given? ? yield : default)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
private :config_accessor
|
152
|
+
|
153
|
+
private
|
154
|
+
def inherited(subclass)
|
155
|
+
super
|
156
|
+
subclass.class_eval do
|
157
|
+
@_config = nil
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# Reads and writes attributes from a configuration OrderedOptions.
|
163
|
+
#
|
164
|
+
# require "active_support/configurable"
|
165
|
+
#
|
166
|
+
# class User
|
167
|
+
# include ActiveSupport::Configurable
|
168
|
+
# end
|
169
|
+
#
|
170
|
+
# user = User.new
|
171
|
+
#
|
172
|
+
# user.config.allowed_access = true
|
173
|
+
# user.config.level = 1
|
174
|
+
#
|
175
|
+
# user.config.allowed_access # => true
|
176
|
+
# user.config.level # => 1
|
177
|
+
def config
|
178
|
+
@_config ||= self.class.config.inheritable_copy
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
require "active_support"
|
2
2
|
require "active_support/concern"
|
3
|
-
require "active_support/configurable"
|
4
3
|
require "active_support/core_ext/enumerable"
|
5
4
|
require "active_support/core_ext/hash/keys"
|
6
5
|
|
7
6
|
module ConfigurableFromEnv
|
7
|
+
autoload :Configurable, "configurable_from_env/configurable"
|
8
8
|
autoload :EnvironmentValue, "configurable_from_env/environment_value"
|
9
9
|
autoload :VERSION, "configurable_from_env/version"
|
10
10
|
|
11
11
|
extend ActiveSupport::Concern
|
12
|
-
include
|
12
|
+
include Configurable
|
13
13
|
|
14
14
|
module ClassMethods
|
15
15
|
def config_accessor(*attributes, from_env: nil, **options, &block)
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configurable_from_env
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Brictson
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-01-25 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: activesupport
|
@@ -24,7 +23,6 @@ dependencies:
|
|
24
23
|
- - ">="
|
25
24
|
- !ruby/object:Gem::Version
|
26
25
|
version: '7.2'
|
27
|
-
description:
|
28
26
|
email:
|
29
27
|
- opensource@mattbrictson.com
|
30
28
|
executables: []
|
@@ -34,6 +32,7 @@ files:
|
|
34
32
|
- LICENSE.txt
|
35
33
|
- README.md
|
36
34
|
- lib/configurable_from_env.rb
|
35
|
+
- lib/configurable_from_env/configurable.rb
|
37
36
|
- lib/configurable_from_env/environment_value.rb
|
38
37
|
- lib/configurable_from_env/version.rb
|
39
38
|
homepage: https://github.com/mattbrictson/configurable_from_env
|
@@ -45,7 +44,6 @@ metadata:
|
|
45
44
|
source_code_uri: https://github.com/mattbrictson/configurable_from_env
|
46
45
|
homepage_uri: https://github.com/mattbrictson/configurable_from_env
|
47
46
|
rubygems_mfa_required: 'true'
|
48
|
-
post_install_message:
|
49
47
|
rdoc_options: []
|
50
48
|
require_paths:
|
51
49
|
- lib
|
@@ -60,8 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
58
|
- !ruby/object:Gem::Version
|
61
59
|
version: '0'
|
62
60
|
requirements: []
|
63
|
-
rubygems_version: 3.
|
64
|
-
signing_key:
|
61
|
+
rubygems_version: 3.6.3
|
65
62
|
specification_version: 4
|
66
63
|
summary: Define accessors that are automatically populated via ENV
|
67
64
|
test_files: []
|