Class: Racket::Settings::Base
- Inherits:
-
Object
- Object
- Racket::Settings::Base
- Defined in:
- lib/racket/settings/base.rb
Overview
Base class for settings.
Direct Known Subclasses
Class Method Summary collapse
-
.default_value(symbol) ⇒ Object
Returns a default value for a key.
-
.setting(symbol) ⇒ nil
Creates a setting with a default value.
Instance Method Summary collapse
-
#delete(key) ⇒ nil
Deletes a custom setting associated with the application.
-
#fetch(key, default = nil) ⇒ Object
Returns a settings value associated with the application.
-
#initialize(defaults = {}) ⇒ Base
constructor
A new instance of Base.
-
#present?(key) ⇒ true|false
Returns whether
key
is present among the settings. -
#store(key, value) ⇒ nil
Sets/updates a custom setting in the application.
Constructor Details
#initialize(defaults = {}) ⇒ Base
Returns a new instance of Base.
24 25 26 27 28 29 30 31 32 |
# File 'lib/racket/settings/base.rb', line 24 def initialize(defaults = {}) @custom = {} defaults.each_pair do |key, value| meth = "#{key}=".to_sym if respond_to?(meth) then send(meth, value) else @custom[key] = value end end end |
Class Method Details
.default_value(symbol) ⇒ Object
Returns a default value for a key. Default values are stored in @defaults, which is a Racket::Registry object.
81 82 83 84 |
# File 'lib/racket/settings/base.rb', line 81 def self.default_value(symbol) return nil unless defined?(@defaults) && @defaults.respond_to?(symbol) @defaults.send(symbol) end |
.setting(symbol) ⇒ nil
Creates a setting with a default value.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/racket/settings/base.rb', line 90 def self.setting(symbol) klass = self ivar = "@#{symbol}".to_sym define_method symbol do instance_variable_set(ivar, klass.default_value(symbol)) unless instance_variables.include?(ivar) instance_variable_get(ivar) end attr_writer(symbol) && nil end |
Instance Method Details
#delete(key) ⇒ nil
Deletes a custom setting associated with the application.
38 39 40 41 42 |
# File 'lib/racket/settings/base.rb', line 38 def delete(key) raise ArgumentErrpr, "Cannot delete standard setting #{key}" if respond_to?(key.to_sym) @custom.delete(key) && nil end |
#fetch(key, default = nil) ⇒ Object
Returns a settings value associated with the application. Both standard and custom settings are searched. If the key cannot be found, a default value is returned.
50 51 52 53 54 |
# File 'lib/racket/settings/base.rb', line 50 def fetch(key, default = nil) meth = key.to_sym return send(meth) if respond_to?(meth) @custom.fetch(key, default) end |
#present?(key) ⇒ true|false
Returns whether key
is present among the settings.
60 61 62 63 |
# File 'lib/racket/settings/base.rb', line 60 def present?(key) meth = key.to_sym respond_to?(meth) || @custom.key?(key) end |
#store(key, value) ⇒ nil
Sets/updates a custom setting in the application.
70 71 72 73 74 |
# File 'lib/racket/settings/base.rb', line 70 def store(key, value) raise ArgumentError, "Cannot overwrite standard setting #{key}" if respond_to?("#{key}=".to_sym) (@custom[key] = value) && nil end |