Sha256: b81ccb4f380d1825a063f9d4edeb6149e24d6f423368dfdbbbcc763a3e7aad07

Contents?: true

Size: 1.21 KB

Versions: 19

Compression:

Stored size: 1.21 KB

Contents

module Dpl
  module ConfigFile
    def self.included(base)
      base.extend(ClassMethods)
    end

    # should this sit in Cl?
    module ClassMethods
      attr_reader :config_files

      def config(*paths)
        if paths.any?
          @config_files = paths
        elsif config_files
          paths = config_files.dup
          opts = paths.last.is_a?(Hash) ? paths.pop : {}
          conf = ConfigFiles.new(paths, opts).config
          known = self.opts.map(&:name).map(&:to_sym)
          conf.select { |key, _| known.include?(key) }
        else
          {}
        end
      end
    end

    class ConfigFiles < Struct.new(:paths, :opts)
      def config
        paths.map { |path| parse(path) }.inject(&:merge) || {}
      end

      def parse(path)
        str = File.exists?(path) ? File.read(path) : ''
        opts = str.lines.select { |line| line.include?('=') }.map(&:strip)
        opts = opts.map { |pair| pair.split('=', 2) }.to_h
        opts.map { |key, value| [strip_prefix(key).to_sym, value] }.to_h
      end

      def strip_prefix(str)
        opts[:prefix] ? str.sub(/^#{opts[:prefix]}[\-_]?/, '') : str
      end
    end

    def opts
      @opts ||= self.class.config.merge(super)
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
dpl-2.0.3.beta.4 lib/dpl/helper/config_file.rb
dpl-2.0.3.beta.3 lib/dpl/helper/config_file.rb
dpl-2.0.3.beta.2 lib/dpl/helper/config_file.rb
dpl-2.0.3.beta.1 lib/dpl/helper/config_file.rb
dpl-2.0.2.beta.1 lib/dpl/helper/config_file.rb
dpl-2.0.0.beta.3 lib/dpl/helper/config_file.rb
dpl-2.0.0.beta.2 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.14 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.13 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.12 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.11 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.10 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.9 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.8 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.7 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.6 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.5 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.2 lib/dpl/helper/config_file.rb
dpl-2.0.0.alpha.1 lib/dpl/helper/config_file.rb