Skip to content

Commit

Permalink
adding contract_for, a better syntax for defining contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
Trevor Little committed Jan 14, 2013
1 parent 249c01f commit 91b25f2
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/obvious/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ def self.contracts *contracts
end
end

# Public: Defines a contract for a method
#
# method - a symbol representing the method name
# contract - a hash with the keys :input and :output holding the respective shapes.
#
# Examples
#
# class FooJackContract < Contract
#
# contract_for :save, {
# :input => Foo.shape,
# :output => Foo.shape,
# }
#
# end
#
def self.contract_for method, contract
method_alias = "#{method}_alias".to_sym
method_contract = "#{method}_contract".to_sym

define_method method_contract do |*args|
input = args[0]
call_method method_alias, input, contract[:input], contract[:output]
end

contracts( *contract_list, method )
end

# This method will move methods defined in @contracts into new methods.
# Each entry in @contracts will cause the method with the same name to
# become method_name_alias and for the original method to point to
Expand Down

0 comments on commit 91b25f2

Please sign in to comment.