class Irc::Bot::Config::Value

Attributes

auth_path[R]
desc[R]
key[R]
manager[R]
order[R]
requires_rescan[R]
requires_restart[R]
type[R]
wizard[R]

Public Class Methods

new(key, params) click to toggle source
# 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 =~ %r^.+\..+$/
    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

Public Instance Methods

default() click to toggle source
# File lib/rbot/config.rb, line 59
def default
  if @default.instance_of?(Proc)
    @default.call
  else
    @default
  end
end
get() click to toggle source
# File lib/rbot/config.rb, line 66
def get
  return @manager.config[@key] if @manager.config.has_key?(@key)
  return default
end
Also aliased as: value
parse(string) click to toggle source

override this. the default will work for strings only

# File lib/rbot/config.rb, line 95
def parse(string)
  string
end
set(value, on_change = true) click to toggle source
# 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) click to toggle source

set string will raise ArgumentErrors on failed parse/validate

# 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_s() click to toggle source
# File lib/rbot/config.rb, line 99
def to_s
  get.to_s
end
unset() click to toggle source
# 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
value() click to toggle source
Alias for: get

Protected Instance Methods

validate(val, validator = @validate) click to toggle source
# File lib/rbot/config.rb, line 104
def validate(val, validator  = @validate)
  case validator
  when false, nil
    return true
  when Proc
    return validator.call(val)
  when Regexp
    raise ArgumentError, "validation via Regexp only supported for strings!" unless String === val
    return validator.match(val)
  else
    raise ArgumentError, "validation type #{validator.class} not supported"
  end
end