Sha256: 033386abadb52f0a91da0bb89b0359454de2238f78f56e66c770a998ae6cd63d

Contents?: true

Size: 913 Bytes

Versions: 6

Compression:

Stored size: 913 Bytes

Contents

module Figaro
  module ENV
    extend self

    def respond_to?(method, *)
      key, punctuation = extract_key_from_method(method)

      case punctuation
      when "!" then has_key?(key) || super
      when "?", nil then true
      else super
      end
    end

    private

    def method_missing(method, *)
      key, punctuation = extract_key_from_method(method)

      case punctuation
      when "!" then send(key) || missing_key!(key)
      when "?" then !!send(key)
      when nil then get_value(key)
      else super
      end
    end

    def extract_key_from_method(method)
      method.to_s.downcase.match(/^(.+?)([!?=])?$/).captures
    end

    def has_key?(key)
      ::ENV.any? { |k, _| k.downcase == key }
    end

    def missing_key!(key)
      raise MissingKey.new(key)
    end

    def get_value(key)
      _, value = ::ENV.detect { |k, _| k.downcase == key }
      value
    end
  end
end

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
daqing_figaro-1.2.1 lib/figaro/env.rb
figaro-1.2.0 lib/figaro/env.rb
seielit-figaro-1.1.2 lib/figaro/env.rb
figaro-1.1.1 lib/figaro/env.rb
figaro-1.1.0 lib/figaro/env.rb
figaro-1.0.0 lib/figaro/env.rb