Sha256: efdff53e2262fdc56b654640f304e90d473f6fdcd249028ed6b00493843bf19d

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

require 'active_support'

module Gris
  class << self
    def env
      @_env ||= ActiveSupport::StringInquirer.new(ENV['RACK_ENV'] || 'development')
    end

    def env=(environment)
      @_env = ActiveSupport::StringInquirer.new(environment)
    end

    # adapted from https://github.com/rails/rails/blob/master/railties/lib/rails/application.rb
    # Returns secrets added to config/secrets.yml.
    def secrets
      @secrets ||= begin
        secrets = ActiveSupport::OrderedOptions.new
        yaml = 'config/secrets.yml'
        if File.exist?(yaml)
          require 'erb'
          all_secrets = YAML.load(ERB.new(IO.read(yaml)).result) || {}
          env_secrets = all_secrets[Gris.env]
          secrets.merge!(env_secrets.symbolize_keys) if env_secrets
        end
        secrets
      end
    end

    def db_connection_details
      YAML.load(ERB.new(File.read('./config/database.yml')).result)[Gris.env]
    end

    def cache
      @_cache ||= ActiveSupport::Cache.lookup_store(:memory_store)
    end

    def cache=(store_option)
      @_cache = ActiveSupport::Cache.lookup_store(store_option)
    end
  end
end

# prevent DalliError: Unable to unmarshal value: undefined class/module
Gris.cache.instance_eval do
  def fetch(key, options = {}, rescue_and_require = true)
    super(key, options)

  rescue ArgumentError => ex
    if rescue_and_require && %r{^undefined class\/module (.+?)$} =~ ex.message
      self.class.const_missing(Regexp.last_match(1))
      fetch(key, options, false)
    else
      raise ex
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gris-0.3.3 lib/gris/setup.rb
gris-0.3.2 lib/gris/setup.rb
gris-0.3.1 lib/gris/setup.rb
gris-0.3.0 lib/gris/setup.rb