Sha256: c900a92ebe6247b9b622a528702c6dc5dbee3759ec220e7a50e8f6bd7d86e7e1

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require "yaml_properties/version"
require "active_support/core_ext/hash"
require "yaml"
require "erb"

module YamlProperties
  def properties
    reset! if dev_environment

    @properties ||= load_properties.with_indifferent_access
  end

  attr_accessor :dev_environment

  def reset!
    reset_properties
  end

  def reset_properties
    @properties = nil
  end

  def method_missing(key, *args, &block)
    return properties[key] if key_present? key

    super key, *args, &block
  end

  def fetch(key, default = nil)
    key_present?(key) ? properties[key] : default
  end

  def override_attribute attribute, value
    unless key_present? attribute
      raise ArgumentError, "Trying to override non-existent property `#{attribute}' with `#{value}'"
    end
    properties[attribute] = value
  end

  private

  def key_present? key
    properties.keys.include? key.to_s
  end

  def yaml_file
    File.join %w(config properties.yml)
  end

  def load_properties
    yaml = YAML.load ERB.new(File.read(yaml_file)).result

    yaml
  end

  extend self
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shutl_yaml_properties-0.1.0 lib/yaml_properties.rb