Class: Irc::Bot::Config::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/rbot/config.rb

Constant Summary collapse

@@order =

allow the definition order to be preserved so that sorting by definition order is possible. The Wizard does this to allow the :wizard questions to be in a sensible order.

0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, params) ⇒ Value

Returns a new instance of Value.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rbot/config.rb', line 32

def initialize(key, params)
  @manager = Config.manager
  # Keys must be in the form 'module.name'.
  # They will be internally passed around as symbols,
  # but we accept them both in string and symbol form.
  unless key.to_s =~ /^.+\..+$/
    raise ArgumentError,"key must be of the form 'module.name'"
  end
  @order = @@order
  @@order += 1
  @key = key.to_sym
  if @manager.overrides.key?(@key)
    @default = @manager.overrides[@key]
  elsif params.has_key? :default
    @default = params[:default]
  else
    @default = false
  end
  @desc = params[:desc]
  @type = params[:type] || String
  @on_change = params[:on_change]
  @validate = params[:validate]
  @wizard = params[:wizard]
  @requires_restart = params[:requires_restart]
  @requires_rescan = params[:requires_rescan]
  @auth_path = "config::key::#{key.sub('.','::')}"
end

Instance Attribute Details

#auth_pathObject (readonly)

Returns the value of attribute auth_path.



31
32
33
# File 'lib/rbot/config.rb', line 31

def auth_path
  @auth_path
end

#descObject (readonly)

Returns the value of attribute desc.



24
25
26
# File 'lib/rbot/config.rb', line 24

def desc
  @desc
end

#keyObject (readonly)

Returns the value of attribute key.



25
26
27
# File 'lib/rbot/config.rb', line 25

def key
  @key
end

#managerObject (readonly)

Returns the value of attribute manager.



30
31
32
# File 'lib/rbot/config.rb', line 30

def manager
  @manager
end

#orderObject (readonly)

Returns the value of attribute order.



29
30
31
# File 'lib/rbot/config.rb', line 29

def order
  @order
end

#requires_rescanObject (readonly)

Returns the value of attribute requires_rescan.



28
29
30
# File 'lib/rbot/config.rb', line 28

def requires_rescan
  @requires_rescan
end

#requires_restartObject (readonly)

Returns the value of attribute requires_restart.



27
28
29
# File 'lib/rbot/config.rb', line 27

def requires_restart
  @requires_restart
end

#typeObject (readonly)

Returns the value of attribute type.



23
24
25
# File 'lib/rbot/config.rb', line 23

def type
  @type
end

#wizardObject (readonly)

Returns the value of attribute wizard.



26
27
28
# File 'lib/rbot/config.rb', line 26

def wizard
  @wizard
end

Instance Method Details

#defaultObject



59
60
61
62
63
64
65
# File 'lib/rbot/config.rb', line 59

def default
  if @default.instance_of?(Proc)
    @default.call
  else
    @default
  end
end

#getObject Also known as: value



66
67
68
69
# File 'lib/rbot/config.rb', line 66

def get
  return @manager.config[@key] if @manager.config.has_key?(@key)
  return default
end

#parse(string) ⇒ Object

override this. the default will work for strings only



95
96
97
# File 'lib/rbot/config.rb', line 95

def parse(string)
  string
end

#set(value, on_change = true) ⇒ Object



71
72
73
74
75
76
# File 'lib/rbot/config.rb', line 71

def set(value, on_change = true)
  @manager.config[@key] = value
  @manager.changed = true
  @on_change.call(@manager.bot, value) if on_change && @on_change
  return self
end

#set_string(string, on_change = true) ⇒ Object

set string will raise ArgumentErrors on failed parse/validate



85
86
87
88
89
90
91
92
# File 'lib/rbot/config.rb', line 85

def set_string(string, on_change = true)
  value = parse string
  if validate value
    set value, on_change
  else
    raise ArgumentError, "invalid value: #{string}"
  end
end

#to_sObject



99
100
101
# File 'lib/rbot/config.rb', line 99

def to_s
  get.to_s
end

#unsetObject



77
78
79
80
81
82
# File 'lib/rbot/config.rb', line 77

def unset
  @manager.config.delete(@key)
  @manager.changed = true
  @on_change.call(@manager.bot, value) if @on_change
  return self
end