├── .coditsu └── ci.yml ├── .diffend.yml ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .rspec ├── .ruby-gemset ├── .ruby-version ├── CHANGELOG.md ├── Gemfile ├── Gemfile.lock ├── MIT-LICENSE ├── README.md ├── Rakefile ├── certs └── mensfeld.pem ├── envlogic.gemspec ├── lib ├── envlogic.rb └── envlogic │ ├── env.rb │ └── version.rb └── spec ├── lib ├── envlogic │ ├── env_spec.rb │ └── version_spec.rb └── envlogic_spec.rb ├── spec_helper.rb └── support └── class_builder.rb /.coditsu/ci.yml: -------------------------------------------------------------------------------- 1 | repository_id: '8e9c10ac-336a-485a-9f9b-69ea15075c2b' 2 | api_key: <%= ENV['CODITSU_API_KEY'] %> 3 | api_secret: <%= ENV['CODITSU_API_SECRET'] %> 4 | -------------------------------------------------------------------------------- /.diffend.yml: -------------------------------------------------------------------------------- 1 | project_id: '2aea122d-f4f1-4a9d-a1d1-34f9bbd769be' 2 | shareable_id: '4cb899b8-bf3c-4976-8948-70f367bbdc7c' 3 | shareable_key: '7479aeaf-1ecc-403d-9325-2c4e6abbb68b' 4 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | concurrency: ci-${{ github.ref }} 4 | 5 | on: 6 | pull_request: 7 | push: 8 | schedule: 9 | - cron: '0 1 * * *' 10 | 11 | jobs: 12 | specs: 13 | runs-on: ubuntu-latest 14 | needs: diffend 15 | strategy: 16 | fail-fast: false 17 | matrix: 18 | ruby: 19 | - '3.1' 20 | - '3.0' 21 | - '2.7' 22 | include: 23 | - ruby: '3.1' 24 | coverage: 'true' 25 | steps: 26 | - uses: actions/checkout@v3 27 | - name: Install package dependencies 28 | run: "[ -e $APT_DEPS ] || sudo apt-get install -y --no-install-recommends $APT_DEPS" 29 | - name: Set up Ruby 30 | uses: ruby/setup-ruby@v1 31 | with: 32 | ruby-version: ${{matrix.ruby}} 33 | - name: Install latest bundler 34 | run: | 35 | gem install bundler --no-document 36 | bundle config set without 'tools benchmarks docs' 37 | - name: Bundle install 38 | run: bundle install --jobs 4 --retry 3 39 | - name: Run all tests 40 | env: 41 | GITHUB_COVERAGE: ${{matrix.coverage}} 42 | run: bundle exec rspec 43 | 44 | diffend: 45 | runs-on: ubuntu-latest 46 | strategy: 47 | fail-fast: false 48 | steps: 49 | - uses: actions/checkout@v3 50 | with: 51 | fetch-depth: 0 52 | - name: Set up Ruby 53 | uses: ruby/setup-ruby@v1 54 | with: 55 | ruby-version: 3.1 56 | - name: Install latest bundler 57 | run: gem install bundler --no-document 58 | - name: Install Diffend plugin 59 | run: bundle plugin install diffend 60 | - name: Bundle Secure 61 | run: bundle secure 62 | 63 | coditsu: 64 | runs-on: ubuntu-latest 65 | strategy: 66 | fail-fast: false 67 | steps: 68 | - uses: actions/checkout@v3 69 | with: 70 | fetch-depth: 0 71 | - name: Run Coditsu 72 | run: \curl -sSL https://api.coditsu.io/run/ci | bash 73 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | .idea 3 | 4 | ### SublimeText template 5 | # cache files for sublime text 6 | *.tmlanguage.cache 7 | *.tmPreferences.cache 8 | *.stTheme.cache 9 | 10 | # workspace files are user-specific 11 | *.sublime-workspace 12 | 13 | # project files should be checked into the repository, unless a significant 14 | # proportion of contributors will probably not be using SublimeText 15 | # *.sublime-project 16 | 17 | # sftp configuration file 18 | sftp-config.json 19 | 20 | 21 | ### Ruby template 22 | *.gem 23 | *.rbc 24 | /.config 25 | /coverage/ 26 | /InstalledFiles 27 | /pkg/ 28 | /spec/reports/ 29 | /test/tmp/ 30 | /test/version_tmp/ 31 | /tmp/ 32 | 33 | ## Specific to RubyMotion: 34 | .dat* 35 | .repl_history 36 | build/ 37 | 38 | ## Documentation cache and generated files: 39 | /.yardoc/ 40 | /_yardoc/ 41 | /doc/ 42 | /rdoc/ 43 | 44 | ## Environment normalisation: 45 | /.bundle/ 46 | /vendor/bundle 47 | /app/lib/bundler/man/ 48 | 49 | # for a library or gem, you might want to ignore these files since the code is 50 | # intended to run in multiple environments; otherwise, check them in: 51 | # Gemfile.lock 52 | # .ruby-version 53 | # .ruby-gemset 54 | 55 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: 56 | .rvmrc 57 | .coditsu/local.yml 58 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /.ruby-gemset: -------------------------------------------------------------------------------- 1 | env_logic 2 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 3.1.3 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Envlogic gem changelog 2 | 3 | ## 1.1.5 (2022-04-17) 4 | - Support for Ruby 3.1 5 | - Drop support for ruby 2.6 6 | - Add `rubygems_mfa_required` 7 | 8 | ## 1.1.4 (2021-12-05) 9 | - drop JRuby support because the latest version is based on Ruby 2.5 10 | - Source code metadata url added to the gemspec 11 | 12 | ## 1.1.3 (2021-04-21) 13 | - Restore MIT license 14 | - Remove Ruby 2.5 support and update minimum Ruby requirement to 2.6 15 | 16 | ## 1.1.2 (2020-04-24) 17 | - restore JRuby support 18 | - add TruffleRuby support 19 | - Change license to LGPL-3.0 20 | 21 | ## 1.1.1 (2020-04-22) 22 | - drop jruby support 23 | - drop Ruby 2.2 support 24 | - drop Ruby 2.3 support 25 | - drop Ruby 2.4 support 26 | - Ruby 2.6.5 support 27 | - Ruby 2.7.1 support 28 | - Replace travis with GH actions 29 | - Update docs to show per instance support 30 | - Support signing the releases 31 | 32 | ## 1.1.0 33 | - Ruby 2.4.2 support 34 | - Ruby 2.5.0 support 35 | - ActiveSupport dependency dropped in favor of dry-inflector 36 | 37 | ## 1.0.4 38 | - Switched with quality metrics to Coditsu (https://coditsu.io/) 39 | - Gem dump 40 | - Quality improvements 41 | - Gems cleanup 42 | - Ruby dump to 2.4.1 43 | - Better specs (less internal, more integration) 44 | - License added to the gemspec file 45 | 46 | ## 1.0.3 47 | - Gem dump x3 48 | - ~~Ruby version defaults to 2.3.3~~ 49 | - Ruby version defaults to 2.4.0 50 | - Dropped support for Ruby 2.1.* 51 | - added .rspec for default spec helper require 52 | 53 | ## 1.0.2 54 | - Dev tools update 55 | - Gem update 56 | - Rspec specs update 57 | 58 | ## 1.0.0 59 | - Initial gem version with all the magic 60 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source 'https://rubygems.org' 4 | 5 | plugin 'diffend' 6 | 7 | gemspec 8 | 9 | group :development, :test do 10 | gem 'rake' 11 | end 12 | 13 | group :test do 14 | gem 'rspec' 15 | gem 'simplecov' 16 | end 17 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | envlogic (1.1.5) 5 | dry-inflector (~> 0.1) 6 | 7 | GEM 8 | remote: https://rubygems.org/ 9 | specs: 10 | diff-lcs (1.5.0) 11 | docile (1.4.0) 12 | dry-inflector (0.3.0) 13 | rake (13.0.6) 14 | rspec (3.12.0) 15 | rspec-core (~> 3.12.0) 16 | rspec-expectations (~> 3.12.0) 17 | rspec-mocks (~> 3.12.0) 18 | rspec-core (3.12.0) 19 | rspec-support (~> 3.12.0) 20 | rspec-expectations (3.12.1) 21 | diff-lcs (>= 1.2.0, < 2.0) 22 | rspec-support (~> 3.12.0) 23 | rspec-mocks (3.12.1) 24 | diff-lcs (>= 1.2.0, < 2.0) 25 | rspec-support (~> 3.12.0) 26 | rspec-support (3.12.0) 27 | simplecov (0.22.0) 28 | docile (~> 1.1) 29 | simplecov-html (~> 0.11) 30 | simplecov_json_formatter (~> 0.1) 31 | simplecov-html (0.12.3) 32 | simplecov_json_formatter (0.1.4) 33 | 34 | PLATFORMS 35 | arm64-darwin-21 36 | x86_64-darwin 37 | x86_64-linux 38 | 39 | DEPENDENCIES 40 | envlogic! 41 | rake 42 | rspec 43 | simplecov 44 | 45 | BUNDLED WITH 46 | 2.3.26 47 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any person obtaining 2 | a copy of this software and associated documentation files (the 3 | "Software"), to deal in the Software without restriction, including 4 | without limitation the rights to use, copy, modify, merge, publish, 5 | distribute, sublicense, and/or sell copies of the Software, and to 6 | permit persons to whom the Software is furnished to do so, subject to 7 | the following conditions: 8 | 9 | The above copyright notice and this permission notice shall be 10 | included in all copies or substantial portions of the Software. 11 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 13 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 14 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 15 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 16 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 17 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 18 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Envlogic [Unmaintained] 2 | 3 | Note: This library is no longer in use in the Karafka ecosystem. It was developed for Karafka versions prior to 2.0. 4 | 5 | [![Build Status](https://github.com/karafka/envlogic/workflows/ci/badge.svg)](https://github.com/karafka/envlogic/actions?query=workflow%3Aci) 6 | [![Gem Version](https://badge.fury.io/rb/envlogic.svg)](http://badge.fury.io/rb/envlogic) 7 | [![Join the chat at https://slack.karafka.io](https://raw.githubusercontent.com/karafka/misc/master/slack.svg)](https://slack.karafka.io) 8 | 9 | Envlogic is a library used to manage environments for your Ruby application in a similar to Rails.env way. 10 | 11 | ## Installation 12 | 13 | Add the gem to your Gemfile 14 | ```ruby 15 | gem 'envlogic' 16 | ``` 17 | 18 | ## Usage 19 | 20 | ### On a class/module level 21 | 22 | Extend your class or module in which you want to use this library with **Envlogic** module. 23 | 24 | ```ruby 25 | module ExampleModule 26 | extend Envlogic 27 | # code of this module 28 | end 29 | ``` 30 | 31 | Once you extend your class/module with it, you will have two additional methods (with two aliases): 32 | 33 | - *.env* (.environment) - obtain current env and work with it 34 | - *.env=* (.environment=) - set your own environment 35 | 36 | ```ruby 37 | ExampleModule.env = 'development' 38 | ExampleModule.env.development? # => true 39 | ExampleModule.env.production? # => false 40 | ``` 41 | 42 | ### On a per instance basis 43 | 44 | Include the **Envlogic** module in the class for which instances you want to use it. 45 | 46 | ```ruby 47 | class ExampleClass 48 | include Envlogic 49 | # code of this class 50 | end 51 | ``` 52 | 53 | Once you include it in your class, you will have two additional methods (with two aliases): 54 | 55 | - *.env* (.environment) - obtain current env and work with it 56 | - *.env=* (.environment=) - set your own environment 57 | 58 | ```ruby 59 | instance = ExampleClass.new 60 | instance.env = 'development' 61 | instance.env.development? # => true 62 | instance.env.production? # => false 63 | ``` 64 | 65 | ### ENV variables key names and default fallbacks 66 | 67 | #### Application root directory env key name 68 | 69 | By default, the gem is looking for ENV variable that is based on your application root directory. 70 | 71 | For example, if your application lies in */home/deploy/my_app* it will look for **MY_APP_ENV** variable. 72 | 73 | #### Module/class name based env key name 74 | 75 | If there's no env value under the app directory name key, it will fallback to the module/class based env variable name (including the whole namespace chain): 76 | 77 | ```ruby 78 | module Basic 79 | module Karafka 80 | extend Envlogic 81 | # code of Karafka module 82 | end 83 | end 84 | ``` 85 | 86 | ```ruby 87 | ENV['FACEBOOK_API_ENV'] = nil 88 | ENV['BASIC_KARAFKA_ENV'] = 'development' 89 | 90 | Basic::Karafka.env.production? # => false 91 | Basic::Karafka.env.development? # => true 92 | ``` 93 | 94 | #### Default fallbacks 95 | 96 | If there's no other way to determine the environment, Envlogic will fallback to ENV['RACK_ENV'] and if it fails, it will just assume that we're in 'development' mode. 97 | 98 | You can also assign the environment directly in Ruby: 99 | 100 | ```ruby 101 | module Basic 102 | module Karafka 103 | extend Envlogic 104 | # code of Karafka module 105 | end 106 | end 107 | 108 | Basic::Karafka.env = :development 109 | Basic::Karafka.env.production? # => false 110 | Basic::Karafka.env.development? # => true 111 | ``` 112 | 113 | ## References 114 | 115 | * [Karafka framework](https://github.com/karafka/karafka) 116 | * [Envlogic Actions CI](https://github.com/karafka/envlogic/actions?query=workflow%3Aci) 117 | * [Envlogic Coditsu](https://app.coditsu.io/karafka/repositories/envlogic) 118 | 119 | ## Note on contributions 120 | 121 | First, thank you for considering contributing to the Karafka ecosystem! It's people like you that make the open source community such a great community! 122 | 123 | Each pull request must pass all the RSpec specs, integration tests and meet our quality requirements. 124 | 125 | Fork it, update and wait for the Github Actions results. 126 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'bundler' 4 | require 'bundler/gem_tasks' 5 | require 'rake' 6 | require 'rspec/core/rake_task' 7 | 8 | RSpec::Core::RakeTask.new(:spec) 9 | 10 | task default: :spec 11 | -------------------------------------------------------------------------------- /certs/mensfeld.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIEODCCAqCgAwIBAgIBATANBgkqhkiG9w0BAQsFADAjMSEwHwYDVQQDDBhtYWNp 3 | ZWovREM9bWVuc2ZlbGQvREM9cGwwHhcNMjEwODExMTQxNTEzWhcNMjIwODExMTQx 4 | NTEzWjAjMSEwHwYDVQQDDBhtYWNpZWovREM9bWVuc2ZlbGQvREM9cGwwggGiMA0G 5 | CSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQDV2jKH4Ti87GM6nyT6D+ESzTI0MZDj 6 | ak2/TEwnxvijMJyCCPKT/qIkbW4/f0VHM4rhPr1nW73sb5SZBVFCLlJcOSKOBdUY 7 | TMY+SIXN2EtUaZuhAOe8LxtxjHTgRHvHcqUQMBENXTISNzCo32LnUxweu66ia4Pd 8 | 1mNRhzOqNv9YiBZvtBf7IMQ+sYdOCjboq2dlsWmJiwiDpY9lQBTnWORnT3mQxU5x 9 | vPSwnLB854cHdCS8fQo4DjeJBRZHhEbcE5sqhEMB3RZA3EtFVEXOxlNxVTS3tncI 10 | qyNXiWDaxcipaens4ObSY1C2HTV7OWb7OMqSCIybeYTSfkaSdqmcl4S6zxXkjH1J 11 | tnjayAVzD+QVXGijsPLE2PFnJAh9iDET2cMsjabO1f6l1OQNyAtqpcyQcgfnyW0z 12 | g7tGxTYD+6wJHffM9d9txOUw6djkF6bDxyqB8lo4Z3IObCx18AZjI9XPS9QG7w6q 13 | LCWuMG2lkCcRgASqaVk9fEf9yMc2xxz5o3kCAwEAAaN3MHUwCQYDVR0TBAIwADAL 14 | BgNVHQ8EBAMCBLAwHQYDVR0OBBYEFBqUFCKCOe5IuueUVqOB991jyCLLMB0GA1Ud 15 | EQQWMBSBEm1hY2llakBtZW5zZmVsZC5wbDAdBgNVHRIEFjAUgRJtYWNpZWpAbWVu 16 | c2ZlbGQucGwwDQYJKoZIhvcNAQELBQADggGBADD0/UuTTFgW+CGk2U0RDw2RBOca 17 | W2LTF/G7AOzuzD0Tc4voc7WXyrgKwJREv8rgBimLnNlgmFJLmtUCh2U/MgxvcilH 18 | yshYcbseNvjkrtYnLRlWZR4SSB6Zei5AlyGVQLPkvdsBpNegcG6w075YEwzX/38a 19 | 8V9B/Yri2OGELBz8ykl7BsXUgNoUPA/4pHF6YRLz+VirOaUIQ4JfY7xGj6fSOWWz 20 | /rQ/d77r6o1mfJYM/3BRVg73a3b7DmRnE5qjwmSaSQ7u802pJnLesmArch0xGCT/ 21 | fMmRli1Qb+6qOTl9mzD6UDMAyFR4t6MStLm0mIEqM0nBO5nUdUWbC7l9qXEf8XBE 22 | 2DP28p3EqSuS+lKbAWKcqv7t0iRhhmaod+Yn9mcrLN1sa3q3KSQ9BCyxezCD4Mk2 23 | R2P11bWoCtr70BsccVrN8jEhzwXngMyI2gVt750Y+dbTu1KgRqZKp/ECe7ZzPzXj 24 | pIy9vHxTANKYVyI4qj8OrFdEM5BQNu8oQpL0iQ== 25 | -----END CERTIFICATE----- 26 | -------------------------------------------------------------------------------- /envlogic.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | lib = File.expand_path('lib', __dir__) 4 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 5 | 6 | require 'envlogic/version' 7 | 8 | Gem::Specification.new do |spec| 9 | spec.name = 'envlogic' 10 | spec.platform = Gem::Platform::RUBY 11 | spec.version = Envlogic::VERSION 12 | spec.authors = ['pavlo_vavruk', 'Maciej Mensfeld'] 13 | spec.email = %w[pavlo.vavruk@gmail.com maciej@mensfeld.pl] 14 | spec.summary = 'Library which allows to set and get environments values' 15 | spec.description = 'Library used to manage environments for your Ruby application' 16 | spec.homepage = 'https://karafka.io' 17 | spec.license = 'MIT' 18 | 19 | spec.add_dependency 'dry-inflector', '~> 0.1' 20 | 21 | spec.required_ruby_version = '>= 2.7' 22 | 23 | if $PROGRAM_NAME.end_with?('gem') 24 | spec.signing_key = File.expand_path('~/.ssh/gem-private_key.pem') 25 | end 26 | 27 | spec.cert_chain = %w[certs/mensfeld.pem] 28 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) } 29 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 30 | spec.require_paths = %w[lib] 31 | 32 | spec.metadata = { 33 | 'source_code_uri' => 'https://github.com/karafka/envlogic', 34 | 'rubygems_mfa_required' => 'true' 35 | } 36 | end 37 | -------------------------------------------------------------------------------- /lib/envlogic.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | %w[ 4 | dry/inflector 5 | envlogic/version 6 | envlogic/env 7 | ].each { |lib| require lib } 8 | 9 | # Main module that encapsulates logic that should be used to extend any class/module 10 | # @note This module should be extended/included into the class/module in which we want to have 11 | # env support 12 | # 13 | # @example Use it in RandomClass class 14 | # class RandomClass 15 | # extend Envlogic 16 | # end 17 | # 18 | # RandomClass.env #=> Envlogic::Env instance 19 | # RandomClass.env.production? #=> false 20 | # RandomClass.env.development? #=> true 21 | # 22 | # @example Use it for instances of objects 23 | # class RandomClass 24 | # include Envlogic 25 | # end 26 | # 27 | # instance = RandomClass.new 28 | # instance.env #=> Envlogic::Env instance 29 | # instance.env.production? #=> false 30 | # instance.env.development? #=> true 31 | module Envlogic 32 | # @return [Envlogic::Env] envlogic env instance that allows us to check environment 33 | # @example Invoke env in TestClass 34 | # TestClass.env #=> Envlogic::Env instance 35 | def env 36 | @env ||= Envlogic::Env.new(self) 37 | end 38 | 39 | # @param environment [String, Symbol] new environment that we want to set 40 | # @return [Envlogic::Env] envlogic env instance 41 | # @example Assign new environment to MyApp 42 | # MyApp.env = :production 43 | def env=(environment) 44 | env.update(environment.to_s) 45 | end 46 | 47 | # We alias this for backward compatibility with some code that uses full names 48 | alias environment env 49 | alias environment= env= 50 | end 51 | -------------------------------------------------------------------------------- /lib/envlogic/env.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | # Main module 4 | module Envlogic 5 | # Env module to get and set environment 6 | class Env < String 7 | # What environment key should be used by default 8 | FALLBACK_ENV_KEY = 'RACK_ENV' 9 | # What default environment should be assumed when there's nothing else 10 | FALLBACK_ENV = 'development' 11 | # Postfix for ENV keys 12 | ENV_KEY_POSTFIX = '_ENV' 13 | # String inflecting engine 14 | INFLECTOR = Dry::Inflector.new 15 | 16 | private_constant :FALLBACK_ENV_KEY, :FALLBACK_ENV, :ENV_KEY_POSTFIX, :INFLECTOR 17 | 18 | # It's just a string replace alias for the envlogic compatibility 19 | alias update replace 20 | 21 | # @param klass [Class, Module] class/module for which we want to build a Envlogic::Env object 22 | # @return [Envlogic::Env] envlogic env object] 23 | # @note Will load appropriate environment automatically 24 | # @example 25 | # Envlogic::Env.new(User) 26 | def initialize(klass) 27 | super('') 28 | 29 | env = ENV[to_env_key(app_dir_name)] 30 | env ||= ENV[to_env_key(klass.to_s)] 31 | env ||= ENV[FALLBACK_ENV_KEY] 32 | 33 | update(env || FALLBACK_ENV) 34 | end 35 | 36 | # @param method_name [String] method name 37 | # @param include_private [Boolean] should we include private methods as well 38 | # @return [Boolean] true if we respond to a given missing method, otherwise false 39 | def respond_to_missing?(method_name, include_private = false) 40 | (method_name[-1] == '?') || super 41 | end 42 | 43 | # Reacts to missing methods, from which some might be the env checks. 44 | # If the method ends with '?' we assume, that it is an env check 45 | # @param method_name [String] method name for missing or env name with question mark 46 | # @param arguments [Array] any arguments that we pass to the method 47 | def method_missing(method_name, *arguments) 48 | method_name[-1] == '?' ? self == method_name[0..-2] : super 49 | end 50 | 51 | private 52 | 53 | # @return [String] name of the directory in which this application is 54 | # @note Will return only the last part, so if the dir is /home/apps/my_app it will 55 | # only return the 'my_app' part 56 | def app_dir_name 57 | Pathname 58 | .new(ENV['BUNDLE_GEMFILE']) 59 | .dirname 60 | .basename 61 | .to_s 62 | end 63 | 64 | # Converts any string into a bash ENV key 65 | # @param string [String] string we want to convert into an env key 66 | # @return [String] converted name that can be an ENV key 67 | def to_env_key(string) 68 | INFLECTOR 69 | .underscore(string) 70 | .tr('/', '_') 71 | .upcase + ENV_KEY_POSTFIX 72 | end 73 | end 74 | end 75 | -------------------------------------------------------------------------------- /lib/envlogic/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | # Main module to encapsulate logic 4 | module Envlogic 5 | # Current version of gem 6 | VERSION = '1.1.5' 7 | end 8 | -------------------------------------------------------------------------------- /spec/lib/envlogic/env_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe Envlogic::Env do 4 | subject(:envlogic_env) { described_class.new(test_class) } 5 | 6 | let(:test_class) { ClassBuilder.build } 7 | 8 | describe '#initialize' do 9 | context 'when we dont have any ENVs that we can use' do 10 | before do 11 | ENV['RACK_ENV'] = nil 12 | envlogic_env 13 | end 14 | 15 | it 'expect to use FALLBACK_ENV' do 16 | expect(envlogic_env.send(:initialize, test_class)).to eq 'development' 17 | end 18 | end 19 | 20 | context 'when app_dir_name env key env is set' do 21 | let(:env_value) { rand.to_s } 22 | 23 | before do 24 | ENV['ENVLOGIC_ENV'] = env_value 25 | envlogic_env 26 | end 27 | 28 | after { ENV['ENVLOGIC_ENV'] = nil } 29 | 30 | it 'expect to use it' do 31 | expect(envlogic_env.send(:initialize, test_class)).to eq env_value 32 | end 33 | end 34 | 35 | context 'when class name env is set' do 36 | let(:test_class) { stub_const('ClassName', ClassBuilder.build) } 37 | let(:env_value) { rand.to_s } 38 | 39 | before do 40 | ENV['CLASS_NAME_ENV'] = env_value 41 | envlogic_env 42 | end 43 | 44 | after { ENV['CLASS_NAME_ENV'] = env_value } 45 | 46 | it 'expect to use it' do 47 | expect(envlogic_env.send(:initialize, test_class)).to eq env_value 48 | end 49 | end 50 | 51 | context 'when FALLBACK_ENV_KEY value is set' do 52 | let(:env_value) { rand.to_s } 53 | 54 | before do 55 | ENV['RACK_ENV'] = env_value 56 | envlogic_env 57 | end 58 | 59 | after { ENV['RACK_ENV'] = nil } 60 | 61 | it 'expect to use it' do 62 | expect(envlogic_env.send(:initialize, test_class)).to eq env_value 63 | end 64 | end 65 | end 66 | 67 | describe '#update' do 68 | let(:new_env) { rand.to_s } 69 | 70 | before do 71 | envlogic_env 72 | ENV['CLASS_NAME_ENV'] = new_env 73 | end 74 | 75 | it 'expect to replace self with inquired new value' do 76 | envlogic_env.update(new_env) 77 | expect(envlogic_env).to eq new_env 78 | end 79 | end 80 | 81 | describe '#respond_to? with missing' do 82 | context 'when we check for regular existing methods' do 83 | %w[ 84 | chop 85 | upcase! 86 | ].each do |method_name| 87 | it 'expect not to respond to those' do 88 | expect(envlogic_env.respond_to?(method_name)).to eq true 89 | end 90 | end 91 | end 92 | 93 | context 'when we check for regular named non-existing methods' do 94 | %w[ 95 | supermethod 96 | extra_other 97 | ].each do |method_name| 98 | it 'expect not to respond to those' do 99 | expect(envlogic_env.respond_to?(method_name)).to eq false 100 | end 101 | end 102 | end 103 | 104 | context 'when we check for questionmark environmentable methods' do 105 | %w[ 106 | test? 107 | production? 108 | development? 109 | unknown? 110 | ].each do |method_name| 111 | it 'expect not to respond to those' do 112 | expect(envlogic_env.respond_to?(method_name)).to eq true 113 | end 114 | end 115 | end 116 | end 117 | 118 | describe '#app_dir_name' do 119 | it 'expect to get a basename from dirname' do 120 | expect(envlogic_env.send(:app_dir_name)).to eq 'envlogic' 121 | end 122 | end 123 | 124 | %w[ 125 | production test development 126 | ].each do |env| 127 | context "environment: #{env}" do 128 | before { envlogic_env.update(env) } 129 | 130 | it { expect(envlogic_env.public_send(:"#{env}?")).to eq true } 131 | end 132 | end 133 | 134 | %w[ 135 | unknown 136 | unset 137 | invalid 138 | ].each do |env| 139 | context "environment: #{env}" do 140 | it { expect(envlogic_env.public_send(:"#{env}?")).to eq false } 141 | end 142 | end 143 | end 144 | -------------------------------------------------------------------------------- /spec/lib/envlogic/version_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe Envlogic do 4 | it 'has a version number' do 5 | expect(Envlogic::VERSION).not_to be nil 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /spec/lib/envlogic_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe Envlogic do 4 | subject(:envlogic) do 5 | extended = described_class 6 | 7 | ClassBuilder.build do 8 | extend extended 9 | end 10 | end 11 | 12 | describe '#env' do 13 | before { envlogic.instance_variable_set(:'@env', env) } 14 | 15 | context 'when env is not yet set' do 16 | let(:env) { nil } 17 | 18 | it 'expect to create envlogic env instance and return it' do 19 | expect(envlogic.env).to be_a described_class::Env 20 | end 21 | end 22 | 23 | context 'when env is set' do 24 | let(:env) { double } 25 | 26 | it 'expect to return it' do 27 | expect(envlogic.env).to eq env 28 | end 29 | end 30 | end 31 | 32 | describe '#env=' do 33 | let(:stringified_env) { double } 34 | let(:new_env) { instance_double(Envlogic::Env, to_s: stringified_env) } 35 | 36 | it 'expect to execute update on env' do 37 | expect(envlogic.env) 38 | .to receive(:update) 39 | .with(stringified_env) 40 | 41 | envlogic.env = new_env 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | coverage = !ENV.key?('GITHUB_WORKFLOW') 4 | coverage = true if ENV['GITHUB_COVERAGE'] == 'true' 5 | 6 | if coverage 7 | require 'simplecov' 8 | 9 | # Don't include unnecessary stuff into rcov 10 | SimpleCov.start do 11 | add_filter '/spec/' 12 | add_filter '/vendor/' 13 | add_filter '/gems/' 14 | add_filter '/.bundle/' 15 | add_filter '/doc/' 16 | add_filter '/config/' 17 | 18 | merge_timeout 600 19 | minimum_coverage 100 20 | enable_coverage :branch 21 | end 22 | end 23 | 24 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"] 25 | .sort 26 | .each { |f| require f } 27 | 28 | RSpec.configure do |config| 29 | config.disable_monkey_patching! 30 | config.order = :random 31 | 32 | config.expect_with :rspec do |expectations| 33 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 34 | end 35 | end 36 | 37 | require 'envlogic' 38 | -------------------------------------------------------------------------------- /spec/support/class_builder.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | # Class builder helps creating anonymous classes that we can use to spec our code 4 | # We need co create new class instances to have an "empty" and "clear" class for each spec 5 | # This module acts as an interface to create classes 6 | module ClassBuilder 7 | class << self 8 | # Creates an empty class without any predefined methods 9 | # @param block [Proc, nil] block that should be evaluated (if given) 10 | # @return [Class] created anonymous class 11 | def build(&block) 12 | klass = Class.new 13 | 14 | klass.class_eval(&block) if block_given? 15 | klass 16 | end 17 | 18 | # This method allows us to create a class that inherits from any other 19 | # @param klass [Class] any class from which we want to inherit in our anonymous class 20 | # @param block [Proc] a block of code that should be evaluated in a new anonymous class body 21 | # @return [Class] new anonymous class 22 | def inherit(klass, &block) 23 | Class.new(klass, &block) 24 | end 25 | end 26 | end 27 | --------------------------------------------------------------------------------