Sha256: a0635aa0c086610b986cb9869be687c7fb52b5ac60737221e5dc344fa501abe5

Contents?: true

Size: 911 Bytes

Versions: 2

Compression:

Stored size: 911 Bytes

Contents

require "easy-settings/coercion"

class EasySettings::PathSource
  attr_reader :base_path, :separator, :converter, :parse_values

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

  def load
    {}.tap do |data|
      Dir["#{base_path}/*"].each do |path|
        next unless File.file?(path)

        variable = path.gsub("#{base_path}/", "")
        value = File.read(path).strip

        keys = variable.to_s.split(separator)
        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

2 entries across 2 versions & 1 rubygems

Version Path
easy-settings-1.1.0 lib/easy-settings/path_source.rb
easy-settings-1.0.1 lib/easy-settings/path_source.rb