Sha256: 5d7b1dd97bdf85890358e2921689e5fbd2f77d03c906d1d0733b129a912af455

Contents?: true

Size: 898 Bytes

Versions: 1

Compression:

Stored size: 898 Bytes

Contents

require "yaml"
require "active_support/inflector"
require "active_support/core_ext/hash/indifferent_access"

module Configurable
  def config
    self.class.config
  end

  def self.included(base)
    base.send :extend, ClassMethods
  end

  module ClassMethods
    def config_file_path
      "./config/#{name.underscore}.yml"
    end

    def config
      @config ||= load_config.with_indifferent_access
    end

    private

    def load_config
      config_hash = load_from_yaml

      config_hash.default_proc = proc do |hash, key|
        raise Configurable::KeyNotFoundError.new(key)
      end

      config_hash
    end

    def load_from_yaml
      yaml = ERB.new(File.read(config_file_path)).result
      YAML.load(yaml)
    end
  end

  class KeyNotFoundError < StandardError
    def initialize(key)
      super("The key #{key} was not found in the configuration file")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pipekit-0.2.0 lib/pipekit/configurable.rb