Skip to content

Commit

Permalink
added respond_to? on configatron object
Browse files Browse the repository at this point in the history
  • Loading branch information
chatgris committed May 20, 2011
1 parent 4d21767 commit c69f87b
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 104 deletions.
25 changes: 15 additions & 10 deletions lib/configatron/configatron.rb
Expand Up @@ -2,24 +2,29 @@

class Configatron
include Singleton

alias_method :send!, :send

def initialize # :nodoc:
@_namespace = [:default]
reset!
end

# Forwards the method call onto the 'namespaced' Configatron::Store
def method_missing(sym, *args)
@_store[@_namespace.last].send(sym, *args)
end


# respond_to to respond_to
def respond_to?(method)
!@_store[@_namespace.last].send(method).nil? || super
end

# Removes ALL configuration parameters
def reset!
@_store = {:default => Configatron::Store.new}
end

# Allows for the temporary overriding of parameters in a block.
# Takes an optional Hash of parameters that will be applied before
# the block gets called. At the end of the block, the temporary
Expand All @@ -34,7 +39,7 @@ def temp(options = nil)
temp_end
end
end

def temp_start(options = nil)
n_space = rand
@_store[n_space] = @_store[@_namespace.last].deep_clone
Expand All @@ -43,17 +48,17 @@ def temp_start(options = nil)
self.method_missing(:configure_from_hash, options)
end
end

def temp_end
@_store.delete(@_namespace.pop)
end

begin
undef :inspect # :nodoc:
undef :nil? # :nodoc:
undef :test # :nodoc:
rescue Exception => e
end

end

end
49 changes: 27 additions & 22 deletions lib/configatron/store.rb
Expand Up @@ -6,7 +6,7 @@ class Store
end

alias_method :send!, :send

# Takes an optional Hash of parameters
def initialize(options = {}, name = nil, parent = nil)
@_name = name
Expand All @@ -16,17 +16,17 @@ def initialize(options = {}, name = nil, parent = nil)
@_protected = []
@_locked = false
end

# Returns a Hash representing the configurations
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
h
end

def heirarchy
path = [@_name]
parent = @_parent
Expand All @@ -38,21 +38,26 @@ def heirarchy
path.reverse!
path.join('.')
end

def configatron_keys
return @_store.keys.collect{|k| k.to_s}.sort
end

# 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
def exists?(name)
@_store.has_key?(name.to_sym) || @_store.has_key?(name.to_s)
end


# respond_to to respond_to
def respond_to?(name)
exists?(name) || super
end

def inspect
path = [@_name]
parent = @_parent
Expand Down Expand Up @@ -92,7 +97,7 @@ def configure_from_hash(options)
# Allows for the configuration of the system from a YAML file.
# Takes the path to the YAML file. Also takes an optional parameter,
# <tt>:hash</tt>, that indicates a specific hash that should be
# loaded from the file.
# loaded from the file.
def configure_from_yaml(path, opts = {})
begin
yml = ::Yamler.load(path)
Expand All @@ -119,7 +124,7 @@ def retrieve(name, default_value = nil)
val = method_missing(name.to_sym)
return val.is_a?(Configatron::Store) ? default_value : val
end

# Removes a parameter. In the case of a nested parameter
# it will remove all below it.
def remove(name)
Expand All @@ -134,7 +139,7 @@ def set_default(name, default_value)
self.send("#{name}=", default_value)
end
end

def method_missing(sym, *args) # :nodoc:
if sym.to_s.match(/(.+)=$/)
name = sym.to_s.gsub("=", '').to_sym
Expand All @@ -159,11 +164,11 @@ def method_missing(sym, *args) # :nodoc:
return store
end
end

def ==(other) # :nodoc:
self.to_hash == other
end

# Prevents a parameter from being reassigned. If called on a 'namespace' then
# all parameters below it will be protected as well.
def protect(name)
Expand All @@ -179,12 +184,12 @@ def protect_all!
@_protected << k
end
end

# Removes the protection of a parameter.
def unprotect(name)
@_protected.reject! { |e| e == name.to_sym }
end

def unprotect_all!
@_protected.clear
@_store.keys.each do |k|
Expand All @@ -206,7 +211,7 @@ def unlock(name)
raise ArgumentError, "Namespace #{name.inspect} does not exist" if namespace.nil?
namespace.unlock!
end

# = DeepClone
#
# == Version
Expand Down Expand Up @@ -274,7 +279,7 @@ def deep_clone( obj=self, cloned={} )
end
end
end

protected
def lock!
@_locked = true
Expand All @@ -285,12 +290,12 @@ def unlock!
@_locked = false
@_store.values.each { |store| store.unlock! if store.is_a?(Configatron::Store) }
end

private
def methods_include?(name)
self.methods.include?(RUBY_VERSION > '1.9.0' ? name.to_sym : name.to_s)
end

def parse_options(options)
if options.is_a?(Hash)
options.each do |k,v|
Expand All @@ -308,13 +313,13 @@ def parse_options(options)
return options
end
end

begin
undef :test # :nodoc:
rescue Exception => e
end

SYCK_CONSTANT = (RUBY_VERSION.match(/^1\.9/) ? Syck::MergeKey : YAML::Syck::MergeKey)

end # Store
end # Configatron

0 comments on commit c69f87b

Please sign in to comment.