Skip to content

Instantly share code, notes, and snippets.

@r38y
Created November 5, 2009 05:28
Show Gist options
  • Save r38y/226795 to your computer and use it in GitHub Desktop.
Save r38y/226795 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe LoseItDispatcher do
describe ".deliver_phone_confirmation(user)" do
it "should call the same method on the SmsNotifier" do
user = mock_model(User, :phone_number => '12673346833', :null_object => true)
SmsNotifier.should_receive(:deliver_phone_confirmation).with(user) # wrong number of arguments (2 for 1)
LoseItDispatcher.deliver_phone_confirmation(user)
end
end
end
class ActionTextMessager
def initialize(method_name=nil, *parameters)
obj = self
self.send(method_name, *parameters)
obj
end
class << self
def method_missing(method_symbol, *parameters)
if match = matches_dynamic_method?(method_symbol)
case match[1]
when 'create' then new(match[2], *parameters)
when 'deliver' then new(match[2], *parameters).deliver!
else super
end
else
super
end
end
# problem goes away when I get rid of this
def respond_to?(method_symbol)
if match = matches_dynamic_method?(method_symbol)
instance_methods.include?(match[2])
else
super
end
end
private
def matches_dynamic_method?(method_name)
method_name = method_name.to_s
/^(create|deliver)_([_a-z]\w*)/.match(method_name)
end
end
%w(number message).each do |method_name|
define_method(method_name) do |*args|
arg = *args # we can't set a default when defining a method this way
if arg
instance_variable_set("@#{method_name}", arg)
else
instance_variable_get("@#{method_name}")
end
end
end
def deliver!
CLICKATELL_API.send_message(number, message)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment