Class: Sidewalk::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/sidewalk/config.rb

Overview

Class for storing application configuration.

This will:

  • load config/environment.rb (arbitrary Ruby)

  • read config/environment.yaml into a Hash

  • the same for config/#{ENV}.rb and .yaml

Examples:

environment.yaml

activerecord:
  adapter: sqlite
  database: data.sqlite

Reading from Config

ActiveRecord::Base.establish_connection(
  Sidewalk::Config['activerecord']
)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Config

Initialize a Config object at the given path.

You probably want to use the class methods instead.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sidewalk/config.rb', line 26

def initialize path
  @config = Hash.new
  @root = path
  [
    'environment.rb',
    "#{Config.environment}.rb",
  ].each{|x| load_ruby!(x, :silent => true)}

  yaml_configs = [
    'environment.yaml',
    "#{Config.environment}.yaml",
  ].each{|x| load_yaml!(x, :silent => true)}
end

Class Attribute Details

.pathObject

Where to look for configuration files.

This defaults to config/ underneath the application root.



117
118
119
# File 'lib/sidewalk/config.rb', line 117

def path
  @path ||= Application.local_root + '/config'
end

Class Method Details

.development?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/sidewalk/config.rb', line 147

def development?
  self.environment == 'development'
end

.environmentObject

What the current Rack environment is.

This is an arbitrary string, but will usually be:

production

this is live, probably via Passenger

development

running on a developer’s local machine, probably via

+rackup+ or +shotgun+
testing

automated tests are running.

This is copied from ENV, but can be overridden (see #environment=).



132
133
134
# File 'lib/sidewalk/config.rb', line 132

def environment
  @environment ||= ENV['RACK_ENV'] || raise("Unable to determine environment. Set ENV['RACK_ENV'].")
end

.environment=(foo) ⇒ Object

Override the auto-detected environment.

Handy for testing - especially as RACK_ENV might not even be set.



139
140
141
# File 'lib/sidewalk/config.rb', line 139

def environment= foo
  @environment = foo
end

.instanceConfig Also known as: init!

The main instance of Sidewalk::Config.

You probably don’t want to use this - all of the methods defined on instances are usable as class methods instead, that map onto the instance.

Returns:



109
110
111
# File 'lib/sidewalk/config.rb', line 109

def instance
  @instance ||= Config.new(self.path)
end

.production?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/sidewalk/config.rb', line 143

def production?
  self.environment == 'production'
end

.testing?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/sidewalk/config.rb', line 151

def testing?
  self.environment == 'testing'
end

Instance Method Details

#[](key) ⇒ Object

Return a configuration value from YAML.

Acts like a Hash. Also available as a class method.

Parameters:

  • key (String)


45
46
47
# File 'lib/sidewalk/config.rb', line 45

def [] key
  @config[key]
end

#[]=(key, value) ⇒ Object

Store a configuration value, as if it were in the YAML.

Also available as a class method.

Parameters:

  • key (String)
  • value (Object)


55
56
57
# File 'lib/sidewalk/config.rb', line 55

def []= key, value
  @config[key] = value
end

#load_ruby!(file, options = {}) ⇒ Object

Execute a Ruby file.

Nothing special is done, it’s just evaluated.

Also available as a class method.

Parameters:

  • file (String)

    the path to the file, relative to the base configuration path.

  • options (Hash) (defaults to: {})

Raises:

  • (LoadError)

    if file does not exist, unless :silent is set to true in options.



70
71
72
73
74
75
76
77
# File 'lib/sidewalk/config.rb', line 70

def load_ruby! file, options = {}
  path = File.join(@root, file)
  begin
    load path
  rescue LoadError
    raise unless options[:silent]
  end
end

#load_yaml!(file, options = {}) ⇒ Object

Read a YAML file, and merge with the current config.

This is available via #[]

Also available as a class method.

Parameters:

  • file (String)

    the path to the file, relative to the base configuration path.

  • options (Hash) (defaults to: {})

Raises:

  • (LoadError)

    if file does not exist, unless :silent is set to true in options.



90
91
92
93
94
95
96
97
98
99
# File 'lib/sidewalk/config.rb', line 90

def load_yaml! file, options = {}
  path = File.join(@root, file)
  if File.exists? path
    @config.merge! YAML.load(File.read(path))
  else
    unless options[:silent]
      raise LoadError.new("unable to find #{file}")
    end
  end
end