Skip to content

Commit

Permalink
Updated tests one more time with better docs
Browse files Browse the repository at this point in the history
  • Loading branch information
auser committed May 8, 2009
1 parent d275b1d commit 3c591c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
15 changes: 11 additions & 4 deletions lib/dslify.rb
Expand Up @@ -32,21 +32,28 @@ class MyClass

module Dslify
module ClassMethods
# Allow default options
def default_options(hsh={})
(@default_options ||= {}).merge!(hsh)
end
# For every method, add a default of nil to the default_options hash
def dsl_methods(*arr)
arr.each {|a| default_options({a => nil}) }
end
# Forwarders array. If the method cannot be found on self, then check the forwarders
# to see if any of them respond to the method
# Starts with self.class and self.class.superclass
def forwarders
@forwarders ||= [self, superclass]
end
# Add forwarders
def forwards_to(*arr)
arr.each {|a| forwarders << a }
end
def forward_my_whole_chain(t=false)
@forward_my_whole_chain ||= t
def do_not_forward_my_whole_chain(t=false)
@do_not_forward_my_whole_chain ||= t
end
# Can the class handle the method?
def can_i_handle_the_method?(m)
(respond_to?(m.to_sym) || default_options.has_key?(m.to_sym)) ? true : false
end
Expand All @@ -65,8 +72,8 @@ def handle_the_method(m, *a, &block)
end

module InstanceMethods
def initialize(o={}, *a)
add_chain_of_ancestry if self.class.forward_my_whole_chain
def initialize(o={}, *a)
add_chain_of_ancestry unless self.class.do_not_forward_my_whole_chain

forwarders.each do |fwd|
@dsl_options = (fwd.default_options).merge(dsl_options) if fwd.respond_to?(:default_options)
Expand Down
11 changes: 8 additions & 3 deletions test/dslify_test.rb
Expand Up @@ -113,7 +113,6 @@ class Dad < Pop
end

class Grandad < Dad
forward_my_whole_chain true
end

class Defaults < Pop
Expand All @@ -134,7 +133,8 @@ def self.global_method
it "should take the default options set on the class" do
assert_equal @pop.dsl_options[:name], "pop"
end
it "should allow us to add defaults on the instance by calling dsl_options" do
it "should allow us to add defaults on the instance by calling dsl_options" do
# QuickieTest::Pop.name == "Cinnamon"
assert_equal @poptart.name, "Cinnamon"
end
it "should take the default options on a second class that inherits from the base" do
Expand All @@ -151,22 +151,27 @@ def self.global_method
assert_equal @bar.default_options[:name], "pangy"
end
it "should set the default options of the child to the superclass's if it doesn't exist" do
# QuickieTest::Dad => QuickieTest::Pop
d = Dad.new
assert d.name, "pop"
assert_equal d.name, "pop"
d.name "Frankenstein"
assert_equal d.name, "Frankenstein"
end
it "should raise if the method isn't found on itself, the parent or in the rest of the method missing chain" do
assert_raise NoMethodError do
Class.new.sanitorium
end
end
it "should be able to reach the grandparent through the chain of dsify-ed classes" do
# QuickieTest::Grandad => QuickieTest::Dad => QuickieTest::Pop
assert Grandad.new.name, "pop"
end
it "should be able to add a class as a forwarder" do
class Grandad
forwards_to Defaults
end
g = Grandad.new
# QuickieTest::Grandad => QuickieTest::Dad => QuickieTest::Defaults => QuickieTest::Dad => QuickieTest::Pop
assert_equal g.global_default, "red_rum"
end
it "should be able to add a class as a forwarder and get a method" do
Expand Down

0 comments on commit 3c591c9

Please sign in to comment.