Class: Configatron::Store
Instance Method Summary collapse
-
#==(other) ⇒ Object
:nodoc:.
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #blank? ⇒ Boolean
- #configatron_keys ⇒ Object
-
#configure_from_hash(options) ⇒ Object
Allows for the configuration of the system via a Hash.
-
#configure_from_yaml(path, opts = {}) ⇒ Object
Allows for the configuration of the system from a YAML file.
-
#deep_clone(obj = self, cloned = {}) ⇒ Object
DeepClone.
-
#exists?(name) ⇒ Boolean
Checks whether or not a parameter exists.
- #heirarchy ⇒ Object
-
#initialize(options = {}, name = nil, parent = nil) ⇒ Store
constructor
Takes an optional Hash of parameters.
- #inspect ⇒ Object
-
#lock(name) ⇒ Object
Prevents a namespace from having new parameters set.
-
#method_missing(sym, *args) ⇒ Object
:nodoc:.
-
#nil? ⇒ Boolean
Returns true if there are no configuration parameters.
-
#protect(name) ⇒ Object
Prevents a parameter from being reassigned.
-
#protect_all! ⇒ Object
Prevents all parameters from being reassigned.
-
#remove(name) ⇒ Object
Removes a parameter.
-
#respond_to?(name) ⇒ Boolean
respond_to to respond_to.
-
#retrieve(name, default_value = nil) ⇒ Object
Retrieves a certain parameter and if that parameter doesn’t exist it will return the default_value specified.
-
#set_default(name, default_value) ⇒ Object
Sets a ‘default’ value.
-
#to_hash ⇒ Object
Returns a Hash representing the configurations.
- #unlock(name) ⇒ Object
-
#unprotect(name) ⇒ Object
Removes the protection of a parameter.
- #unprotect_all! ⇒ Object
Constructor Details
#initialize(options = {}, name = nil, parent = nil) ⇒ Store
Takes an optional Hash of parameters
11 12 13 14 15 16 17 18 |
# File 'lib/configatron/store.rb', line 11 def initialize( = {}, name = nil, parent = nil) @_name = name @_parent = parent @_store = {} configure_from_hash() @_protected = [] @_locked = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
:nodoc:
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/configatron/store.rb', line 152 def method_missing(sym, *args) # :nodoc: if sym.to_s.match(/(.+)=$/) name = sym.to_s.gsub("=", '').to_sym raise Configatron::ProtectedParameter.new(name) if @_protected.include?(name) || methods_include?(name) raise Configatron::LockedNamespace.new(@_name) if @_locked && !@_store.has_key?(name) @_store[name] = (*args) elsif sym.to_s.match(/(.+)\?/) return !@_store[$1.to_sym].blank? elsif block_given? yield self.send(sym) elsif @_store.has_key?(sym) val = @_store[sym] if val.is_a?(Configatron::Proc) res = val.execute if val.finalize? @_store[sym] = res end return res end return val else store = Configatron::Store.new({}, sym, self) @_store[sym] = store return store end end |
Instance Method Details
#==(other) ⇒ Object
:nodoc:
179 180 181 |
# File 'lib/configatron/store.rb', line 179 def ==(other) # :nodoc: self.to_hash == other end |
#[](key) ⇒ Object
20 21 22 |
# File 'lib/configatron/store.rb', line 20 def [](key) method_missing(key.to_sym) end |
#[]=(key, value) ⇒ Object
24 25 26 |
# File 'lib/configatron/store.rb', line 24 def []=(key, value) method_missing(:"#{key}=", value) end |
#blank? ⇒ Boolean
125 126 127 128 |
# File 'lib/configatron/store.rb', line 125 def blank? value = retrieve(@_name) value.respond_to?(:empty?) ? value.empty? : !value end |
#configatron_keys ⇒ Object
50 51 52 |
# File 'lib/configatron/store.rb', line 50 def configatron_keys return @_store.keys.collect{|k| k.to_s}.sort end |
#configure_from_hash(options) ⇒ Object
Allows for the configuration of the system via a Hash
101 102 103 |
# File 'lib/configatron/store.rb', line 101 def configure_from_hash() () end |
#configure_from_yaml(path, opts = {}) ⇒ Object
Allows for the configuration of the system from a YAML file. Takes the path to the YAML file. Also takes an optional parameter, :hash, that indicates a specific hash that should be loaded from the file.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/configatron/store.rb', line 109 def configure_from_yaml(path, opts = {}) Configatron.log.warn "DEPRECATED! (configure_from_yaml) Please stop using YAML and use Ruby instead. This method will be removed in 3.1." begin yml = ::Yamler.load(path) yml = yml[opts[:hash]] unless opts[:hash].nil? configure_from_hash(yml) rescue Errno::ENOENT => e puts e. end end |
#deep_clone(obj = self, cloned = {}) ⇒ Object
DeepClone
Version
1.2006.05.23 (change of the first number means Big Change)
Description
Adds deep_clone method to an object which produces deep copy of it. It means
if you clone a Hash, every nested items and their nested items will be cloned.
Moreover deep_clone checks if the object is already cloned to prevent endless recursion.
Usage
(see examples directory under the ruby gems root directory)
require 'rubygems'
require 'deep_clone'
include DeepClone
obj = []
a = [ true, false, obj ]
b = a.deep_clone
obj.push( 'foo' )
p obj # >> [ 'foo' ]
p b[2] # >> []
Source
simplypowerful.1984.cz/goodlibs/1.2006.05.23
Author
jan molic (/mig/at_sign/1984/dot/cz/)
Licence
You can redistribute it and/or modify it under the same terms of Ruby's license;
either the dual license version in 2003, or any later version.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/configatron/store.rb', line 262 def deep_clone( obj=self, cloned={} ) if cloned.has_key?( obj.object_id ) return cloned[obj.object_id] else begin cl = obj.clone rescue Exception # unclonnable (TrueClass, Fixnum, ...) cloned[obj.object_id] = obj return obj else cloned[obj.object_id] = cl cloned[cl.object_id] = cl if cl.is_a?( Hash ) cl.clone.each { |k,v| cl[k] = deep_clone( v, cloned ) } elsif cl.is_a?( Array ) cl.collect! { |v| deep_clone( v, cloned ) } end cl.instance_variables.each do |var| v = cl.instance_eval( var.to_s ) v_cl = deep_clone( v, cloned ) cl.instance_eval( "#{var} = v_cl" ) end return cl end end end |
#exists?(name) ⇒ Boolean
Checks whether or not a parameter exists
Examples:
configatron.i.am.alive = 'alive!'
configatron.i.am.exists?(:alive) # => true
configatron.i.am.exists?(:dead) # => false
60 61 62 |
# File 'lib/configatron/store.rb', line 60 def exists?(name) @_store.has_key?(name.to_sym) || @_store.has_key?(name.to_s) end |
#heirarchy ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/configatron/store.rb', line 38 def heirarchy path = [@_name] parent = @_parent until parent.nil? path << parent.instance_variable_get('@_name') parent = parent.instance_variable_get('@_parent') end path.compact! path.reverse! path.join('.') end |
#inspect ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/configatron/store.rb', line 69 def inspect path = [@_name] parent = @_parent until parent.nil? path << parent.instance_variable_get('@_name') parent = parent.instance_variable_get('@_parent') end path << 'configatron' path.compact! path.reverse! f_out = [] @_store.each do |k, v| if v.is_a?(Configatron::Store) v.inspect.each_line do |line| if line.match(/\n/) line.each_line do |l| l.strip! f_out << l end else line.strip! f_out << line end end else f_out << "#{path.join('.')}.#{k} = #{v.inspect}" end end f_out.compact.sort.join("\n") end |
#lock(name) ⇒ Object
Prevents a namespace from having new parameters set. The lock is applied recursively to any namespaces below it.
214 215 216 217 218 |
# File 'lib/configatron/store.rb', line 214 def lock(name) namespace = @_store[name.to_sym] raise ArgumentError, "Namespace #{name.inspect} does not exist" if namespace.nil? namespace.lock! end |
#nil? ⇒ Boolean
Returns true if there are no configuration parameters
121 122 123 |
# File 'lib/configatron/store.rb', line 121 def nil? return @_store.empty? end |
#protect(name) ⇒ Object
Prevents a parameter from being reassigned. If called on a ‘namespace’ then all parameters below it will be protected as well.
185 186 187 |
# File 'lib/configatron/store.rb', line 185 def protect(name) @_protected << name.to_sym end |
#protect_all! ⇒ Object
Prevents all parameters from being reassigned.
190 191 192 193 194 195 196 197 |
# File 'lib/configatron/store.rb', line 190 def protect_all! @_protected.clear @_store.keys.each do |k| val = self.send(k) val.protect_all! if val.class == Configatron::Store @_protected << k end end |
#remove(name) ⇒ Object
Removes a parameter. In the case of a nested parameter it will remove all below it.
139 140 141 |
# File 'lib/configatron/store.rb', line 139 def remove(name) @_store.delete(name.to_sym) end |
#respond_to?(name) ⇒ Boolean
respond_to to respond_to
65 66 67 |
# File 'lib/configatron/store.rb', line 65 def respond_to?(name) exists?(name) || super end |
#retrieve(name, default_value = nil) ⇒ Object
Retrieves a certain parameter and if that parameter doesn’t exist it will return the default_value specified.
132 133 134 135 |
# File 'lib/configatron/store.rb', line 132 def retrieve(name, default_value = nil) val = method_missing(name.to_sym) return val.is_a?(Configatron::Store) ? default_value : val end |
#set_default(name, default_value) ⇒ Object
Sets a ‘default’ value. If there is already a value specified it won’t set the value.
145 146 147 148 149 150 |
# File 'lib/configatron/store.rb', line 145 def set_default(name, default_value) unless @_store[name.to_sym] # @_store[name.to_sym] = parse_options(default_value) self.send("#{name}=", default_value) end end |
#to_hash ⇒ Object
Returns a Hash representing the configurations
29 30 31 32 33 34 35 36 |
# File 'lib/configatron/store.rb', line 29 def to_hash h = Hash.new @_store.each { |k,v| # Descend the tree and hashify each node h[k] = v.is_a?(Store) ? v.to_hash : v } h end |
#unlock(name) ⇒ Object
220 221 222 223 224 |
# File 'lib/configatron/store.rb', line 220 def unlock(name) namespace = @_store[name.to_sym] raise ArgumentError, "Namespace #{name.inspect} does not exist" if namespace.nil? namespace.unlock! end |
#unprotect(name) ⇒ Object
Removes the protection of a parameter.
200 201 202 |
# File 'lib/configatron/store.rb', line 200 def unprotect(name) @_protected.reject! { |e| e == name.to_sym } end |
#unprotect_all! ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/configatron/store.rb', line 204 def unprotect_all! @_protected.clear @_store.keys.each do |k| val = self.send(k) val.unprotect_all! if val.class == Configatron::Store end end |