Class: Halcyon::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/halcyon/config.rb,
lib/halcyon/config/file.rb,
lib/halcyon/config/paths.rb,
lib/halcyon/config/helpers.rb

Overview

Application configuration map.

Defined Under Namespace

Modules: Helpers Classes: File, Paths

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, &block) ⇒ Config

Creates an empty configuration hash (Mash) and sets up the configuration to whatever the settings are provided, merging over the defaults.

Examples:

Halcyon::Config.new(:environment => :development)

OR

Halcyon::Config.new(:allow_from => :all)

OR

Halcyon::Config.new

OR

Halcyon::Config.new do |c|
  c[:foo] = true
end


34
35
36
37
38
39
# File 'lib/halcyon/config.rb', line 34

def initialize(config={}, &block)
  env = config.delete(:environment)
  self.config = Mash.new
  self.setup(self.defaults(env).merge(config))
  self.use(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Allows retrieval of single key config values and setting single config values.

Examples:

Halcyon.config.app #=> 'AppName'
Halcyon.config[:app] #=> 'AppName'


93
94
95
96
97
98
99
# File 'lib/halcyon/config.rb', line 93

def method_missing(method, *args)
  if method.to_s[-1,1] == '='
    self.put(method.to_s.tr('=',''), *args)
  else
    self.get(method)
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/halcyon/config.rb', line 7

def config
  @config
end

Class Method Details

.defaults(env = nil) ⇒ Object

Default configuration values.

Defaults to the configuration for :development.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/halcyon/config.rb', line 198

def defaults(env = nil)
  base = {
    :app => nil,
    :root => Dir.pwd,
    :environment => :development,
    :allow_from => 'all',
    :logging => {
      :type => 'Logger',
      :level => 'debug'
    },
    :paths => Paths.new,
    :hooks => {:startup => [], :shutdown => []}
  }
  case (env || :development)
  when :development
    base.merge({
      :environment => :development
    })
  when :test
    base.merge({
      :app => 'Specs',
      :environment => :test,
      :logging => {
        :type => 'Logger',
        :level => 'warn',
        :file => 'log/test.log'
      }
    })
  when :console
    base.merge({
      :environment => :console
    })
  when :production
    base.merge({
      :environment => :production,
      :logging => {
        :type => 'Logger',
        :level => 'warn',
        :file => 'log/production.log'
      }
    })
  end
end

.load_from(path) ⇒ Object

Loads the contents of a configuration file found at path.



244
245
246
# File 'lib/halcyon/config.rb', line 244

def load_from(path)
  Halcyon::Config::File.load(path)
end

Instance Method Details

#[](key) ⇒ Object

Alias for the get method.

Examples:

Halcyon.config[:foo] #=> true


145
146
147
# File 'lib/halcyon/config.rb', line 145

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

#[]=(key, value) ⇒ Object

Alias for the put method. (Restricted to the key/value pair.)

Examples:

Halcyon.config[:foo] = true


155
156
157
# File 'lib/halcyon/config.rb', line 155

def []=(key, value)
  self.put(key, value)
end

#configure(config = {}) ⇒ Object

Sets the configuration up with the values given.



43
44
45
46
47
# File 'lib/halcyon/config.rb', line 43

def configure(config={})
  config.each do |(key, val)|
    self.config[key] = val
  end
end

#defaults(env = nil) ⇒ Object

Shortcut for Halcyon::Config.defaults.



174
175
176
# File 'lib/halcyon/config.rb', line 174

def defaults(env = nil)
  Halcyon::Config.defaults(env)
end

#delete(key) ⇒ Object

Removes the configuration value from the hash.

Examples:

Halcyon.config.delete(:app) #=> 'AppName'


135
136
137
# File 'lib/halcyon/config.rb', line 135

def delete(key)
  self.config.delete(key)
end

#get(key) ⇒ Object

Get the configuration value associated with the key.

Examples:

Halcyon.config.get(:app) #=> 'AppName'


107
108
109
# File 'lib/halcyon/config.rb', line 107

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

#inspectObject



186
187
188
189
190
# File 'lib/halcyon/config.rb', line 186

def inspect
  attrs = ""
  self.config.keys.each {|key| attrs << " #{key}=#{self.config[key].inspect}"}
  "#<Halcyon::Config#{attrs}>"
end

#load_from(path) ⇒ Object

Loads the contents of a configuration file found at path and merges it with the current configuration.



181
182
183
184
# File 'lib/halcyon/config.rb', line 181

def load_from(path)
  self.configure(Halcyon::Config::File.load(path))
  self
end

#put(key_or_config_hash, value = nil) ⇒ Object

Put the configuration value associated with the key or setup with a hash.

Examples:

Halcyon.config.put(:app, 'AppName')

OR

Halcyon.config.put(:app => 'AppName')


121
122
123
124
125
126
127
# File 'lib/halcyon/config.rb', line 121

def put(key_or_config_hash, value = nil)
  if value.nil? and key_or_config_hash.is_a?(Hash)
    self.configure(key_or_config_hash)
  else
    self.config[key_or_config_hash] = value
  end
end

#setup(config = {}) ⇒ Object

Sets up the configuration by storing the settings provided (via param or via block).

Usage:

Halcyon.config.setup do |c|
  c[:foo] = true
end

or

Halcyon.config.setup(:foo => true)


62
63
64
65
66
67
68
# File 'lib/halcyon/config.rb', line 62

def setup(config={})
  if block_given?
    yield(self.config.dup)
  end
  # merge new settings
  self.configure(config)
end

#to_hashObject

Returns the configuration as a hash.



168
169
170
# File 'lib/halcyon/config.rb', line 168

def to_hash
  self.config.to_hash
end

#to_yamlObject

Returns the configuration rendered as YAML.



161
162
163
164
# File 'lib/halcyon/config.rb', line 161

def to_yaml
  require 'yaml'
  self.config.to_hash.to_yaml
end

#useObject

Yields and returns the configuration.

Examples:

Halcyon.config.use do |c|
  c[:foo] = true
end


78
79
80
81
82
83
# File 'lib/halcyon/config.rb', line 78

def use
  if block_given?
    yield self.config
  end
  self.config
end