Skip to content

Commit

Permalink
Make RootStore and Store inherit from BasicObject.
Browse files Browse the repository at this point in the history
This change makes it less problematic to integrate with other libraries
that monkey patch Object to add their own methods, like capistrano or
rake.
  • Loading branch information
juanibiapina committed Jun 13, 2014
1 parent 6a83ead commit 5f6d2de
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
1 change: 0 additions & 1 deletion lib/configatron.rb
@@ -1,4 +1,3 @@
require 'singleton'
require 'configatron/version'

require 'configatron/deep_clone'
Expand Down
10 changes: 6 additions & 4 deletions lib/configatron/root_store.rb
@@ -1,7 +1,9 @@
require 'singleton'

# This is the root configatron object, and contains methods which
# operate on the entire configatron hierarchy.
class Configatron::RootStore
include Singleton
class Configatron::RootStore < BasicObject
include ::Singleton
extend ::Forwardable

attr_reader :store
Expand All @@ -22,7 +24,7 @@ def method_missing(name, *args, &block)
end

def reset!
@store = Configatron::Store.new(self)
@store = ::Configatron::Store.new(self)
end

def temp(&block)
Expand All @@ -32,7 +34,7 @@ def temp(&block)
end

def temp_start
@temp = Configatron::DeepClone.deep_clone(@store)
@temp = ::Configatron::DeepClone.deep_clone(@store)
end

def temp_end
Expand Down
21 changes: 11 additions & 10 deletions lib/configatron/store.rb
@@ -1,7 +1,8 @@
require 'forwardable'

class Configatron
class Store
class Store < BasicObject
include ::Kernel
extend ::Forwardable

def initialize(root_store, name='configatron')
Expand All @@ -14,16 +15,16 @@ def initialize(root_store, name='configatron')
def [](key)
val = fetch(key.to_sym) do
if @root_store.locked?
raise Configatron::UndefinedKeyError.new("Key not found: #{key} (for locked #{self})")
raise ::Configatron::UndefinedKeyError.new("Key not found: #{key} (for locked #{self})")
end
Configatron::Store.new(@root_store, "#{@name}.#{key}")
::Configatron::Store.new(@root_store, "#{@name}.#{key}")
end
return val
end

def store(key, value)
if @root_store.locked?
raise Configatron::LockedError.new("Cannot set key #{key} for locked #{self}")
raise ::Configatron::LockedError.new("Cannot set key #{key} for locked #{self}")
end
@attributes[key.to_sym] = value
end
Expand All @@ -38,7 +39,7 @@ def fetch(key, default_value = nil, &block)
end
store(key, val)
end
if val.is_a?(Configatron::Proc)
if val.is_a?(::Configatron::Proc)
val = val.call
end
return val
Expand All @@ -50,7 +51,7 @@ def key?(key)

def configure_from_hash(hash)
hash.each do |key, value|
if value.is_a?(Hash)
if value.is_a?(::Hash)
self[key].configure_from_hash(value)
else
store(key, value)
Expand All @@ -65,7 +66,7 @@ def to_s
def inspect
f_out = []
@attributes.each do |k, v|
if v.is_a?(Configatron::Store)
if v.is_a?(::Configatron::Store)
v.inspect.each_line do |line|
if line.match(/\n/)
line.each_line do |l|
Expand All @@ -87,13 +88,13 @@ def inspect
def method_missing(name, *args, &block)
# Needed to allow 'puts'ing of a configatron.
if name == :to_ary
raise NoMethodError.new("Called :to_ary")
raise ::NoMethodError.new("Called :to_ary")
end

# In case of Configatron bugs, prevent method_missing infinite
# loops.
if @method_missing
raise NoMethodError.new("Bug in configatron; ended up in method_missing while running: #{name.inspect}")
raise ::NoMethodError.new("Bug in configatron; ended up in method_missing while running: #{name.inspect}")
end
@method_missing = true
do_lookup(name, *args, &block)
Expand All @@ -115,7 +116,7 @@ def do_lookup(name, *args, &block)
if self.has_key?(key)
return self[key]
else
raise Configatron::UndefinedKeyError.new($1)
raise ::Configatron::UndefinedKeyError.new($1)
end
else
return self[name]
Expand Down
2 changes: 1 addition & 1 deletion test/unit/configatron/root_store.rb
Expand Up @@ -14,7 +14,7 @@ class Critic::Unit::RootTest < Critic::Unit::Test

describe 'global configatron' do
it 'returns an instance of Configatron::RootStore' do
assert_equal(true, configatron.kind_of?(Configatron::RootStore))
assert_equal(Configatron::RootStore.instance, configatron)
end
end

Expand Down

0 comments on commit 5f6d2de

Please sign in to comment.