Sha256: 030988b13aafb484ebc4b4eae96bbe4877dabba7876d7139f02eeaa54e3f0a5c

Contents?: true

Size: 1.93 KB

Versions: 34

Compression:

Stored size: 1.93 KB

Contents

# encoding: utf-8

module Alchemy
  class Config

    class << self

      # Returns the configuration for given parameter name.
      #
      # @param name [String]
      #
      def get(name)
        show[name.to_s]
      end
      alias_method :parameter, :get

      # Returns a merged configuration of the following files
      # 
      # Alchemy´s default config: +gems/../alchemy_cms/config/alchemy/config.yml+
      # Your apps default config: +your_app/config/alchemy/config.yml+
      # Environment specific config: +your_app/config/alchemy/development.config.yml+
      #
      # An environment specific config overwrites the settings of your apps default config, 
      # while your apps default config has precedence over Alchemy´s default config.
      #
      def show
        @config ||= merge_configs!(alchemy_config, main_app_config, env_specific_config)
      end

    private

      # Alchemy default configuration
      def alchemy_config
        read_file(File.join(File.dirname(__FILE__), '..', '..', 'config/alchemy/config.yml'))
      end

      # Application specific configuration
      def main_app_config
        read_file("#{Rails.root}/config/alchemy/config.yml")
      end

      # Rails Environment specific configuration
      def env_specific_config
        read_file("#{Rails.root}/config/alchemy/#{Rails.env}.config.yml")
      end

      # Tries to load yaml file from given path.
      # If it does not exist, or its empty, it returns an empty Hash.
      #
      def read_file(file)
        return YAML.load_file(file) || {} if File.exists?(file) # YAML.load_file returns false if file is empty.
        {}
      end

      # Merges all given configs together
      # 
      def merge_configs!(*config_files)
        raise LoadError, 'No Alchemy config file found!' if config_files.map(&:blank?).all?
        config = {}
        config_files.each {|h| config.merge!(h.stringify_keys!) }
        config
      end

    end

  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
alchemy_cms-3.1.3 lib/alchemy/config.rb
alchemy_cms-3.2.0.rc1 lib/alchemy/config.rb
alchemy_cms-3.2.0.beta lib/alchemy/config.rb
alchemy_cms-2.9.1 lib/alchemy/config.rb
alchemy_cms-3.1.1 lib/alchemy/config.rb
alchemy_cms-3.0.4 lib/alchemy/config.rb
alchemy_cms-3.1.0 lib/alchemy/config.rb
alchemy_cms-3.1.0.rc3 lib/alchemy/config.rb
alchemy_cms-3.1.0.rc2 lib/alchemy/config.rb
alchemy_cms-3.1.0.rc1 lib/alchemy/config.rb
alchemy_cms-3.1.0.beta6 lib/alchemy/config.rb
alchemy_cms-3.1.0.beta5 lib/alchemy/config.rb
alchemy_cms-3.1.0.beta4 lib/alchemy/config.rb
alchemy_cms-3.0.3 lib/alchemy/config.rb
alchemy_cms-3.1.0.beta3 lib/alchemy/config.rb
alchemy_cms-3.1.0.beta2 lib/alchemy/config.rb
alchemy_cms-3.1.0.beta1 lib/alchemy/config.rb
alchemy_cms-3.0.2 lib/alchemy/config.rb
alchemy_cms-3.0.1 lib/alchemy/config.rb
alchemy_cms-3.0.0 lib/alchemy/config.rb