env_vars 0.4.0 → 1.1.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
- SHA1:
3
- metadata.gz: edb62ff44a63e4c934b684ce185c368f5c7e033d
4
- data.tar.gz: 3ea149485a9c4e3a6de43346e86089344b997eea
2
+ SHA256:
3
+ metadata.gz: e32d97f4e650d16b8c82ec8dd85bc19112788797262c00425e83a1d8594678f3
4
+ data.tar.gz: ebec12747a93d37dbeadf026ec1140dbcb1122eca498db9db7f5612221868cc4
5
5
  SHA512:
6
- metadata.gz: de7f9410fce327186d147d664630417ddc153338b726ccf81afafd8d3a970762ffccf910ce99e5fc400e00641006c6617194401841781bca1dad748c72adfdce
7
- data.tar.gz: b566be6e2e9f4a2479d2def53c9ff68c10dd7e7b5fc478802d896e6c73ae5cf0a02d7e1359e8942bc6d5ebd9582b9180ee1a24d66f8b91406dfcacf3237eb4d7
6
+ metadata.gz: c6822500b56e48d42f508d128e505b288133724996a926dab44ecb6e43b8373ee6ea177faa6b30a21e48a3e2e2f9eb980d950140f1beca0447d6475aa1ce48ea
7
+ data.tar.gz: b4240b420772985e2c3403a7a5abd047b0b8bc8dfc0500340ef3a1e187dea6e181df0bbb4cea52652603c3bc04046b2cde98e85dda48d888067c8305070f1370
@@ -0,0 +1,15 @@
1
+ ---
2
+ inherit_gem:
3
+ rubocop-fnando: .rubocop.yml
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.7
7
+
8
+ Metrics/ClassLength:
9
+ Enabled: false
10
+
11
+ Metrics/MethodLength:
12
+ Enabled: false
13
+
14
+ Metrics/ParameterLists:
15
+ Enabled: false
@@ -4,5 +4,15 @@ sudo: false
4
4
  notifications:
5
5
  email: false
6
6
  rvm:
