Skip to content

Instantly share code, notes, and snippets.

@brianknapp
Created September 14, 2012 18:32
Show Gist options
  • Save brianknapp/3723792 to your computer and use it in GitHub Desktop.
Save brianknapp/3723792 to your computer and use it in GitHub Desktop.
tweet pre-post condition contract
class Contract
@@disable_override = false
@@contracts = []
def self.method_added name
unless @@disable_override
@@contracts.each do |method|
if name == method.to_sym
method_alias = "#{method}_alias".to_sym
method_contract = "#{method}_contract".to_sym
@@disable_override = true # to stop the new build method
self.send :alias_method, method_alias, name
self.send :remove_method, name
self.send :alias_method, name, method_contract
@@disable_override = false
else
puts "defining other method #{name}"
end
end
end
end
end
class TweetGatewayContract < Contract
@@contracts = [ 'save_tweet' ]
def save_tweet_contract(foo)
result = save_tweet_alias(foo)
if result != "result: " + foo
raise 'fail!'
end
return result
end
end
class TG < TweetGatewayContract
def save_tweet(foo)
return "result: " + foo
end
end
tg = TG.new
result = tg.save_tweet 'hi'
puts result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment