env_value 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 20e781fcfcbecfa408f57df1d763d0d680a60704a62fe010f20dff84772881a8
4
+ data.tar.gz: 31bc9fb16a1750bfb1f6adffc8b35c3d07c86e1cceb9f7a2575017735dab585a
5
+ SHA512:
6
+ metadata.gz: 9c3c6e167bed44c7d947261643176be54d6311f1a02189844c87be80b1414dee226c6e6f13f9a209a1d4f43edeb8342e35fcbd0f30b9a3f4be76bc10081d4b14
7
+ data.tar.gz: e47eb5eed1bcb1bc013383f90f5f945a46c43abf7e44c1b1aa41e400859f4d54974e0ccf05808e29d83a6a9bee170f8d43f219b415767b4de0b564df14496819
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /Gemfile.lock
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Changelog.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in env_value.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/License ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2021 Tyler Rick
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
4
+ associated documentation files (the "Software"), to deal in the Software without restriction,
5
+ including without limitation the rights to use, copy, modify, merge, publish, distribute,
6
+ sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7
+ furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial
10
+ portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/Readme.md ADDED
@@ -0,0 +1,69 @@
1
+ # EnvValue
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'env_value'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ ## Usage
16
+
17
+ ### `ENV_value`
18
+
19
+ Get a boolean or string value from the specified ENV variable. '1', 'true', '0', 'false' will
20
+ be converted to boolean values; all values others as string values (returned as-is, since ENV
21
+ only contains string values).
22
+
23
+ ```ruby
24
+ ENV_value('option', missing: :raise) # raises 'Environment variable "option" was missing.'
25
+
26
+ ENV['option'] = 'sanely'
27
+ ENV_value('option') # => 'sanely'
28
+ ENV_value('option', convert: :to_sym) # => :sanely
29
+ ```
30
+
31
+ ```ruby
32
+ ENV['max_attempts'] = '3'
33
+ ENV_value('max_attempts') # => '3'
34
+ ENV_value('max_attempts', convert: :to_i) # => 3
35
+ ```
36
+
37
+ See ENV_boolean for more available options.
38
+
39
+ ### `ENV_boolean`
40
+
41
+ Get a boolean value (true or false) from the specified ENV variable.
42
+
43
+ By default, '1', 'true' will be treated as true values; '0', 'false' as false values;
44
+ and all others as nil.
45
+
46
+ You can configure that with the `true:`, `false:`, and `default:` (or
47
+ `missing:`/`invalid:`) options.
48
+
49
+ ```ruby
50
+ ENV['enabled'] = '1'
51
+ ENV['skip_it'] = 'skip'
52
+
53
+ ENV_boolean('enabled') # => true
54
+ ENV_boolean('skip_it') # => nil
55
+ ENV_boolean('unknown') # => nil
56
+ ENV_boolean('unknown', default: true) # => true
57
+ ENV_boolean('unknown', missing: true) # => true
58
+
59
+ ENV_boolean('skip_it', default: false) # => false
60
+ ENV_boolean('skip_it', false: ['skip']) # => false
61
+ ENV_boolean('skip_it', true: ['skip']) # => true
62
+ ENV_boolean('skip_it', missing: true, invalid: :string) # => 'skip'
63
+ ```
64
+
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/TylerRick/env_value.
69
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "env_value"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/env_value.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/env_value/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "env_value"
5
+ spec.version = EnvValue.version
6
+ spec.authors = ["Tyler Rick"]
7
+ spec.email = ["tyler@tylerrick.com"]
8
+ spec.license = "MIT"
9
+
10
+ spec.summary = %q{Easily get a boolean, string, or integer value from an ENV variable.}
11
+ spec.description = spec.summary
12
+ spec.homepage = "https://github.com/TylerRick/#{spec.name}"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = "#{spec.metadata["source_code_uri"]}/blob/master/Changelog.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+ end
data/lib/env_value.rb ADDED
@@ -0,0 +1,105 @@
1
+ require "env_value/version"
2
+
3
+ module Kernel
4
+ # Get a boolean or string value from the specified ENV variable. '1', 'true', '0', 'false' will
5
+ # be converted to boolean values; all values others as string values (returned as-is, since ENV
6
+ # only contains string values).
7
+ #
8
+ # See ENV_boolean for more available options.
9
+ #
10
+ def ENV_value(key, convert: :string, **options)
11
+ ENV_boolean(key, invalid: convert, **options)
12
+ end
13
+
14
+ # Get a boolean value (true or false) from the specified ENV variable.
15
+ #
16
+ # By default, '1', 'true' will be treated as true values; '0', 'false' as false values;
17
+ # and all others as nil.
18
+ # You can configure that with the `true:`, `false:`, and `default:` (or
19
+ # `missing:`/`invalid:`) options.
20
+ #
21
+ # @example
22
+ # ENV['enabled'] = '1'
23
+ # ENV['skip_it'] = 'skip'
24
+ #
25
+ # ENV_boolean('enabled') # => true
26
+ # ENV_boolean('skip_it') # => nil
27
+ # ENV_boolean('unknown') # => nil
28
+ # ENV_boolean('unknown', default: true) # => true
29
+ #
30
+ # ENV_boolean('skip_it', default: false) # => false
31
+ # ENV_boolean('skip_it', false: ['skip']) # => false
32
+ # ENV_boolean('skip_it', true: ['skip']) # => true
33
+ # ENV_boolean('skip_it', invalid: :string) # => 'skip'
34
+ #
35
+ # @example
36
+ # You can rewrite this:
37
+ # Capybara.javascript_driver = ENV['javascript_driver'].to_sym if ENV['javascript_driver']) || :firefox
38
+ # into this slightly more readable and shorter:
39
+ # Capybara.javascript_driver = ENV_value('javascript_driver', convert: :to_sym, default: :firefox)
40
+ #
41
+ def ENV_boolean(
42
+ key,
43
+ true: [],
44
+ false: [],
45
+ # By default the true: and false: options get appended to the internal defaults, but you can
46
+ # override internal defaults, too, if you want.
47
+ true_values: ['1', 'true' ] + Array(binding.local_variable_get('true')),
48
+ false_values: ['0', 'false'] + Array(binding.local_variable_get('false')),
49
+ default: nil,
50
+ missing: default, # (missing_default)
51
+ invalid: default # (invalid_default)
52
+ )
53
+ unless ENV.key?(key)
54
+ if [:raise, :fail, :abort].include? missing
55
+ message = %(Environment variable "#{key}" was missing.)
56
+ send(missing, message)
57
+ else
58
+ return missing
59
+ end
60
+ end
61
+
62
+ allowed_values = true_values + false_values
63
+ value = ENV[key]
64
+ case value
65
+ when *true_values; true
66
+ when *false_values; false
67
+ else
68
+ if [:string, :to_s].include?(invalid)
69
+ value
70
+ elsif [:symbol, :to_sym].include?(invalid)
71
+ value.to_sym
72
+ elsif [:integer, :to_i].include?(invalid)
73
+ value.to_i
74
+ elsif [:raise, :fail, :abort].include?(invalid)
75
+ message = %(Environment variable "#{key}" (#{value.inspect}) was not one of the allowed values (#{allowed_values}).)
76
+ send(invalid, message)
77
+ else
78
+ invalid
79
+ end
80
+ end
81
+ end
82
+ alias_method :ENV_boolean_value, :ENV_boolean
83
+
84
+ # TODO: API idea, so you can use more natural [] index access syntax:
85
+ # ENV.boolean['a']
86
+
87
+ # Previous version:
88
+ #
89
+ # (Probably don't need these if we have ENV_boolean.)
90
+ #
91
+ # def ENV_value_truthy?(key, default: nil)
92
+ # return default unless ENV.key?(key)
93
+ #
94
+ # value = ENV[key]
95
+ # value.in? ['1', 'true']
96
+ # end
97
+ #
98
+ # def ENV_value_falsey?(key, default: nil)
99
+ # return default unless ENV.key?(key)
100
+ #
101
+ # value = ENV[key]
102
+ # value.in? ['0', 'false']
103
+ # end
104
+
105
+ end
@@ -0,0 +1,5 @@
1
+ module EnvValue
2
+ def self.version
3
+ "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: env_value
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Rick
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Easily get a boolean, string, or integer value from an ENV variable.
14
+ email:
15
+ - tyler@tylerrick.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - Changelog.md
24
+ - Gemfile
25
+ - License
26
+ - Rakefile
27
+ - Readme.md
28
+ - bin/console
29
+ - bin/setup
30
+ - env_value.gemspec
31
+ - lib/env_value.rb
32
+ - lib/env_value/version.rb
33
+ homepage: https://github.com/TylerRick/env_value
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://github.com/TylerRick/env_value
38
+ source_code_uri: https://github.com/TylerRick/env_value
39
+ changelog_uri: https://github.com/TylerRick/env_value/blob/master/Changelog.md
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.3.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.1.2
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Easily get a boolean, string, or integer value from an ENV variable.
59
+ test_files: []