Class: Play::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/play/configuration.rb

Overview

A ‘play` configuration. Usually loaded from YAML, configured in Ruby, or passed on the command line.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg = nil) ⇒ Configuration

Creates a new ‘Configuration` instance.



39
40
41
# File 'lib/play/configuration.rb', line 39

def initialize arg = nil
  @settings = arg || Hash.new
end

Class Attribute Details

.defaultObject

A ‘Configuration` instance that is referenced when a value is not set.



12
13
14
# File 'lib/play/configuration.rb', line 12

def default
  @default
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



35
36
37
# File 'lib/play/configuration.rb', line 35

def settings
  @settings
end

Class Method Details

.dump(path) ⇒ Object

Loads a ‘Configuration`. If given a string, treats it as a file path to a YAML file from which to load the `Configuration`; if given a Hash, loads it directly.



27
28
29
30
31
32
33
# File 'lib/play/configuration.rb', line 27

def self.dump path
  path = File::expand_path path
  Dir::mkdir File.dirname(path)
  File.open(path, File::CREAT|File::TRUNC|File::RDWR, 0644) do |f|
    f.puts YAML::dump(@settings)
  end
end

.load(arg) ⇒ Object

Loads a ‘Configuration`. If given a string, treats it as a file path to a YAML file from which to load the `Configuration`; if given a Hash, loads it directly.



19
20
21
# File 'lib/play/configuration.rb', line 19

def self.load arg
  Configuration.new(arg.is_a?(Hash) ? arg : (YAML.load_file(File.expand_path arg) if File.exist?(File.expand_path arg)))
end

Instance Method Details

#[](key) ⇒ Object

Retrieves a setting from this ‘Configuration`.



45
46
47
# File 'lib/play/configuration.rb', line 45

def [] key
  @settings[key] || (Configuration.default == self ? nil : (Configuration.default.nil? ? nil : Configuration.default[key]))
end

#[]=(key, value) ⇒ Object

Sets a setting for this ‘Configuration`.



51
52
53
# File 'lib/play/configuration.rb', line 51

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