Module: Configurable

Included in:
Geeklet
Defined in:
lib/configurable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.extend(Configurable)
end

Instance Method Details

#add_overrides(group, params) ⇒ Object

Raises:

  • (Trollop::HelpNeeded)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/configurable.rb', line 47

def add_overrides(group, params)
  return if (params.nil? || params.empty?) # nothing to do in this case
  raise Trollop::HelpNeeded if isHelp?(params[0])

  groupConfigs = configurations[group]
  trollop_opts = command_parser(group).parse(params)
  trollop_opts.each do |key, value| 
    groupConfigs[key][:override] = value if groupConfigs.key?(key) 
  end
  
  # puts "trollop opts:"
  # puts trollop_opts.to_yaml
  # puts
  # puts "group configs:"
  # puts groupConfigs.to_yaml
end

#command_parser(group) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/configurable.rb', line 30

def command_parser(group)
  groupConfigs = configurations[group]
  return unless groupConfigs #if there are no configs defined, there is nothing to do.
  
  parser = Trollop::Parser.new do
    groupConfigs.each do |key, options|
      # our concept of a default is different from trollop's, so remove any default key and value
      defaultValue = options[:stored] || options[:default]
      if defaultValue
        opt key, options[:description], options.merge({ :default => defaultValue })
      else
        opt key, options[:description], options
      end
    end
  end
end

#configurableValue(group, key) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/configurable.rb', line 17

def configurableValue(group, key)
  raise UnregisteredConfigValueError unless configurations.include?(group)
  raise UnregisteredConfigValueError unless configurations[group].include?(key)
  value = configurations[group][key][:override] || configurations[group][key][:stored] || configurations[group][key][:default]
  raise ConfigurationRequiredError unless value
  value
end

#configurationsObject

configurations should be a hash of hashes, where the first key is the calculator type, and the second key is the tag



9
10
11
# File 'lib/configurable.rb', line 9

def configurations
  @@configuration ||= {}
end

#isHelp?(param) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/configurable.rb', line 13

def isHelp?(param)
  param.to_s.downcase == 'help'
end

#registerConfiguration(group, key, options = {}) ⇒ Object



25
26
27
28
# File 'lib/configurable.rb', line 25

def registerConfiguration(group, key, options = {})
  configurations[group] ||= {}
  configurations[group][key] = options
end