Class: Junkfood::Settings

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/junkfood/settings.rb

Overview

A Global Settings Singleton class for Rails applications.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Settings) initialize

Reads the settings.yml and settings_<env>.yml file for configuration options and merges them together. The env is the Rails.env, and the settings files are assumed to be in the Rails.root's config subdirectory.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/junkfood/settings.rb', line 36

def initialize
  file = "#{Rails.root}/config/settings.yml"
  env_file = "#{Rails.root}/config/settings_#{Rails.env}.yml"
  @config = {}
  if File.exist? file
    base_cfg = YAML.load File.read(file)
    @config.deep_merge! base_cfg if base_cfg.kind_of? Hash
  end
  if File.exist? env_file
    env_cfg = YAML.load File.read(env_file)
    @config.deep_merge! env_cfg if env_cfg.kind_of? Hash
  end
end

Instance Attribute Details

- (Object) config (readonly)

Returns the value of attribute config



28
29
30
# File 'lib/junkfood/settings.rb', line 28

def config
  @config
end

Class Method Details

+ (Object) [](key)

Looks up the configuration option.

Parameters:

  • key (Object)

    the configuration option key.

Returns:

  • (Object)

    the configuration value.



63
64
65
# File 'lib/junkfood/settings.rb', line 63

def self.[](key)
  config[key]
end

+ (Hash) config

Returns the singleton's configuration.

Returns:

  • (Hash)

    the singleton's configuration.



53
54
55
# File 'lib/junkfood/settings.rb', line 53

def self.config
  instance.config
end