Permalink
Browse files

first cut at adding in blank slate...work in progress!

  • Loading branch information...
1 parent 61b423e commit 27b060c6ed016971e3e4d0518ea6795a7f1a38a4 @rsanheim rsanheim committed Jan 20, 2009
View
@@ -0,0 +1,22 @@
+map_for(:configatron) do |nachos|
+ # nachos.watch "dir1", "dir2"
+
+ test_type = "spec" # or even just "test_type = 'test' or 'example' or 'spec' ??
+
+ nachos.keep_a_watchful_eye_for "app", "spec"
+
+ nachos.prepare_spell_for %r%^lib/(.*)\.rb% do |match|
+ ["#{test_type}/#{match[1]}_#{test_type}.rb"]
+ end
+
+ nachos.prepare_spell_for %r%^#{test_type}/(.*)_#{test_type}\.rb% do |match|
+ p match[1]
+ p match[0]
+ ["#{test_type}/#{match[1]}_#{test_type}.rb"]
+ end
+
+ nachos.prepare_spell_for %r%^#{test_type}/#{test_type}_helper\.rb% do |spell_component|
+ Dir["#{test_type}/**/*_#{test_type}.rb"]
+ end
+
+end
View
@@ -1,6 +1,7 @@
base = File.join(File.dirname(__FILE__), 'configatron')
require 'yaml'
require 'erb'
+require File.join(base, 'blank_slate')
require File.join(base, 'configatron')
require File.join(base, 'store')
require File.join(base, 'errors')
@@ -0,0 +1,37 @@
+class Configatron
+ class BlankSlate
+ class << self
+
+ # Hide the method named +name+ in the BlankSlate class. Don't
+ # hide +instance_eval+ or any method beginning with "__".
+ def hide(name)
+ if instance_methods.include?(name.to_s) and
+ name !~ /^(__|instance_eval|methods)/
+ @hidden_methods ||= {}
+ @hidden_methods[name.to_sym] = instance_method(name)
+ undef_method name
+ end
+ end
+
+ def find_hidden_method(name)
+ @hidden_methods ||= {}
+ @hidden_methods[name] || superclass.find_hidden_method(name)
+ end
+
+ # Redefine a previously hidden method so that it may be called on a blank
+ # slate object.
+ def reveal(name)
+ bound_method = nil
+ unbound_method = find_hidden_method(name)
+ fail "Don't know how to reveal method '#{name}'" unless unbound_method
+ define_method(name) do |*args|
+ bound_method ||= unbound_method.bind(self)
+ bound_method.call(*args)
+ end
+ end
+ end
+
+ instance_methods.each { |m| hide(m) }
+
+ end
+end
@@ -12,7 +12,7 @@ def initialize # :nodoc:
# Forwards the method call onto the 'namespaced' Configatron::Store
def method_missing(sym, *args)
- @_store[@_namespace.last].send(sym, *args)
+ @_store[@_namespace.last].__send__(sym, *args)
end
# Removes ALL configuration parameters
@@ -2,7 +2,7 @@ class Object # :nodoc:
def send_with_chain(methods, *args) # :nodoc:
obj = self
- [methods].flatten.each {|m| obj = obj.send(m, *args)}
+ [methods].flatten.each {|m| obj = obj.__send__(m, *args)}
obj
end
View
@@ -1,13 +1,13 @@
class Configatron
- class ProtectedParameter < StandardError
- def intialize(name)
- super("Can not modify protected parameter: '#{name}'")
+ class ProtectedParameter < RuntimeError
+ def initialize(_name)
+ super("Can not modify protected parameter: '#{_name}'")
end
end
- class LockedNamespace < StandardError
- def initialize(name)
- super("Cannot add new parameters to locked namespace: #{name.inspect}")
+ class LockedNamespace < RuntimeError
+ def initialize(_name)
+ super("Cannot add new parameters to locked namespace: #{_name.inspect}")
end
end
end
View
@@ -1,6 +1,6 @@
class Configatron
- class Store
- alias_method :send!, :send
+ class Store < BlankSlate
+ # alias_method :__send!, :__send__
# Takes an optional Hash of parameters
def initialize(options = {}, name = nil, parent = nil)
@@ -92,10 +92,14 @@ def set_default(name, default_value)
end
def method_missing(sym, *args) # :nodoc:
+ p "method_missing with #{sym} and #{args}"
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)
+ p 'in if block...'
+ p "protected: #{@_protected.inspect}"
+ p methods_include?(name)
+ raise Configatron::ProtectedParameter.new(name.to_s) if @_protected.include?(name) || methods_include?(name)
+ raise Configatron::LockedNamespace.new(@_name.to_s) if @_locked && !@_store.has_key?(name)
@_store[name] = parse_options(*args)
elsif @_store.has_key?(sym)
return @_store[sym]
@@ -120,7 +124,7 @@ def protect(name)
def protect_all!
@_protected.clear
@_store.keys.each do |k|
- val = self.send(k)
+ val = self.__send__(k)
val.protect_all! if val.class == Configatron::Store
@_protected << k
end
@@ -134,7 +138,7 @@ def unprotect(name)
def unprotect_all!
@_protected.clear
@_store.keys.each do |k|
- val = self.send(k)
+ val = self.__send__(k)
val.unprotect_all! if val.class == Configatron::Store
end
end
@@ -0,0 +1,31 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
+
+describe Configatron::BlankSlate do
+
+ before(:each) do
+ configatron.reset!
+ end
+
+ it "should respond to __id__, __send__, and instance_eval" do
+ lambda {
+ Configatron::BlankSlate.new.__id__
+ Configatron::BlankSlate.new.__send__
+ Configatron::BlankSlate.new.__methods__
+ Configatron::BlankSlate.new.instance_eval { 'hi' }
+ }.should_not raise_error(NoMethodError)
+ end
+
+ it "has methods" do
+ Configatron::BlankSlate.new.methods.should == ["methods", "__id__", "__send__", "instance_eval", "configatron", "send_with_chain"]
+ end
+
+ it "should not respond to Object's immediate methods" do
+ object_methods = Object.methods(false).reject { |m| m =~ /^(__|instance_eval)/ }.sort
+ object_methods.each do |meth|
+ lambda {
+ Configatron::BlankSlate.new.__send__(meth)
+ }.should raise_error(NoMethodError)
+ end
+ end
+
+end
@@ -3,15 +3,40 @@
describe "configatron" do
before(:each) do
+ p 'in each!'
configatron.reset!
end
+
+ describe 'store' do
+ it 'responds to methods_include' do
+ Configatron::Store.new.should respond_to(:methods_include)
+ Configatron::Store.new.should respond_to(:methods)
+ end
+
+ it "responds to protect" do
+ Configatron::Store.new.should respond_to(:protect)
+ end
+
+ end
describe 'protect' do
+ fit "should shovel protected attribute onto @protected" do
+ begin
+ configatron.one = 1
+ rescue Exception => e
+ p e.message
+ end
+ configatron.protect(:one)
+ configatron.instance_variable_get(:@_protected).should == [:one]
+ end
+
it 'should protect a parameter and prevent it from being set' do
configatron.one = 1
configatron.protect(:one)
- lambda{configatron.one = 'one'}.should raise_error(Configatron::ProtectedParameter)
+ lambda {
+ configatron.one = 'one'
+ }.should raise_error(Configatron::ProtectedParameter)
configatron.one.should == 1
end
@@ -51,7 +76,7 @@
configatron.protect_all!
[:a,:b].each do |l|
lambda{configatron.configure_from_hash(:letters => {l => l.to_s})}.should raise_error(Configatron::ProtectedParameter)
- configatron.letters.send(l).should == l.to_s.upcase
+ configatron.letters.__send__(l).should == l.to_s.upcase
end
lambda{configatron.letters.configure_from_hash(:a => 'a')}.should raise_error(Configatron::ProtectedParameter)
lambda{configatron.configure_from_hash(:letters => 'letters')}.should raise_error(Configatron::ProtectedParameter)
View
@@ -0,0 +1,9 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
+
+describe "errors" do
+
+ it "should have descriptive message" do
+ Configatron::ProtectedParameter.new("name").message.should == "Can not modify protected parameter: 'name'"
+ end
+
+end
View
@@ -0,0 +1,10 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
+
+describe Configatron::Store do
+
+ before(:each) do
+ configatron.reset!
+ end
+
+
+end
View
@@ -1,4 +1,15 @@
require 'rubygems'
-require 'spec'
+# require 'spec'
+gem 'spicycode-micronaut'
+gem 'relevance-log_buddy'
+require 'micronaut'
+require 'log_buddy'
+LogBuddy.init
+
+Micronaut.configure do |c|
+ c.filter_run :focused => true
+ c.alias_example_to :fit, :focused => true
+end
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'configatron')

0 comments on commit 27b060c

Please sign in to comment.