Skip to content

Commit

Permalink
remove some hard coding in return value
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyhaines committed Jul 31, 2009
1 parent a4c1929 commit d39ce66
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
11 changes: 8 additions & 3 deletions features/multi-methods.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Feature: Multi-methods

Scenario: simple hello, world, one path
Scenario Outline: simple one path
Given the following code
"""
class HelloWorldReturner
Expand All @@ -9,7 +9,7 @@ Feature: Multi-methods
multi_method :hello_world do
router {|*args| :hello_world}
implementation_for :hello_world do
'you worked'
"<expected_return_value>"
end
end
end
Expand All @@ -19,4 +19,9 @@ Feature: Multi-methods
p = HelloWorldReturner.new
p.hello_world
"""
Then the return should be "you worked"
Then the return should be "<expected_return_value>"

Examples:
|expected_return_value|
|you worked|
|another value|
2 changes: 1 addition & 1 deletion features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
require 'multi-methods'
require 'multi_methods'

require 'spec/expectations'
14 changes: 0 additions & 14 deletions lib/multi-methods.rb

This file was deleted.

27 changes: 27 additions & 0 deletions lib/multi_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module MultiMethods
def self.included(base)
base.extend ClassMethods
end

class ImplementationCapture
attr_accessor :methods
def initialize
@methods = {}
end
def router

end
def implementation_for symbol, &block
@methods[symbol] = block
end
end

module ClassMethods
def multi_method name, &block
implementation = ImplementationCapture.new
implementation.instance_eval(&block)
define_method name, implementation.methods[:hello_world]
end
end

end
25 changes: 9 additions & 16 deletions spec/multi-methods_spec.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')


class Foo
class HelloWorldReturner
include MultiMethods

multi_method :bar do
multi_method :hello_world do
router {|*args| :hello_world}
implementation_for :hello_world do
'you worked'
end
end
end

class HelloWorldReturner
class HelloWorldReturner2
include MultiMethods

multi_method :hello_world do
router {|*args| :hello_world}
implementation_for :hello_world do
'you worked'
'another return'
end
end
end

describe MultiMethods do

context "getting it compiling" do
it "allows me to include and call on a class" do
Foo.new
end
end

context "calling the created method" do
it "has the method" do
Foo.new.bar
end
it "returns the appropriate value" do
HelloWorldReturner.new.hello_world.should == "you worked"
HelloWorldReturner2.new.hello_world.should == "another return"
end
end


end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'multi-methods'
require 'multi_methods'
require 'spec'
require 'spec/autorun'

Expand Down

0 comments on commit d39ce66

Please sign in to comment.