Module: Waffle::Config

Extended by:
Config
Included in:
Config
Defined in:
lib/waffle/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultsObject

Returns the value of attribute defaults.



6
7
8
# File 'lib/waffle/config.rb', line 6

def defaults
  @defaults
end

#settingsObject

Returns the value of attribute settings.



6
7
8
# File 'lib/waffle/config.rb', line 6

def settings
  @settings
end

Instance Method Details

#environmentObject



53
54
55
# File 'lib/waffle/config.rb', line 53

def environment
  defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : (ENV['RACK_ENV'] || 'development')
end

#load!(options = nil) ⇒ Object



34
35
36
37
# File 'lib/waffle/config.rb', line 34

def load! options=nil
  options[:path] ? load_from_yaml!(options[:path]) : load_from_hash!(options)
  self
end

#load_from_hash!(options) ⇒ Object



49
50
51
# File 'lib/waffle/config.rb', line 49

def load_from_hash! options
  @settings = defaults.merge(options)
end

#load_from_yaml!(filename) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/waffle/config.rb', line 39

def load_from_yaml! filename
  filename = Rails.root.join(filename) if defined?(Rails)
  filename = File.expand_path(filename)

  if File.exists?(filename)
    settings_hash = YAML.load_file(filename)[environment]
    @settings = defaults.merge(settings_hash.symbolize_keys) if settings_hash
  end
end

#option(name, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/waffle/config.rb', line 11

def option(name, options = {})
  defaults[name] = settings[name] = options[:default]

  class_eval <<-RUBY
    def #{name}
      settings[#{name.inspect}]
    end

    def #{name}=(value)
      settings[#{name.inspect}] = value
    end

    def #{name}?
      #{name}
    end
  RUBY
end