Class: Vapir::Configuration
- Inherits:
-
Object
- Object
- Vapir::Configuration
- Defined in:
- lib/vapir-common/config.rb
Overview
represents a entry in a heirarchy of configuration options
Direct Known Subclasses
Defined Under Namespace
Classes: BadKeyError, Error, NoValueError, Option
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
the parent Configuration in the heirarchy.
Instance Method Summary collapse
-
#[](key) ⇒ Object
alias for #read.
-
#[]=(key, value) ⇒ Object
alias for #update.
-
#create(key, options = {}) ⇒ Object
creates a new key.
-
#create_update(key, value, options = {}) ⇒ Object
creates a new key and updates it with the given value.
-
#defined_key?(key) ⇒ Boolean
returns true if the given key is defined on this Configuration or any of its ancestors.
-
#delete(key) ⇒ Object
deletes the given value from the hash.
-
#initialize(parent) {|_self| ... } ⇒ Configuration
constructor
creates a new Configuration with the given parent.
-
#locally_defined_key?(key) ⇒ Boolean
returns true if the given key is defined on this Configuration; returns false if not - note that this returns false if the given key is defined on an ancestor Configuration.
-
#method_missing(method, *args) ⇒ Object
if the method invoked looks like assignment (ends with an =), calls to #update with the given method as the key and its argument as the value.
-
#read(key) ⇒ Object
reads the value for the given key.
-
#recognize_key!(key) ⇒ Object
assert that the given key must be recognized; if it is not recognized, an error should be raised.
-
#recognized_key?(key) ⇒ Boolean
returns true if the given key is recognized; false otherwise.
-
#recognized_keys ⇒ Object
returns an array of.
-
#update(key, value) ⇒ Object
updates the given key with the given value.
-
#update_hash(hash) ⇒ Object
takes a hash of key/value pairs and calls #update on each pair.
-
#validate_key_format!(key) ⇒ Object
raises an error if the given key is not in an acceptable format.
-
#with_config(hash, &block) ⇒ Object
temporarily set the given keys of this configuration to the given values, yield to the block, and restore to the original configuration before returning.
Constructor Details
#initialize(parent) {|_self| ... } ⇒ Configuration
creates a new Configuration with the given parent. if a block is given, this Configuration object will be yielded to it.
62 63 64 65 66 67 68 69 70 |
# File 'lib/vapir-common/config.rb', line 62 def initialize(parent, &block) unless parent==nil || parent.is_a?(Configuration) raise TypeError, "expected parent to be a Configuration; got #{parent.inspect}" end @parent=parent @config_hash = {} @recognized_options = {} yield(self) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
if the method invoked looks like assignment (ends with an =), calls to #update with the given method as the key and its argument as the value. otherwise calls #read with the method as the key.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vapir-common/config.rb', line 74 def method_missing(method, *args) method=method.to_s if method =~ /\A([a-z_][a-z0-9_]*)([=?!])?\z/i method = $1 special = $2 else # don't deal with any special character crap return super end case special when nil raise ArgumentError, "wrong number of arguments retrieving #{method} (#{args.size} for 0)" unless args.size==0 read(method) when '=' raise ArgumentError, "wrong number of arguments setting #{method} (#{args.size} for 1)" unless args.size==1 update(method, *args) #when '?' # no defined behavior for ? or ! at the moment #when '!' else return super end end |
Instance Attribute Details
#parent ⇒ Object (readonly)
the parent Configuration in the heirarchy. may be nil if there is no parent.
59 60 61 |
# File 'lib/vapir-common/config.rb', line 59 def parent @parent end |
Instance Method Details
#[](key) ⇒ Object
alias for #read
96 97 98 |
# File 'lib/vapir-common/config.rb', line 96 def [](key) read(key) end |
#[]=(key, value) ⇒ Object
alias for #update
100 101 102 |
# File 'lib/vapir-common/config.rb', line 100 def []=(key, value) update(key, value) end |
#create(key, options = {}) ⇒ Object
creates a new key. options are passed to Option.new; see its documentation.
152 153 154 155 156 157 158 |
# File 'lib/vapir-common/config.rb', line 152 def create(key, ={}) key=validate_key_format!(key) if recognized_key?(key) raise "already created key #{key}" end @recognized_options[key]= Option.new(key, ) end |
#create_update(key, value, options = {}) ⇒ Object
creates a new key and updates it with the given value. options are passed to Option.new.
177 178 179 180 |
# File 'lib/vapir-common/config.rb', line 177 def create_update(key, value, ={}) create(key, ) update(key, value) end |
#defined_key?(key) ⇒ Boolean
returns true if the given key is defined on this Configuration or any of its ancestors.
129 130 131 |
# File 'lib/vapir-common/config.rb', line 129 def defined_key?(key) locally_defined_key?(key) || (parent && parent.defined_key?(key)) end |
#delete(key) ⇒ Object
deletes the given value from the hash. this does not affect any ancestor Configurations.
188 189 190 191 |
# File 'lib/vapir-common/config.rb', line 188 def delete(key) key = check_key key @config_hash.delete(key) end |
#locally_defined_key?(key) ⇒ Boolean
returns true if the given key is defined on this Configuration; returns false if not - note that this returns false if the given key is defined on an ancestor Configuration.
124 125 126 127 |
# File 'lib/vapir-common/config.rb', line 124 def locally_defined_key?(key) key = validate_key_format!(key) @config_hash.key?(key) end |
#read(key) ⇒ Object
reads the value for the given key. if on value is defined, raises NoValueError.
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/vapir-common/config.rb', line 160 def read(key) key = recognize_key! key if @config_hash.key?(key) @config_hash[key] elsif @parent @parent.read(key) else raise NoValueError, "There is no value defined for key #{key}" end end |
#recognize_key!(key) ⇒ Object
assert that the given key must be recognized; if it is not recognized, an error should be raised.
115 116 117 118 119 120 121 |
# File 'lib/vapir-common/config.rb', line 115 def recognize_key!(key) key = validate_key_format!(key) unless recognized_key?(key) raise BadKeyError, "Unrecognized key: #{key}" end key end |
#recognized_key?(key) ⇒ Boolean
returns true if the given key is recognized; false otherwise. may raise BadKeyError if the given key isn’t even a valid format for a key.
109 110 111 112 |
# File 'lib/vapir-common/config.rb', line 109 def recognized_key?(key) key = validate_key_format!(key) recognized_keys.include?(key) end |
#recognized_keys ⇒ Object
returns an array of
104 105 106 |
# File 'lib/vapir-common/config.rb', line 104 def recognized_keys ((@parent ? @parent.recognized_keys : [])+@recognized_options.keys).uniq end |
#update(key, value) ⇒ Object
updates the given key with the given value.
171 172 173 174 175 |
# File 'lib/vapir-common/config.rb', line 171 def update(key, value) key = recognize_key! key value = [key].validate! value @config_hash[key]=value end |
#update_hash(hash) ⇒ Object
takes a hash of key/value pairs and calls #update on each pair.
182 183 184 185 186 |
# File 'lib/vapir-common/config.rb', line 182 def update_hash(hash) hash.each do |k,v| update(k,v) end end |
#validate_key_format!(key) ⇒ Object
raises an error if the given key is not in an acceptable format. the key should be a string or symbol consisting of alphanumerics and underscorse, beginning with an alpha or underscore.
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/vapir-common/config.rb', line 134 def validate_key_format!(key) unless key.is_a?(String) || key.is_a?(Symbol) raise BadKeyError, "key should be a String or Symbol; got #{key.inspect} (#{key.class})" end key=key.to_s.downcase unless key =~ /\A([a-z_][a-z0-9_]*)\z/ raise BadKeyError, "key should be all alphanumeric/underscores, not starting with a number" end key end |
#with_config(hash, &block) ⇒ Object
temporarily set the given keys of this configuration to the given values, yield to the block, and restore to the original configuration before returning.
195 196 197 198 199 200 201 202 203 |
# File 'lib/vapir-common/config.rb', line 195 def with_config(hash, &block) begin orig_config_hash = @config_hash.dup update_hash hash return yield ensure @config_hash = orig_config_hash end end |