env_helpers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0f5a4bc370ed19fec6324cc8775cb381490a836434c1fc276d55778b1a75eee
4
+ data.tar.gz: f0e6817ab137cbef597f360f027533bc4eb84ad003ea06f545798c0ab02edf67
5
+ SHA512:
6
+ metadata.gz: 885efcd4587d2f70ac8e7bb9dee853abf13515d2f6921d2e74fa281f5a6e21e319743464aa65eb550257e3aeb35666a1b5f316bdb537dc91348f31e55d6a5abe
7
+ data.tar.gz: 65ba21b3d2150d240156fc929e2a2bf245373c7a603ee3f45465b84eb743348633f51e9b328bb559037371432aeb4ee12c321106059ec993e6bf0de926fe4a7a
@@ -0,0 +1,162 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+
7
+ spec_shared: &spec_shared
8
+ working_directory: ~/env_helpers
9
+ steps:
10
+ - checkout
11
+
12
+ - run:
13
+ name: install dependencies
14
+ command: |
15
+ bundle check --path vendor/bundle || bundle install --jobs=4 --retry=3 --path vendor/bundle
16
+
17
+ - run:
18
+ name: Make test-results
19
+ command: |
20
+ mkdir /tmp/test-results
21
+
22
+ - run:
23
+ name: Run basic specs
24
+ command: |
25
+ bundle exec rspec --format progress \
26
+ --format RspecJunitFormatter \
27
+ --out /tmp/test-results/rspec-basic.xml \
28
+ --format progress \
29
+ spec --tag ~ext:true --tag ~override:true
30
+ - run:
31
+ name: Run extended ENV specs
32
+ command: |
33
+ bundle exec rspec --format progress \
34
+ --format RspecJunitFormatter \
35
+ --out /tmp/test-results/rspec-ext.xml \
36
+ --format progress \
37
+ spec --tag ext:true
38
+
39
+ - run:
40
+ name: Run boolean override specs
41
+ command: |
42
+ bundle exec rspec --format progress \
43
+ --format RspecJunitFormatter \
44
+ --out /tmp/test-results/rspec-override.xml \
45
+ --format progress \
46
+ spec --tag override:true
47
+
48
+ # collect reports
49
+ - store_test_results:
50
+ path: /tmp/test-results
51
+ - store_artifacts:
52
+ path: /tmp/test-results
53
+ destination: test-results
54
+
55
+ jobs:
56
+ deps:
57
+ docker:
58
+ - image: circleci/ruby:2.5
59
+ working_directory: ~/env_helpers
60
+
61
+ steps:
62
+ - checkout
63
+
64
+ # Download and cache dependencies
65
+ - restore_cache:
66
+ keys:
67
+ - v1-dependencies-{{ checksum "env_helpers.gemspec" }}
68
+
69
+ - run:
70
+ name: install dependencies
71
+ command: |
72
+ bundle check --path vendor/bundle || bundle install --jobs=4 --retry=3 --path vendor/bundle
73
+
74
+ - save_cache:
75
+ paths:
76
+ - vendor/bundle
77
+ key: v1-dependencies-{{ checksum "env_helpers.gemspec" }}
78
+
79
+ - run:
80
+ name: dependencies security audit
81
+ command: |
82
+ bundle exec bundle-audit check --update
83
+
84
+ - run:
85
+ name: Rubocop
86
+ command: bundle exec rubocop
87
+
88
+ ruby-2.5:
89
+ <<: *spec_shared
90
+ docker:
91
+ - image: circleci/ruby:2.5
92
+
93
+ ruby-2.4:
94
+ <<: *spec_shared
95
+ docker:
96
+ - image: circleci/ruby:2.4
97
+
98
+ ruby-2.3:
99
+ <<: *spec_shared
100
+ docker:
101
+ - image: circleci/ruby:2.3
102
+
103
+ ruby-2.2:
104
+ <<: *spec_shared
105
+ docker:
106
+ - image: circleci/ruby:2.2
107
+
108
+ generate_docs:
109
+ docker:
110
+ - image: circleci/ruby:2.5
111
+ working_directory: ~/env_helpers
112
+
113
+ steps:
114
+ - checkout
115
+
116
+ - restore_cache:
117
+ keys:
118
+ - v1-dependencies-{{ checksum "env_helpers.gemspec" }}
119
+
120
+ - run:
121
+ name: Setup bundler path
122
+ command: |
123
+ bundle check --path vendor/bundle
124
+
125
+ - run:
126
+ name: Generate Yard docs
127
+ command: |
128
+ bundle exec yard --output-dir /tmp/workspace/docs/yard
129
+
130
+ - run:
131
+ name: Num Docs
132
+ command: |
133
+ ls -al /tmp/workspace/docs/yard | wc -l
134
+
135
+ - persist_to_workspace:
136
+ root: /tmp/workspace/docs
137
+ paths:
138
+ - yard
139
+
140
+ workflows:
141
+ version: 2
142
+ build:
143
+ jobs:
144
+ - deps
145
+ - ruby-2.5:
146
+ requires:
147
+ - deps
148
+ - ruby-2.4:
149
+ requires:
150
+ - deps
151
+ - ruby-2.3:
152
+ requires:
153
+ - deps
154
+ - ruby-2.2:
155
+ requires:
156
+ - deps
157
+ - generate_docs:
158
+ requires:
159
+ - ruby-2.5
160
+ - ruby-2.4
161
+ - ruby-2.3
162
+ - ruby-2.2
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5.1
3
+
4
+ Metrics:
5
+ Enabled: false
6
+
7
+ Metrics/LineLength:
8
+ Max: 99
9
+ Exclude:
10
+ - "spec/**/*"
11
+ - "*.gemspec"
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at les.fletcher@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in env_helpers.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ env_helpers (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ bundler-audit (0.6.0)
11
+ bundler (~> 1.2)
12
+ thor (~> 0.18)
13
+ climate_control (0.2.0)
14
+ coderay (1.1.2)
15
+ diff-lcs (1.3)
16
+ jaro_winkler (1.5.1)
17
+ method_source (0.9.1)
18
+ parallel (1.12.1)
19
+ parser (2.5.3.0)
20
+ ast (~> 2.4.0)
21
+ powerpack (0.1.2)
22
+ pry (0.12.0)
23
+ coderay (~> 1.1.0)
24
+ method_source (~> 0.9.0)
25
+ rainbow (3.0.0)
26
+ rake (10.5.0)
27
+ rspec (3.8.0)
28
+ rspec-core (~> 3.8.0)
29
+ rspec-expectations (~> 3.8.0)
30
+ rspec-mocks (~> 3.8.0)
31
+ rspec-core (3.8.0)
32
+ rspec-support (~> 3.8.0)
33
+ rspec-expectations (3.8.2)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.8.0)
36
+ rspec-mocks (3.8.0)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.8.0)
39
+ rspec-support (3.8.0)
40
+ rspec_junit_formatter (0.4.1)
41
+ rspec-core (>= 2, < 4, != 2.12.0)
42
+ rubocop (0.60.0)
43
+ jaro_winkler (~> 1.5.1)
44
+ parallel (~> 1.10)
45
+ parser (>= 2.5, != 2.5.1.1)
46
+ powerpack (~> 0.1)
47
+ rainbow (>= 2.2.2, < 4.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (~> 1.4.0)
50
+ ruby-progressbar (1.10.0)
51
+ thor (0.20.0)
52
+ unicode-display_width (1.4.0)
53
+ yard (0.9.16)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ bundler (~> 1.16)
60
+ bundler-audit
61
+ climate_control (~> 0.2)
62
+ env_helpers!
63
+ pry
64
+ rake (~> 10.0)
65
+ rspec (~> 3.0)
66
+ rspec_junit_formatter (~> 0.2)
67
+ rubocop (~> 0.60.0)
68
+ yard
69
+
70
+ BUNDLED WITH
71
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Les Fletcher
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,132 @@
1
+ # EnvHelpers
2
+
3
+ A set of helpers to improve ENV functionality
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'env_helpers'
11
+ ```
12
+
13
+ If you'd like to add the functionality directly to `ENV` you can add:
14
+
15
+ ```ruby
16
+ gem 'env_helpers', require: 'env_helpers/ext/env'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install env_helpers
26
+
27
+ ## Usage
28
+
29
+ You can find the on [rubdoc.info](https://www.rubydoc.info/github/Latermedia/env_helpers/master).
30
+
31
+ ### Methods
32
+
33
+ `EnvHelpers` supplies the following helper methods:
34
+
35
+ - `equal?`
36
+ - `num`
37
+ - `int`
38
+ - `array`
39
+ - `json`
40
+ - `bool`
41
+ - `true?`
42
+ - `false?`
43
+
44
+ ```ruby
45
+ require `env_helpers`
46
+
47
+ EnvHelpers.int('TEST_INT')
48
+ ```
49
+
50
+ If you used the `ENV` extension, you can call any of these methods directly on `ENV`.
51
+
52
+ ```ruby
53
+ require `env_helpers/ext/env`
54
+
55
+ ENV.int('TEST_INT')
56
+ ```
57
+
58
+ ### JSON
59
+
60
+ By default `EnvHelpers` swallows `JSON::ParserError` on invalid JSON when using `.json`. To change this behavior set `rescue_json` to be `false`.
61
+
62
+ ```ruby
63
+ EnvHelpers.rescue_json = false
64
+ ```
65
+
66
+ ### Boolean Values
67
+
68
+ `EnvHelpers` does a case insensitive check for boolean values.
69
+
70
+ **True**
71
+
72
+ - `true`
73
+ - `t`
74
+ - `1`
75
+
76
+ **False**
77
+
78
+ - `false`
79
+ - `f`
80
+ - `0`
81
+
82
+ To modify `EnvHelpers` checks for `true` and `false`, a simple monkey patch will take care of things:
83
+
84
+ ```ruby
85
+ module EnvHelpers
86
+ class << self
87
+ def true_value?(val)
88
+ val == 'false'
89
+ end
90
+
91
+ def false_value?(val)
92
+ val == 'true'
93
+ end
94
+ end
95
+ end
96
+ ```
97
+
98
+ The methods `true_value?` and `false_value?` are meant to be modified in this manner to customize functionality.
99
+
100
+ ## Development
101
+
102
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `./run_specs` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
103
+
104
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
105
+
106
+ ## Tests
107
+
108
+ Since parts of the functionality is optional and modifies other classes and objects, the specs aren't meant to be run all at the same time. This is to isolate the environment the tests are executing in.
109
+
110
+ To run the tests:
111
+
112
+ ```bash
113
+ ./run_specs
114
+ ```
115
+
116
+ Tests are currently run against the following Ruby version:
117
+ - 2.5
118
+ - 2.4
119
+ - 2.3
120
+ - 2.2
121
+
122
+ ## Contributing
123
+
124
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Latermedia/env_helpers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
125
+
126
+ ## License
127
+
128
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
129
+
130
+ ## Code of Conduct
131
+
132
+ Everyone interacting in the EnvHelpers project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Latermedia/env_helpers/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'env_helpers'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'env_helpers/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'env_helpers'
9
+ spec.version = EnvHelpers::VERSION
10
+ spec.authors = ['Les Fletcher']
11
+ spec.email = ['development@later.com']
12
+
13
+ spec.summary = 'A set of helpers to improve ENV functionality'
14
+ spec.description = 'Helpers for dealing with environment variables, with the option of adding the functionality to ENV. '
15
+ spec.homepage = 'https://github.com/Latermedia/env_helpers'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'bundler-audit'
27
+ spec.add_development_dependency 'climate_control', '~> 0.2'
28
+ spec.add_development_dependency 'pry'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'rspec_junit_formatter', '~> 0.2'
32
+ spec.add_development_dependency 'rubocop', '~> 0.60.0'
33
+ spec.add_development_dependency 'yard'
34
+ end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ require 'env_helpers/version'
6
+ require 'env_helpers/utils'
7
+
8
+ # Helper methods to improve ENV functionality
9
+ module EnvHelpers
10
+ class << self
11
+ attr_accessor :rescue_json
12
+
13
+ # Compare the value of an ENV variable to a given string.
14
+ #
15
+ # @param var_name [String] ENV variable name
16
+ # @param value [String] comparison string
17
+ # @return [Boolean] `true` if the value of the ENV variable is the same as value
18
+ def equal?(var_name, value)
19
+ return false unless ENV.key?(var_name)
20
+
21
+ ENV[var_name] == value
22
+ end
23
+
24
+ # Get a number value (using to_f) from an ENV variable or return a default.
25
+ #
26
+ # @param var_name [String] ENV variable name
27
+ # @param default [Object] retured when ENV[var_name] is blank or doesn't exist
28
+ # @return [Float] float representing the ENV value
29
+ def num(var_name, default = nil)
30
+ return default unless ENV.key?(var_name)
31
+
32
+ n = ENV[var_name]
33
+
34
+ return default if n == ''
35
+
36
+ n.to_f
37
+ end
38
+
39
+ # Get an integer value (using to_i) from an ENV variable or return a default.
40
+ #
41
+ # @param var_name [String] ENV variable name
42
+ # @param default [Object] retured when ENV[var_name] is blank or doesn't exist
43
+ # @return [Integer] integer representing the ENV value
44
+ def int(var_name, default = nil)
45
+ return default unless ENV.key?(var_name)
46
+
47
+ n = ENV[var_name]
48
+
49
+ return default if n == ''
50
+
51
+ n.to_i
52
+ end
53
+
54
+ # Get an array value from an ENV variable
55
+ #
56
+ # @param var_name [String] ENV variable name
57
+ # @param sep [String] string to split ENV value on
58
+ # @return [Array] array of strings representing the ENV value.
59
+ # Defaults to [] if value is blank.
60
+ def array(var_name, sep = ',')
61
+ return [] unless ENV.key?(var_name)
62
+
63
+ ENV[var_name].split(sep)
64
+ end
65
+
66
+ # Get a JSON value from an ENV variable
67
+ #
68
+ # @param var_name [String] ENV variable name
69
+ # @raise [JSON::ParserError] if value is invalid JSON
70
+ # @return [Object] array of strings representing the ENV value.
71
+ def json(var_name, default = nil)
72
+ return default unless ENV.key?(var_name)
73
+
74
+ JSON.parse(ENV[var_name])
75
+ rescue JSON::ParserError => ex
76
+ raise ex unless rescue_json?
77
+
78
+ default
79
+ end
80
+
81
+ # @private
82
+ def rescue_json?
83
+ return @rescue_json if defined? @rescue_json
84
+
85
+ true
86
+ end
87
+
88
+ # Get an boolean value from an ENV variable or return a default.
89
+ # Will return the default if there is a non-boolean like value for the ENV variable.
90
+ #
91
+ # @param var_name [String] ENV variable name
92
+ # @param default [Object] retured when ENV[var_name] is blank
93
+ # or doesn't represent a numeric value
94
+ # @return [Boolean] boolean representing the ENV value
95
+ def bool(var_name, default = nil)
96
+ return default unless ENV.key?(var_name)
97
+
98
+ return true if true_value?(ENV[var_name])
99
+ return false if false_value?(ENV[var_name])
100
+
101
+ default
102
+ end
103
+
104
+ # Check if an ENV variable represents `true`. ('true', 't', or '1').
105
+ # Returns false if environment variable isn't defined.
106
+ #
107
+ # @param var_name [String] ENV variable name
108
+ # @return [Boolean]
109
+ def true?(var_name)
110
+ return false unless ENV.key?(var_name)
111
+
112
+ true_value?(ENV[var_name])
113
+ end
114
+
115
+ # Check if an ENV variable represents `false`. ('false', 'f', or '0').
116
+ # Returns false if environment variable isn't defined.
117
+ #
118
+ # @param var_name [String] ENV variable name
119
+ # @return [Boolean]
120
+ def false?(var_name)
121
+ return false unless ENV.key?(var_name)
122
+
123
+ false_value?(ENV[var_name])
124
+ end
125
+
126
+ # Check if a value is true
127
+ # Meant to be overriden / monkey patched to customize functionality
128
+ #
129
+ # @param val [String] value to check if true
130
+ # @return [Boolean]
131
+ def true_value?(val)
132
+ EnvHelpers::Utils.true_value?(val)
133
+ end
134
+
135
+ # Check if a value is false
136
+ # Meant to be overriden / monkey patched to customize functionality
137
+ #
138
+ # @param val [String] value to check if false
139
+ # @return [Boolean]
140
+ def false_value?(val)
141
+ EnvHelpers::Utils.false_value?(val)
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'env_helpers'
4
+
5
+ class << ENV
6
+ # Compare the value of an ENV variable to a given string.
7
+ #
8
+ # @param var_name [String] ENV variable name
9
+ # @param value [String] comparison string
10
+ # @return [Boolean] `true` if the value of the ENV variable is the same as value
11
+ def equal?(var_name, value)
12
+ EnvHelpers.equal?(var_name, value)
13
+ end
14
+
15
+ # Get a number value from an ENV variable or return a default.
16
+ #
17
+ # @param var_name [String] ENV variable name
18
+ # @param default [Object] retured when ENV[var_name] is blank or doesn't exist
19
+ # @return [Float] float representing the ENV value
20
+ def num(var_name, default = nil)
21
+ EnvHelpers.num(var_name, default)
22
+ end
23
+
24
+ # Get an integer value from an ENV variable or return a default.
25
+ #
26
+ # @param var_name [String] ENV variable name
27
+ # @param default [Object] retured when ENV[var_name] is blank or doesn't exist
28
+ # @return [Integer] integer representing the ENV value
29
+ def int(var_name, default = nil)
30
+ EnvHelpers.int(var_name, default)
31
+ end
32
+
33
+ # Get an array value from an ENV variable
34
+ #
35
+ # @param var_name [String] ENV variable name
36
+ # @param sep [String] string to split ENV value on
37
+ # @return [Array] array of strings representing the ENV value.
38
+ # Defaults to [] if value is blank.
39
+ def array(var_name, sep = ',')
40
+ EnvHelpers.array(var_name, sep)
41
+ end
42
+
43
+ # Get a JSON value from an ENV variable
44
+ #
45
+ # @param var_name [String] ENV variable name
46
+ # @raise [JSON::ParserError] if value is invalid JSON
47
+ # @return [Object] array of strings representing the ENV value.
48
+ def json(var_name, default = nil)
49
+ EnvHelpers.json(var_name, default)
50
+ end
51
+
52
+ # Get an boolean value from an ENV variable or return a default.
53
+ # Will return the default if there is a non-boolean like value for the ENV variable.
54
+ #
55
+ # @param var_name [String] ENV variable name
56
+ # @param default [Object] retured when ENV[var_name] is blank
57
+ # or doesn't represent a numeric value
58
+ # @return [Boolean] boolean representing the ENV value
59
+ def bool(var_name, default = nil)
60
+ EnvHelpers.bool(var_name, default)
61
+ end
62
+
63
+ # Check if an ENV variable represents `true`. ('true', 't', or '1').
64
+ # Returns false if environment variable isn't defined.
65
+ #
66
+ # @param var_name [String] ENV variable name
67
+ # @return [Boolean]
68
+ def true?(var_name)
69
+ EnvHelpers.true?(var_name)
70
+ end
71
+
72
+ # Check if an ENV variable represents `false`. ('false', 'f', or '0').
73
+ # Returns false if environment variable isn't defined.
74
+ #
75
+ # @param var_name [String] ENV variable name
76
+ # @return [Boolean]
77
+ def false?(var_name)
78
+ EnvHelpers.false?(var_name)
79
+ end
80
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnvHelpers
4
+ # Utility methods
5
+ class Utils
6
+ class << self
7
+ # Check if a value represents a boolean
8
+ # @param val [Object] object to check if a boolean value
9
+ # @return [Boolean]
10
+ def boolean_value?(val)
11
+ true_value?(val) || false_value?(val)
12
+ end
13
+
14
+ alias bool_value? boolean_value?
15
+
16
+ # Check if value represents `true`. ('true', 't', or '1')
17
+ # @param val [Object] object to check if a true value
18
+ # @return [Boolean]
19
+ def true_value?(val)
20
+ val = val.to_s.downcase
21
+
22
+ return true if val == 'true'
23
+ return true if val == 't'
24
+ return true if val == '1'
25
+
26
+ false
27
+ end
28
+
29
+ # Check if value represents `false`. ('false', 'f', or '0')
30
+ # @param val [Object] object to check if a false value
31
+ # @return [Boolean]
32
+ def false_value?(val)
33
+ val = val.to_s.downcase
34
+
35
+ return true if val == 'false'
36
+ return true if val == 'f'
37
+ return true if val == '0'
38
+
39
+ false
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnvHelpers
4
+ VERSION = '0.1.0'
5
+ end
data/run_specs ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+
3
+ bundle exec rspec spec --tag ~ext:true --tag ~override:true
4
+ bundle exec rspec spec --tag ext:true
5
+ bundle exec rspec spec --tag override:true
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: env_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Les Fletcher
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler-audit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: climate_control
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec_junit_formatter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.60.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.60.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: yard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: 'Helpers for dealing with environment variables, with the option of adding
140
+ the functionality to ENV. '
141
+ email:
142
+ - development@later.com
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".circleci/config.yml"
148
+ - ".gitignore"
149
+ - ".rspec"
150
+ - ".rubocop.yml"
151
+ - CODE_OF_CONDUCT.md
152
+ - Gemfile
153
+ - Gemfile.lock
154
+ - LICENSE.txt
155
+ - README.md
156
+ - Rakefile
157
+ - bin/console
158
+ - bin/setup
159
+ - env_helpers.gemspec
160
+ - lib/env_helpers.rb
161
+ - lib/env_helpers/ext/env.rb
162
+ - lib/env_helpers/utils.rb
163
+ - lib/env_helpers/version.rb
164
+ - run_specs
165
+ homepage: https://github.com/Latermedia/env_helpers
166
+ licenses:
167
+ - MIT
168
+ metadata: {}
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubyforge_project:
185
+ rubygems_version: 2.7.6
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: A set of helpers to improve ENV functionality
189
+ test_files: []