Class: Contraction::Contract
- Inherits:
-
Object
- Object
- Contraction::Contract
- Defined in:
- lib/contract.rb
Instance Attribute Summary collapse
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#mod ⇒ Object
readonly
Returns the value of attribute mod.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
-
#initialize(rules, mod, method_name, type) ⇒ Contract
constructor
contract.
-
#valid_args?(*method_args) ⇒ Boolean
Test weather the arguments to a method are of the correct type, and meet the correct contractual obligations.
-
#valid_return?(*method_args, result) ⇒ Boolean
Tests weather or not the return value is of the correct type and meets the correct contractual obligations.
Constructor Details
#initialize(rules, mod, method_name, type) ⇒ Contract
contract.
7 8 9 10 11 12 13 14 |
# File 'lib/contract.rb', line 7 def initialize(rules, mod, method_name, type) @rules = rules @mod = mod @method_name = method_name update_rule_values get_method_definition(type) end |
Instance Attribute Details
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
3 4 5 |
# File 'lib/contract.rb', line 3 def method_name @method_name end |
#mod ⇒ Object (readonly)
Returns the value of attribute mod.
3 4 5 |
# File 'lib/contract.rb', line 3 def mod @mod end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
3 4 5 |
# File 'lib/contract.rb', line 3 def params @params end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
3 4 5 |
# File 'lib/contract.rb', line 3 def rules @rules end |
Instance Method Details
#valid_args?(*method_args) ⇒ Boolean
Test weather the arguments to a method are of the correct type, and meet the correct contractual obligations.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/contract.rb', line 18 def valid_args?(*method_args) return true if @rules.nil? named_args = params.each_with_index.inject({}) do |h, (param, index)| h[param.to_s] = method_args[index] h end b = binding param_rules.all? do |rule| raise ArgumentError.new("#{rule.name} (#{named_args[rule.name].inspect}) must be a #{rule.type}") unless rule.valid?(named_args[rule.name]) rule.evaluate_in_context(b, method_name, named_args[rule.name]) end end |
#valid_return?(*method_args, result) ⇒ Boolean
Tests weather or not the return value is of the correct type and meets the correct contractual obligations.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/contract.rb', line 34 def valid_return?(*method_args, result) named_args = params.each_with_index.inject({}) do |h, (param, index)| h[param] = method_args[index] h end return true unless return_rule unless return_rule.valid?(result) raise ArgumentError.new("Return value of #{method_name} must be a #{return_rule.type}") end if return_rule.contract b = binding return_rule.evaluate_in_context(b, method_name, result) end end |