Class: Irc::Bot::Config::Value
Direct Known Subclasses
ArrayValue, BooleanValue, EnumValue, FloatValue, IntegerValue, StringValue
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
-
#auth_path ⇒ Object
readonly
Returns the value of attribute auth_path.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#manager ⇒ Object
readonly
Returns the value of attribute manager.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#requires_rescan ⇒ Object
readonly
Returns the value of attribute requires_rescan.
-
#requires_restart ⇒ Object
readonly
Returns the value of attribute requires_restart.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#wizard ⇒ Object
readonly
Returns the value of attribute wizard.
Instance Method Summary collapse
- #default ⇒ Object
- #get ⇒ Object (also: #value)
-
#initialize(key, params) ⇒ Value
constructor
A new instance of Value.
-
#parse(string) ⇒ Object
override this.
- #set(value, on_change = true) ⇒ Object
-
#set_string(string, on_change = true) ⇒ Object
set string will raise ArgumentErrors on failed parse/validate.
- #to_s ⇒ Object
- #unset ⇒ Object
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_path ⇒ Object (readonly)
Returns the value of attribute auth_path.
31 32 33 |
# File 'lib/rbot/config.rb', line 31 def auth_path @auth_path end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
24 25 26 |
# File 'lib/rbot/config.rb', line 24 def desc @desc end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
25 26 27 |
# File 'lib/rbot/config.rb', line 25 def key @key end |
#manager ⇒ Object (readonly)
Returns the value of attribute manager.
30 31 32 |
# File 'lib/rbot/config.rb', line 30 def manager @manager end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
29 30 31 |
# File 'lib/rbot/config.rb', line 29 def order @order end |
#requires_rescan ⇒ Object (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_restart ⇒ Object (readonly)
Returns the value of attribute requires_restart.
27 28 29 |
# File 'lib/rbot/config.rb', line 27 def requires_restart @requires_restart end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
23 24 25 |
# File 'lib/rbot/config.rb', line 23 def type @type end |
#wizard ⇒ Object (readonly)
Returns the value of attribute wizard.
26 27 28 |
# File 'lib/rbot/config.rb', line 26 def wizard @wizard end |
Instance Method Details
#default ⇒ Object
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 |
#get ⇒ Object 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_s ⇒ Object
99 100 101 |
# File 'lib/rbot/config.rb', line 99 def to_s get.to_s end |
#unset ⇒ Object
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 |