A Successful Attempt at Mangling Ruby
I’m working on the Ruby Koans by Edgecase which has been a really nice way to introduce myself (again) to Ruby. It’s the right mix of hand-holding and “solve-it-yourself” problems.
I just finished the about_proxy_object_project.rb Koan, and while it works, I fear that I’ve fairly well mangled Ruby, and there are probably infinitely more elegant ways to do this. My code looks like Java transcribed into Ruby.
Anyway, here is the code. If you have some spare time, take a look and see what I could have done better. I wrote the Proxy class, the rest was supplied.
# Project: Create a Proxy Class
#
# In this assignment, create a proxy class (one is started for you
# below). You should be able to initialize the proxy object with any
# object. Any messages sent to the proxy object should be forwarded
# to the target object. As each message is sent, the proxy should
# record the name of the method send.
#
# The proxy class is started for you. You will need to add a method
# missing handler and any other supporting methods. The specification
# of the Proxy class is given in the AboutProxyObjectProject koan.
class Proxy
class MessageLog
attr_accessor :message_name
attr_accessor :count
def initialize(message_name)
@message_name = message_name
@count = 1
end
def increment_count
@count = @count + 1
end
end
def initialize(target_object)
@object = target_object
@messages = []
end
def method_missing(method_name, *args, &block)
if @object.respond_to?(method_name)
increment_message_count(method_name)
@object.send(method_name, *args, &block)
else
super(method_name, *args, &block)
end
end
def increment_message_count(message_name)
message_log = called?(message_name)
if message_log.nil?
@messages < < MessageLog.new(message_name)
else
message_log.increment_count
end
end
def messages
@messages.collect { |message_log| message_log.message_name }
end
def called?(command)
matched_message_log = nil
@messages.each do |message_log|
if message_log.message_name == command
matched_message_log = message_log
end
end
matched_message_log
end
def number_of_times_called(command)
message_log = called?(command)
message_log.nil? ? 0 : message_log.count
end
end
# The proxy object should pass the following Koan:
#
class AboutProxyObjectProject < EdgeCase::Koan
def test_proxy_method_returns_wrapped_object
# NOTE: The Television class is defined below
tv = Proxy.new(Television.new)
assert tv.instance_of?(Proxy)
end
def test_tv_methods_still_perform_their_function
tv = Proxy.new(Television.new)
tv.channel = 10
tv.power
assert_equal 10, tv.channel
assert tv.on?
end
def test_proxy_records_messages_sent_to_tv
tv = Proxy.new(Television.new)
tv.power
tv.channel = 10
assert_equal [:power, :channel=], tv.messages
end
def test_proxy_handles_invalid_messages
tv = Proxy.new(Television.new)
assert_raise(NoMethodError) do
tv.no_such_method
end
end
def test_proxy_reports_methods_have_been_called
tv = Proxy.new(Television.new)
tv.power
tv.power
assert tv.called?(:power)
assert ! tv.called?(:channel)
end
def test_proxy_counts_method_calls
tv = Proxy.new(Television.new)
tv.power
tv.channel = 48
tv.power
assert_equal 2, tv.number_of_times_called(:power)
assert_equal 1, tv.number_of_times_called(:channel=)
assert_equal 0, tv.number_of_times_called(:on?)
end
def test_proxy_can_record_more_than_just_tv_objects
proxy = Proxy.new("Code Mash 2009")
proxy.upcase!
result = proxy.split
assert_equal ["CODE", "MASH", "2009"], result
assert_equal [:upcase!, :split], proxy.messages
end
end
# ====================================================================
# The following code is to support the testing of the Proxy class. No
# changes should be necessary to anything below this comment.
# Example class using in the proxy testing above.
class Television
attr_accessor :channel
def power
if @power ==
n
@power =
ff
else
@power =
n
end
end
def on?
@power ==
n
end
end
# Tests for the Television class. All of theses tests should pass.
class TelevisionTest < EdgeCase::Koan
def test_it_turns_on
tv = Television.new
tv.power
assert tv.on?
end
def test_it_also_turns_off
tv = Television.new
tv.power
tv.power
assert ! tv.on?
end
def test_edge_case_on_off
tv = Television.new
tv.power
tv.power
tv.power
assert tv.on?
tv.power
assert ! tv.on?
end
def test_can_set_the_channel
tv = Television.new
tv.channel = 11
assert_equal 11, tv.channel
end
end
Since you asked what you could have done better, I just finished the Ruby Koans, and this is what I came up with for the Proxy object:
http://pastie.org/631227