Sha256: 3e602e3d2b4da8bde8b27166968c54e563a83144c34282217e5d0827822e1825

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

require_relative '../helper/hash'

module Gem
  module Release
    class Config
      class Env
        include Helper::Hash

        PREFIX = 'GEM_RELEASE_'
        TRUE   = /^(true|yes|on)$/
        FALSE  = /^(false|no|off)$/

        def load
          opts = vars.map { |key, value| to_hash(keys_for(key), cast(value)) }
          opts = opts.inject { |one, other| deep_merge(one, other) }
          opts || {}
        end

        private

          def vars
            ENV.select { |key, _| key.start_with?(PREFIX) }
          end

          def keys_for(key)
            key.sub(PREFIX, '').split('_').map(&:downcase).map(&:to_sym)
          end

          def to_hash(keys, value)
            keys = keys.reverse
            keys.inject(keys.shift => value) { |value, key| { key => value } }
          end

          def cast(value)
            case value
            when TRUE
              true
            when FALSE
              false
            when ''
              false
            else
              value
            end
          end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gem-release-2.2.4 lib/gem/release/config/env.rb
gem-release-2.2.3 lib/gem/release/config/env.rb
gem-release-2.2.3.alpha4 lib/gem/release/config/env.rb
gem-release-2.2.3.alpha3 lib/gem/release/config/env.rb