Class: Configurative::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/configurative/settings.rb

Constant Summary collapse

@@options =
{}
@@instances =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Settings

Returns a new instance of Settings.



11
12
13
14
# File 'lib/configurative/settings.rb', line 11

def initialize(options={})
  @settings = nil
  self.class.options_for(self.class).merge!(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



64
65
66
# File 'lib/configurative/settings.rb', line 64

def method_missing(method_name, *arguments, &block)
  self.class.method_missing(method_name, *arguments, &block) || super
end

Class Method Details

.[](key) ⇒ Object



40
41
42
# File 'lib/configurative/settings.rb', line 40

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

.[]=(key, value) ⇒ Object



44
45
46
# File 'lib/configurative/settings.rb', line 44

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

.empty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/configurative/settings.rb', line 56

def self.empty?
  instance_for(self).empty?
end

.environment(setting) ⇒ Object



98
99
100
# File 'lib/configurative/settings.rb', line 98

def self.environment(setting)
  options_for(self)[:environment] = setting
end

.fetch(key, alternative = nil) ⇒ Object



48
49
50
# File 'lib/configurative/settings.rb', line 48

def self.fetch(key, alternative=nil)
  self[key] || alternative
end

.files(*files) ⇒ Object



90
91
92
# File 'lib/configurative/settings.rb', line 90

def self.files(*files)
  options_for(self)[:files] = files
end

.include?(key) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/configurative/settings.rb', line 52

def self.include?(key)
  instance_for(self).include?(key)
end

.instanceObject



16
17
18
# File 'lib/configurative/settings.rb', line 16

def self.instance
  instance_for(self)
end

.load(*files) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/configurative/settings.rb', line 110

def self.load(*files)
  settings = nil
  path     = find_file(*files)
  if path
    begin
      settings          = SettingsLoader.new(options).load!(path)
      @@instances[self] = settings
    rescue => error
      # Deliberately ignored.
    end
  end
  settings
end

.load!(*files) ⇒ Object

Raises:



124
125
126
127
128
# File 'lib/configurative/settings.rb', line 124

def self.load!(*files)
  settings = load(*files)
  raise ConfigurationError, "Unable to locate an accessible configuration file." if settings.nil?
  settings
end

.method_missing(method_name, *arguments, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/configurative/settings.rb', line 77

def self.method_missing(method_name, *arguments, &block)
  data = instance_for(self)
  if method_name[-1,1] == "="
    data[property_name(method_name)] = arguments.first
  else
    if data.include?(method_name)
      data[method_name]
    else
      super
    end
  end
end

.namespace(setting) ⇒ Object



102
103
104
# File 'lib/configurative/settings.rb', line 102

def self.namespace(setting)
  environment(setting)
end

.reset(full = false) ⇒ Object



68
69
70
71
# File 'lib/configurative/settings.rb', line 68

def self.reset(full=false)
  @@instances = {}
  @@options.delete(self) if full
end

.respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/configurative/settings.rb', line 73

def self.respond_to?(method_name, include_private=false)
  instance_for(self).include?(property_name(method_name)) || super
end

.section(setting) ⇒ Object



106
107
108
# File 'lib/configurative/settings.rb', line 106

def self.section(setting)
  options_for(self)[:section] = setting
end

.sources(*files) ⇒ Object



94
95
96
# File 'lib/configurative/settings.rb', line 94

def self.sources(*files)
  files(*files)
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/configurative/settings.rb', line 20

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

#[]=(key, value) ⇒ Object



24
25
26
# File 'lib/configurative/settings.rb', line 24

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

#empty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/configurative/settings.rb', line 36

def empty?
  self.class.empty?
end

#fetch(key, alternative = nil) ⇒ Object



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

def fetch(key, alternative=nil)
  self.class.fetch(key, alternative)
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/configurative/settings.rb', line 32

def include?(key)
  self.class.include?(key)
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/configurative/settings.rb', line 60

def respond_to?(method_name, include_private=false)
  self.class.instance_for?(self.class).include?(property_name(method_name)) || super
end