Sha256: 7ea0416f9b7e0ebc29392f685d57c9ded788eddd5946a72d0441b5ed26311d7a

Contents?: true

Size: 872 Bytes

Versions: 1

Compression:

Stored size: 872 Bytes

Contents

require 'digest/md5'

module RailsSettings
  class Default < ::Hash
    class MissingKey < StandardError; end

    class << self
      def enabled?
        source_path && File.exist?(source_path)
      end

      def source(value = nil)
        @source ||= value
      end

      def source_path
        @source || Rails.root.join('config/app.yml')
      end

      def [](key)
        # foo.bar.dar Nested fetch value
        return instance[key] if instance.key?(key)
        keys = key.to_s.split('.')
        instance.dig(*keys)
      end

      def instance
        return @instance if defined? @instance
        @instance = new
      end
    end

    def initialize
      content = open(self.class.source_path).read
      hash = content.empty? ? {} : YAML.load(ERB.new(content).result).to_hash
      hash = hash[Rails.env] || {}
      replace hash
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails-settings-cached-0.7.0 lib/rails-settings/default.rb