Module: Configliere::Define

Defined in:
lib/configliere/define.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Pretend that any #define’d parameter is a method

Examples:

Settings.define :foo
Settings.foo = 4
Settings.foo      #=> 4


170
171
172
173
174
175
176
177
178
179
# File 'lib/configliere/define.rb', line 170

def method_missing meth, *args
  meth.to_s =~ /^(\w+)(=)?$/ or return super
  name, setter = [$1.to_sym, $2]
  return(super) unless param_definitions.include?(name)
  if setter && (args.size == 1)
    self[name] = args.first
  elsif (!setter) && args.empty?
    self[name]
  else super ; end
end

Instance Attribute Details

#param_definitionsObject



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

def param_definitions
  # initialize the param_definitions as an auto-vivifying hash if it's never been set
  @param_definitions ||= Sash.new{|hsh, key| hsh[key] = Sash.new  }
end

Instance Method Details

#define(param, definitions = {}, &block) ⇒ Object

Examples:

Settings.define :dest_time, :type => Date, :description => 'Arrival time. If only a date is given, the current time of day on that date is assumed.'
Settings.define 'delorean.power_source', :description => 'Delorean subsytem supplying power to the Flux Capacitor.'
Settings.define :password, :required => true, :obscure => true

Parameters:

  • param

    the setting to describe. Either a simple symbol or a dotted param string.

  • definitions (defaults to: {})

    the defineables to set (:description, :type, :encrypted, etc.)



14
15
16
17
18
19
20
21
22
23
# File 'lib/configliere/define.rb', line 14

def define param, definitions={}, &block
  self.param_definitions[param].merge! definitions
  self.use(:env_var)      if definitions.include?(:env_var)
  self.use(:encrypted)    if definitions.include?(:encrypted)
  self.use(:config_block) if definitions.include?(:finally)
  self[param] = definitions[:default] if definitions.include?(:default)
  self.env_vars param => definitions[:env_var] if definitions.include?(:env_var)
  self.finally(&definitions[:finally]) if definitions.include?(:finally)
  self.finally(&block) if block
end

#definitions_for(defineable) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/configliere/define.rb', line 156

def definitions_for defineable
  hsh = {}
  param_definitions.each do |param, defs|
    hsh[param] = defs[defineable] if defs[defineable]
  end
  hsh
end

#described_paramsObject

List of params that have descriptions



62
63
64
# File 'lib/configliere/define.rb', line 62

def described_params
  params_with(:description)
end

#description_for(param) ⇒ Object

gets the description (if any) for the param

Parameters:

  • param

    the setting to describe. Either a simple symbol or a dotted param string.



52
53
54
# File 'lib/configliere/define.rb', line 52

def description_for param
  param_definitions[param][:description]
end

#descriptionsObject

All described params with their descriptions



57
58
59
# File 'lib/configliere/define.rb', line 57

def descriptions
  definitions_for(:description).reject{ |param, desc| param_definitions[param][:no_help] }
end

#params_with(defineable) ⇒ Object

all params with a value for the definable aspect

Parameters:

  • definable

    the aspect to list (:description, :type, :encrypted, etc.)



145
146
147
# File 'lib/configliere/define.rb', line 145

def params_with defineable
  param_definitions.keys.find_all{|param| param_definitions[param][defineable] } || []
end

#params_without(defineable) ⇒ Object

all params without a value for the definable aspect

Parameters:

  • definable

    the aspect to reject (:description, :type, :encrypted, etc.)



152
153
154
# File 'lib/configliere/define.rb', line 152

def params_without defineable
  param_definitions.keys.reject{|param| param_definitions[param].include?(defineable) } || []
end

#required_paramsArray

List of params that are required

Returns:

  • (Array)

    list of required params



129
130
131
# File 'lib/configliere/define.rb', line 129

def required_params
  params_with(:required)
end

#resolve!Object

performs type coercion



31
32
33
34
35
# File 'lib/configliere/define.rb', line 31

def resolve!
  resolve_types!
  super()
  self
end

#resolve_types!Object

Coerce all params with types defined to their proper form



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
# File 'lib/configliere/define.rb', line 92

def resolve_types!
  typed_params.each do |param, type|
    val = self[param]
    case
    when val.nil?            then val = nil
    when (type == :boolean)  then
      if ['false', false, 0, '0', ''].include?(val) then val = false else val = true end
    when ((type == Array) && val.is_a?(String))
      val = val.split(",")   rescue nil
      # following types map blank to nil
    when (val.blank?)        then val = nil
    when (type == :filename) then val = File.expand_path(val)
    when (type == Float)     then val = val.to_f
    when (type == Integer)   then val = val.to_i
    when (type == Symbol)    then val = val.to_s.to_sym     rescue nil
    when ((val.to_s == 'now') && (type == Date))     then val = Date.today
    when ((val.to_s == 'now') && (type == DateTime)) then val = DateTime.now
    when (type == Date)     then val = Date.parse(val)     rescue nil
    when (type == DateTime) then val = DateTime.parse(val) rescue nil
    when (type == Regexp)   then val = Regexp.new(val)     rescue nil
    else # nothing
    end
    self[param] = val
  end
end

#type_for(param) ⇒ Object

Type coercion

Define types with

Settings.define :param, :type => Date


75
76
77
# File 'lib/configliere/define.rb', line 75

def type_for param
  param_definitions[param][:type]
end

#typed_param_namesObject

List of params that have descriptions



85
86
87
# File 'lib/configliere/define.rb', line 85

def typed_param_names
  params_with(:type)
end

#typed_paramsObject

All typed params with their descriptions



80
81
82
# File 'lib/configliere/define.rb', line 80

def typed_params
  definitions_for(:type)
end

#validate!Object



37
38
39
40
41
# File 'lib/configliere/define.rb', line 37

def validate!
  validate_requireds!
  super()
  true
end

#validate_requireds!Object

Check that all required params are present.



134
135
136
137
138
139
140
# File 'lib/configliere/define.rb', line 134

def validate_requireds!
  missing = []
  required_params.each do |param|
    missing << param if self[param].nil?
  end
  raise "Missing values for:\n #{missing.map{|s| "  --" + s.to_s + (description_for(s) ? (" (" + description_for(s).to_s + ") ") : '') }.sort.join("\n")}" if (! missing.empty?)
end