Module: Ext::Settings::CClassMethods

Defined in:
lib/ext/settings.rb

Overview

Class methods for your app.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#settings_pathObject

Returns the value of attribute settings_path.



50
51
52
# File 'lib/ext/settings.rb', line 50

def settings_path
  @settings_path
end

Instance Method Details

#load_settings(yaml_file = settings_path) ⇒ Object

Loads the specified file. If none is given, it will look in /etc and your home folder for a file called you_app.yaml



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ext/settings.rb', line 83

def load_settings(yaml_file=settings_path)
  if yaml_file
    settings.load(yaml_file)
    puts "INFO: '#{yaml_file}' loaded." if $DBG
  else
    name = self.name.methodize
 
    # YAML config
    configs = []
    configs << File.join(ENV['HOME'], ".#{name}.conf") if ENV['HOME']
    configs << File.join(ENV['APPDATA'], "#{name}.conf") if ENV['APPDATA']
    configs << "/etc/#{name}.conf"
    configs.each do |conf|
      conf = File.expand_path(conf)
      if File.exists?(conf)
        settings_path = conf
        load_settings(conf)
        break
      end
    end

    # Ruby config
    configs = []
    configs << File.join(ENV['HOME'], ".#{name}.rb") if ENV['HOME']
    configs << File.join(ENV['APPDATA'], "#{name}.rb") if ENV['APPDATA']
    configs << "/etc/#{name}.rb"
    configs.each do |conf|
      conf = File.expand_path(conf)
      if File.exists?(conf)
        load conf
        break
      end
    end
  end
end

#save_settings(yaml_file = settings_path) ⇒ Object

Export the settings once they’re changed. You’ll have to call this by hand.

Oh and, the keys will be stored in the following format : ‘YourApp.keyname’

Raises:

  • (ArgumentError)


124
125
126
127
128
129
130
# File 'lib/ext/settings.rb', line 124

def save_settings(yaml_file=settings_path)
  raise ArgumentError, "No settings path given" unless yaml_file
  if File.directory?(yaml_file)
    yaml_file = File.join(yaml_file, self.name.methodize+'.conf')
  end
  settings.save(yaml_file)
end

#setting(key, value, &on_change) ⇒ Object

This is the method that let you create settings. If you pass a block, it will be called when the key is assigned and the return value will be used.

Example

setting :user, 'HEY', do |new,old|
  new.downcase
end

self.user #=> "hey"


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ext/settings.rb', line 63

def setting(key, value, &on_change)
  settings.set(key, value, &on_change)

  # reader
  metaclass.send :define_method, key do
    settings[key].value
  end

  # writer
  metaclass.send :define_method, "#{key}=" do |val|
    settings[key].value = val
  end     
end

#settingsObject

The group of settings. You can call settings.to_yaml to get the output.



79
# File 'lib/ext/settings.rb', line 79

def settings; @settings ||= SettingGroup.new(self) end