Sha256: d336aa4d50712e6354b15789e0a076b23137a06f0522a3e03ead0c49838855b7

Contents?: true

Size: 801 Bytes

Versions: 1

Compression:

Stored size: 801 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 unless 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.0.1 lib/easy-settings/env_source.rb