Sha256: bf15e02483e3e2d325fd3aaa7ea3c3347580a48230ad441647eddd09a5217059

Contents?: true

Size: 816 Bytes

Versions: 1

Compression:

Stored size: 816 Bytes

Contents

require "easy-settings/coercion"

class EasySettings::EnvSource
  attr_reader :prefix, :separator, :converter, :parse_values

  def initialize(prefix, separator: "__", converter: :downcase, parse_values: true)
    @prefix = prefix
    @separator = separator
    @converter = converter
    @parse_values = parse_values
  end

  def load
    {}.tap do |data|
      ENV.each do |variable, value|
        keys = variable.to_s.split(separator)
        next if prefix.present? && keys.shift != prefix

        keys.map!{ |key| key.send(converter) } if converter

        leaf = keys[0...-1].inject(data){ |h, key| h[key] ||= {} }
        leaf[keys.last] = parse_values ? EasySettings::Coercion.new(value).run : value
      end
    end
  rescue NoMethodError => e
    raise "Invalid name converter: #{converter}"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
easy-settings-1.1.0 lib/easy-settings/env_source.rb