7
- - 2.2.2
8
- before_install: gem install bundler
7
+ - 2.7.1
8
+ before_script:
9
+ - "curl -L
10
+ https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64
11
+ > ./cc-test-reporter"
12
+ - chmod +x ./cc-test-reporter
13
+ - "./cc-test-reporter before-build"
14
+ after_script:
15
+ - "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
16
+ env:
17
+ global:
18
+ secure: dXC3R4mvHkeXyhKskgf5/3/LUtBYKpDcTpNGbuOpnoXAaB87fHgjhDtwSazYERE5i0FYeMHWBWKUosQZkCOCydXCuZQ7Q1AnqjnouiCoaj675uvs/KamRAoknQVzxbSk5nxSiqth2fMDu6DTm8ZcbAXkCJj3JwNe0x/zZrx6ZfqEj928NdL2qxTbGLciFyovjhjwIx5ZNKJ+LlvwZ5uGdaGJvN1K7xuATcrRFyyPRzd0oPGFJb7pBwbkK0VxmdTMKcCnXmovoIIZPsXCMW1zvfE30McrCOyHrE/4OQWFI0FG7QJAzM3ekioWQj94JRkno3NPflTWBqn+tSQmjgKkSuooJQgbJ0eWAD0uU2JBebjrXx4/AvWlUiB4mG4u7OGUbq8TbY2EHhqb5JtTuAo3qy/D5xUtZaSISD70sQqfE6ZdjVlBaC9hDSATYXNyiUYGihslEUySmCqH94ZBkm1c5CKDPm1bnBs8eyzI2NqO6EA/yf8/U97fGKI1EmjNQm888MIgbgSeKlWfc6EonWMxlaZ8SG/J4cFx43nlUPPmP/q488wKz8qaCeVpvuX29mN+8LQmQT0zQlVNZIHfI64tIcdE8kvK/vu+9iq7UlPim2VPNf8B9aHX67NUli0FVo2acvCgivIcZ75YJFSCmy8IhNX/XaJD5AdQgVmwP0A/uk0=
data/Gemfile CHANGED
@@ -1,2 +1,4 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
2
4
  gemspec
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
- # Env::Vars
1
+ ![env_vars: Access environment variables. Also includes presence validation, type coercion and default values.](https://raw.githubusercontent.com/fnando/env_vars/master/env_vars.png)
2
2
 
3
- [![build status](https://travis-ci.org/fnando/env_vars.svg)](https://travis-ci.org/fnando/env_vars)
4
-
5
- Access environment variables. Also includes presence validation, type coercion and default values.
3
+ <p align="center">
4
+ <a href="https://travis-ci.org/fnando/env_vars"><img src="https://travis-ci.org/fnando/env_vars.svg" alt="Travis-CI"></a>
5
+ <a href="https://codeclimate.com/github/fnando/env_vars"><img src="https://codeclimate.com/github/fnando/env_vars/badges/gpa.svg" alt="Code Climate"></a>
6
+ <a href="https://codeclimate.com/github/fnando/env_vars/coverage"><img src="https://codeclimate.com/github/fnando/env_vars/badges/coverage.svg" alt="Test Coverage"></a>
7
+ <a href="https://rubygems.org/gems/env_vars"><img src="https://img.shields.io/gem/v/env_vars.svg" alt="Gem"></a>
8
+ <a href="https://rubygems.org/gems/env_vars"><img src="https://img.shields.io/gem/dt/env_vars.svg" alt="Gem"></a>
9
+ </p>
6
10
 
7
11
  ## Installation
8
12
 
@@ -35,12 +39,25 @@ Config.timeout
35
39
  Config.force_ssl?
36
40
  ```
37
41
 
38
- If you're going to use `env_vars` as your main configuration object, you can also set arbitrary properties, like the following:
42
+ You can specify the description for both `mandatory` and `optional` methods;
43
+ this will be used in exceptions.
44
+
45
+ ```ruby
46
+ Config = Env::Vars.new do
47
+ mandatory :missing_var, string, description: "this is important"
48
+ end
49
+
50
+ #=> Env::Vars::MissingEnvironmentVariable: MISSING_VAR (this is important) is not defined
51
+ ```
52
+
53
+ If you're going to use `env_vars` as your main configuration object, you can
54
+ also set arbitrary properties, like the following:
39
55
 
40
56
  ```ruby
41
57
  Config = Env::Vars.new do
42
58
  optional :redis_url, string, "redis://127.0.0.1"
43
- property :redis, -> { Redis.new }
59
+ property :redis, -> { Redis.new } # pass an object that responds to #call
60
+ property(:now) { Time.now } # or pass a block.
44
61
  end
45
62
 
46
63
  Config.redis.set("key", "value")
@@ -48,7 +65,50 @@ Config.redis.get("key")
48
65
  #=> "value"
49
66
  ```
50
67
 
51
- If you're using [dotenv](https://rubygems.org/gems/dotenv), you can simply require `env_vars/dotenv`. This will load environment variables from `.env.local.%{environment}`, `.env.local`, `.env.%{environment}` and `.env` files, respectively. You _must_ add `dotenv` to your `Gemfile`.
68
+ Values are cached by default. If you want to dynamically generate new values,
69
+ set `cache: false`.
70
+
71
+ ```ruby
72
+ Config = Env::Vars.new do
73
+ property(:uuid, cache: false) { SecureRandom.uuid }
74
+ end
75
+ ```
76
+
77
+ You may want to start a debug session without raising exceptions for missing
78
+ variables. In this case, just pass `raise_exception: false` instead to log error
79
+ messages to `$stderr`. This is especially great with Rails' credentials command
80
+ (`rails credentials:edit`) when already defined the configuration.
81
+
82
+ ```ruby
83
+ Config = Env::Vars.new(raise_exception: false) do
84
+ mandatory :database_url, string, description: "the leader database"
85
+ end
86
+
87
+ #=> [ENV_VARS] DATABASE_URL (the leader database) is not defined
88
+ ```
89
+
90
+ ### Types
91
+
92
+ You can coerce values to the following types:
93
+
94
+ - `string`: Is the default. E.g. `optional :name, string`.
95
+ - `int`: E.g. `optional :timeout, int`.
96
+ - `float`: E.g. `optional :wait, float`.
97
+ - `bigdecimal`: E.g. `optional :fee, bigdecimal`.
98
+ - `bool`: E.g. `optional :force_ssl, bool`. Any of `yes`, `true` or `1` is
99
+ considered as `true`. Any other value will be coerced to `false`.
100
+ - `symbol`: E.g. `optional :app_name, symbol`.
101
+ - `array`: E.g. `optional :chars, array` or `optional :numbers, array(int)`. The
102
+ environment variable must be something like `a,b,c`.
103
+ - `json`: E.g. `mandatory :keyring, json`. The environment variable must be
104
+ parseable by `JSON.parse(content)`.
105
+
106
+ ### Dotenv integration
107
+
108
+ If you're using [dotenv](https://rubygems.org/gems/dotenv), you can simply
109
+ require `env_vars/dotenv`. This will load environment variables from
110
+ `.env.local.%{environment}`, `.env.local`, `.env.%{environment}` and `.env`
111
+ files, respectively. You _must_ add `dotenv` to your `Gemfile`.
52
112
 
53
113
  ```ruby
54
114
  require "env_vars/dotenv"
@@ -56,7 +116,9 @@ require "env_vars/dotenv"
56
116
 
57
117
  ### Configuring Rails
58
118
 
59
- If you want to use `env_vars` even on your Rails configuration files like `database.yml` and `secrets.yml`, you must load it from `config/boot.rb`, right after setting up Bundler.
119
+ If you want to use `env_vars` even on your Rails configuration files like
120
+ `database.yml` and `secrets.yml`, you must load it from `config/boot.rb`, right
121
+ after setting up Bundler.
60
122
 
61
123
  ```ruby
62
124
  ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __FILE__)
@@ -71,15 +133,29 @@ require File.expand_path("../config", __FILE__)
71
133
 
72
134
  ## Development
73
135
 
74
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
136
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
137
+ `rake test` to run the tests. You can also run `bin/console` for an interactive
138
+ prompt that will allow you to experiment.
75
139
 
76
- 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).
140
+ To install this gem onto your local machine, run `bundle exec rake install`. To
141
+ release a new version, update the version number in `version.rb`, and then run
142
+ `bundle exec rake release`, which will create a git tag for the version, push
143
+ git commits and tags, and push the `.gem` file to
144
+ [rubygems.org](https://rubygems.org).
77
145
 
78
146
  ## Contributing
79
147
 
80
- Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/env_vars. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
81
-
148
+ Bug reports and pull requests are welcome on GitHub at
149
+ https://github.com/fnando/env_vars. This project is intended to be a safe,
150
+ welcoming space for collaboration, and contributors are expected to adhere to
151
+ the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
82
152
 
83
153
  ## License
84
154
 
85
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
155
+ The gem is available as open source under the terms of the
156
+ [MIT License](http://opensource.org/licenses/MIT).
157
+
158
+ ## Icon
159
+
160
+ Icon made by [eucalyp](https://www.flaticon.com/authors/eucalyp) from
161
+ [Flaticon](https://www.flaticon.com/) is licensed by Creative Commons BY 3.0.
data/Rakefile CHANGED
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rake/testtask"
5
+ require "rubocop/rake_task"
3
6
 
4
7
  Rake::TestTask.new(:test) do |t|
5
8
  t.libs << "test"
@@ -7,4 +10,6 @@ Rake::TestTask.new(:test) do |t|
7
10
  t.test_files = FileList["test/**/*_test.rb"]
8
11
  end
9
12
 
10
- task :default => :test
13
+ RuboCop::RakeTask.new
14
+
15
+ task default: %i[test rubocop]
@@ -1,4 +1,6 @@
1
- require File.expand_path("../lib/env_vars", __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./lib/env_vars"
2
4
 
3
5
  Gem::Specification.new do |spec|
4
6
  spec.name = "env_vars"
@@ -6,17 +8,22 @@ Gem::Specification.new do |spec|
6
8
  spec.authors = ["Nando Vieira"]
7
9
  spec.email = ["fnando.vieira@gmail.com"]
8
10
 
9
- spec.summary = "Access environment variables. Also includes presence validation, type coercion and default values."
11
+ spec.summary = "Access environment variables. Also includes presence " \
12
+ "validation, type coercion and default values."
10
13
  spec.description = spec.summary
11
14
  spec.homepage = "https://github.com/fnando/env_vars"
12
15
  spec.license = "MIT"
13
16
 
14
- spec.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.files = `git ls-files -z`
18
+ .split("\x0")
19
+ .reject {|f| f.match(%r{^(test|spec|features)/}) }
15
20
  spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
16
21
  spec.require_paths = ["lib"]
17
22
 
18
- spec.add_development_dependency "bundler", "~> 1.10"
19
- spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "bundler"
20
24
  spec.add_development_dependency "minitest"
21
25
  spec.add_development_dependency "minitest-utils"
26
+ spec.add_development_dependency "pry-meta"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "simplecov"
22
29
  end
Binary file
@@ -0,0 +1,20 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" viewBox="0 0 300 300">
2
+ <g fill="none" fill-rule="evenodd">
3
+ <rect width="155" height="63" x="73" y="92" fill="#9013FE"/>
4
+ <rect width="154.839" height="9.714" x="72.581" y="126.286" fill="#000"/>
5
+ <rect width="62.903" height="9.714" x="164.516" y="145.714" fill="#000"/>
6
+ <rect width="62.903" height="9.714" x="72.581" y="145.714" fill="#000"/>
7
+ <rect width="62.903" height="9.714" x="164.516" y="106.857" fill="#000"/>
8
+ <rect width="62.903" height="9.714" x="72.581" y="106.857" fill="#000"/>
9
+ <rect width="154.839" height="9.714" x="72.581" y="87.429" fill="#000"/>
10
+ <path fill="#000" fill-rule="nonzero" d="M283.483871,0 L14.516129,0 C6.51290323,0 0,6.53771429 0,14.5714286 L0,238 L217.741935,238 L217.741935,228.285714 L9.67741935,228.285714 L9.67741935,48.5714286 L290.322581,48.5714286 L290.322581,228.285714 L217.741935,228.285714 L217.741935,238 L300,238 L300,14.5714286 C300,6.53771429 291.487097,0 283.483871,0 Z M9.67741935,38.8571429 L9.67741935,14.5714286 C9.67741935,11.8951429 11.85,9.71428571 14.516129,9.71428571 L285.483871,9.71428571 C288.15,9.71428571 290.322581,11.8951429 290.322581,14.5714286 L290.322581,38.8571429 L9.67741935,38.8571429 Z"/>
11
+ <path fill="#9013FE" d="M163.908393,300 L137.091607,300 L134.078214,287.931964 C130.211429,286.789286 126.479643,285.236786 122.92625,283.303393 L112.251607,289.70625 L93.29375,270.748393 L99.6966071,260.07375 C97.7632143,256.520357 96.2107143,252.78375 95.0680357,248.921786 L83,245.908393 L83,219.091607 L95.0680357,216.078214 C96.2107143,212.211429 97.7632143,208.479643 99.6966071,204.92625 L93.29375,194.251607 L112.251607,175.29375 L122.92625,181.696607 C126.479643,179.763214 130.21625,178.210714 134.078214,177.068036 L137.091607,165 L163.908393,165 L166.921786,177.068036 C170.788571,178.210714 174.520357,179.763214 178.07375,181.696607 L188.748393,175.29375 L207.70625,194.251607 L201.303393,204.92625 C203.236786,208.479643 204.789286,212.21625 205.931964,216.078214 L218,219.091607 L218,245.908393 L205.931964,248.921786 C204.789286,252.788571 203.236786,256.520357 201.303393,260.07375 L207.70625,270.748393 L188.748393,289.70625 L178.07375,283.303393 C174.520357,285.236786 170.78375,286.789286 166.921786,287.931964 L163.908393,300 Z"/>
12
+ <path fill="#000" fill-rule="nonzero" d="M150,213.714286 C139.325806,213.714286 130.645161,222.428 130.645161,233.142857 C130.645161,243.857714 139.325806,252.571429 150,252.571429 C160.674194,252.571429 169.354839,243.857714 169.354839,233.142857 C169.354839,222.428 160.674194,213.714286 150,213.714286 Z M150,242.857143 C144.662903,242.857143 140.322581,238.500286 140.322581,233.142857 C140.322581,227.785429 144.662903,223.428571 150,223.428571 C155.337097,223.428571 159.677419,227.785429 159.677419,233.142857 C159.677419,238.500286 155.337097,242.857143 150,242.857143 Z"/>
13
+ <path fill="#000" d="M150 194.285714C143.574194 194.285714 137.201613 195.903143 131.579032 198.958286L136.190323 207.502C140.404839 205.214286 145.175806 204 150 204 166.01129 204 179.032258 217.070571 179.032258 233.142857 179.032258 237.985429 177.822581 242.774571 175.543548 247.005143L184.054839 251.634C187.098387 245.99 188.709677 239.593143 188.709677 233.142857 188.709677 211.718 171.343548 194.285714 150 194.285714zM150 262.285714C133.98871 262.285714 120.967742 249.215143 120.967742 233.142857 120.967742 228.300286 122.177419 223.511143 124.456452 219.280571L115.945161 214.651714C112.901613 220.295714 111.290323 226.692571 111.290323 233.142857 111.290323 254.567714 128.656452 272 150 272 156.425806 272 162.798387 270.382571 168.420968 267.327429L163.809677 258.783714C159.595161 261.071429 154.824194 262.285714 150 262.285714z"/>
14
+ <rect width="9.677" height="9.714" x="19.355" y="19.429" fill="#000"/>
15
+ <rect width="9.677" height="9.714" x="38.71" y="19.429" fill="#000"/>
16
+ <rect width="9.677" height="9.714" x="58.065" y="19.429" fill="#000"/>
17
+ <polygon fill="#000" points="45 187.289 45 60 68.99 60 68.99 73.227 58.504 73.694 58.504 173.439 68.99 174.062 68.99 187.289"/>
18
+ <polygon fill="#000" points="255.99 187.412 232 187.412 232 174.277 242.486 173.658 242.486 74.599 232 74.136 232 61 255.99 61"/>
19
+ </g>
20
+ </svg>
@@ -1,33 +1,45 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Env
2
4
  class Vars
3
- VERSION = "0.4.0"
4
- BOOL_TRUE = ["yes", "true", "1", true]
5
- BOOL_FALSE = ["no", "false"]
5
+ VERSION = "1.1.0"
6
+ BOOL_TRUE = ["yes", "true", "1", true].freeze
7
+ BOOL_FALSE = %w[no false].freeze
6
8
 
7
9
  MissingEnvironmentVariable = Class.new(StandardError)
10
+ MissingCallable = Class.new(StandardError)
8
11
 
9
- def initialize(&block)
12
+ def initialize(env: ENV, raise_exception: true, stderr: $stderr, &block)
13
+ @env = env
14
+ @raise_exception = raise_exception
15
+ @stderr = stderr
16
+ @__cache__ = {}
10
17
  instance_eval(&block)
11
18
  end
12
19
 
13
- def set(name, type, default = nil, required: false, aliases: [])
20
+ def to_s
21
+ "#<Env::Vars>"
22
+ end
23
+ alias inspect to_s
24
+
25
+ def set(
26
+ name,
27
+ type,
28
+ default = nil,
29
+ required: false,
30
+ aliases: [],
31
+ description: nil
32
+ )
14
33
  name = name.to_s
15
34
  env_var = name.upcase
16
35
  name = "#{name}?" if type == bool
17
36
 
18
- validate!(env_var, required)
37
+ validate!(env_var, required, description)
19
38
 
20
39
  define_singleton_method(name) do
21
- value = ENV[env_var] || default
22
-
23
- case type
24
- when bool
25
- BOOL_TRUE.include?(value)
26
- when int
27
- Integer(value) if !BOOL_FALSE.include?(value) && value
28
- else
29
- value
30
- end
40
+ return default unless @env.key?(env_var)
41
+
42
+ coerce(type, @env[env_var])
31
43
  end
32
44
 
33
45
  aliases.each do |alias_name|
@@ -35,21 +47,49 @@ module Env
35
47
  end
36
48
  end
37
49
 
38
- def validate!(env_var, required)
39
- raise MissingEnvironmentVariable, "#{env_var} is not defined" if required && !ENV.key?(env_var)
50
+ def validate!(env_var, required, description)
51
+ return unless required
52
+ return if @env.key?(env_var)
53
+
54
+ message = env_var.to_s
55
+ message << " (#{description})" if description
56
+ message << " is not defined."
57
+
58
+ raise MissingEnvironmentVariable, message if @raise_exception
59
+
60
+ message = "[ENV_VARS] #{message}"
61
+ message = "\e[31m#{message}\e[0m" if @stderr.tty?
62
+ @stderr << message << "\n"
40
63
  end
41
64
 
42
- def mandatory(name, type, aliases: [])
43
- set(name, type, required: true, aliases: aliases)
65
+ def mandatory(name, type, aliases: [], description: nil)
66
+ set(
67
+ name,
68
+ type,
69
+ required: true,
70
+ aliases: aliases,
71
+ description: description
72
+ )
44
73
  end
45
74
 
46
- def optional(name, type, default = nil, aliases: [])
47
- set(name, type, default, aliases: aliases)
75
+ def optional(name, type, default = nil, aliases: [], description: nil)
76
+ set(name, type, default, aliases: aliases, description: description)
48
77
  end
49
78
 
50
- def property(name, func)
51
- value = func.call
52
- define_singleton_method(name) { value }
79
+ def property(name, func = nil, cache: true, description: nil, &block) # rubocop:disable Lint/UnusedMethodArgument
80
+ callable = (func || block)
81
+
82
+ unless callable
83
+ raise MissingCallable, "arg[1] must respond to #call or pass a block"
84
+ end
85
+
86
+ if cache
87
+ define_singleton_method(name) do
88
+ @__cache__[name.to_sym] ||= callable.call
89
+ end
90
+ else
91
+ define_singleton_method(name) { callable.call }
92
+ end
53
93
  end
54
94
 
55
95
  def int
@@ -63,5 +103,66 @@ module Env
63
103
  def bool
64
104
  :bool
65
105
  end
106
+
107
+ def symbol
108
+ :symbol
109
+ end
110
+
111
+ def float
112
+ :float
113
+ end
114
+
115
+ def bigdecimal
116
+ require "bigdecimal"
117
+ :bigdecimal
118
+ end
119
+
120
+ def array(type = string)
121
+ [:array, type]
122
+ end
123
+
124
+ def json
125
+ :json
126
+ end
127
+
128
+ private def coerce_to_string(value)
129
+ value
130
+ end
131
+
132
+ private def coerce_to_bool(value)
133
+ BOOL_TRUE.include?(value)
134
+ end
135
+
136
+ private def coerce_to_int(value)
137
+ Integer(value) if !BOOL_FALSE.include?(value) && value
138
+ end
139
+
140
+ private def coerce_to_float(value)
141
+ Float(value) if value
142
+ end
143
+
144
+ private def coerce_to_bigdecimal(value)
145
+ BigDecimal(value) if value
146
+ end
147
+
148
+ private def coerce_to_symbol(value)
149
+ value&.to_sym
150
+ end
151
+
152
+ private def coerce_to_array(value, type)
153
+ value&.split(/, */)&.map {|v| coerce(type, v) }
154
+ end
155
+
156
+ private def coerce_to_json(value)
157
+ value && JSON.parse(value)
158
+ end
159
+
160
+ private def coerce(type, value)
161
+ main_type, sub_type = type
162
+ args = [value]
163
+ args << sub_type if sub_type
164
+
165
+ send("coerce_to_#{main_type}", *args)
166
+ end
66
167
  end
67
168
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "env_vars"
2
4
  require "dotenv"
3
5
 
metadata CHANGED
@@ -1,45 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env_vars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-12 00:00:00.000000000 Z
11
+ date: 2020-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.10'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest
42
+ name: minitest-utils
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,35 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: minitest-utils
56
+ name: pry-meta
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: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
57
85
  requirement: !ruby/object:Gem::Requirement
58
86
  requirements:
59
87
  - - ">="
@@ -75,6 +103,7 @@ extensions: []
75
103
  extra_rdoc_files: []
76
104
  files:
77
105
  - ".gitignore"
106
+ - ".rubocop.yml"
78
107
  - ".travis.yml"
79
108
  - CODE_OF_CONDUCT.md
80
109
  - Gemfile
@@ -82,13 +111,15 @@ files:
82
111
  - README.md
83
112
  - Rakefile
84
113
  - env_vars.gemspec
114
+ - env_vars.png
115
+ - env_vars.svg
85
116
  - lib/env_vars.rb
86
117
  - lib/env_vars/dotenv.rb
87
118
  homepage: https://github.com/fnando/env_vars
88
119
  licenses:
89
120
  - MIT
90
121
  metadata: {}
91
- post_install_message:
122
+ post_install_message:
92
123
  rdoc_options: []
93
124
  require_paths:
94
125
  - lib
@@ -103,9 +134,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
134
  - !ruby/object:Gem::Version
104
135
  version: '0'
105
136
  requirements: []
106
- rubyforge_project:
107
- rubygems_version: 2.5.1
108
- signing_key:
137
+ rubygems_version: 3.1.2
138
+ signing_key:
109
139
  specification_version: 4
110
140
  summary: Access environment variables. Also includes presence validation, type coercion
111
141
  and default values.