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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c87398f6f8863cf380719eb8e54029579cbc2941476a97b522b45a256ab996da
4
- data.tar.gz: 8cbf8cf83eb5ce937b22b874fecd12e0ea8ed8161944df184cf79b0ec4d6e595
3
+ metadata.gz: 27d7f547fe61a40ab9ca09b0a5cf7761956425ce49393a0dbea15843442f46b8
4
+ data.tar.gz: 8b6f943e438a4e26372d2cd80f6398e9efa33625d9ced03dc6467aea73caaeed
5
5
  SHA512:
6
- metadata.gz: 2dfeb692f554bd8db7c5ac4d2c3c605f0eec120a82ea9565468b4a1b5c0b508c1c5bc498488719cdd5980f08e26d200a11f71c0abf6e6fe41577b6f21f09156e
7
- data.tar.gz: ee98578923a3262c77e75eece7a8f7bee05f268738a2b4f81d488507f9ed2ca9c188c10856ec2c20b04a1a660b1df68b36313f63191110eb665aabef08d9a384
6
+ metadata.gz: 44c6c8da991c806f85f65946ac0fa8adf105e0f88102c332727b170703dc7b1e3e5954ead71ed0b86f6db090da6d7cce24c35dd853434d0c02b8002ec591b1ec
7
+ data.tar.gz: 43d2c803fb7ea64bd34dd8f7e3ab37f4672bc789c05f6ea1843600cf9d9a8c462af7797234ed944e763da4da3a4e7e0ef22286329e56a9c1917554eec7ba0359
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2024 Matt Brictson
3
+ Copyright (c) 2025 Matt Brictson
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mattbrictson/configurable_from_env/ci.yml)](https://github.com/mattbrictson/configurable_from_env/actions/workflows/ci.yml)
6
6
  [![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/mattbrictson/configurable_from_env)](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 works by extending Active Support's [`config_accessor`](https://api.rubyonrails.org/classes/ActiveSupport/Configurable/ClassMethods.html#method-i-config_accessor) with a new `:from_env` option.
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,3 +1,3 @@
1
1
  module ConfigurableFromEnv
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  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 ActiveSupport::Configurable
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.1.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: 2024-11-19 00:00:00.000000000 Z
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.5.23
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: []