Module: Contracts::DSL::ClassMethods

Defined in:
lib/ruby_contracts.rb

Instance Method Summary collapse

Instance Method Details

#__contract_failure!(name, message, result, *args) ⇒ Object

Raises:



42
43
44
45
# File 'lib/ruby_contracts.rb', line 42

def __contract_failure!(name, message, result, *args)
  args.pop if args.last.kind_of?(Proc)
  raise Contracts::Error.new("#{self}##{name}(#{args.join ', '}) => #{result || "?"} ; #{message}.")
end

#__contracts_for(name, current_contracts = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_contracts.rb', line 27

def __contracts_for(name, current_contracts=nil)
  inherited_contracts = ancestors[1..-1].reduce(Contracts.empty_contracts) do |c, klass|
    ancestor_hash = klass.instance_variable_get('@__contracts_for') || {}
    c[:before] += ancestor_hash.has_key?(name) ? ancestor_hash[name][:before] : []
    c[:after] += ancestor_hash.has_key?(name) ? ancestor_hash[name][:after] : []
    c
  end
  current_contracts = @__contracts_for[name] || current_contracts || Contracts.empty_contracts

  contracts = Contracts.empty_contracts
  contracts[:before] = current_contracts[:before] + inherited_contracts[:before]
  contracts[:after] = current_contracts[:after] + inherited_contracts[:after]
  contracts
end

#__contracts_initializeObject



22
23
24
25
# File 'lib/ruby_contracts.rb', line 22

def __contracts_initialize
  @__contracts = Contracts.empty_contracts
  @__contracts_for = {}
end

#inherited(subclass) ⇒ Object



17
18
19
20
# File 'lib/ruby_contracts.rb', line 17

def inherited(subclass)
  super
  subclass.__contracts_initialize
end

#method_added(name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruby_contracts.rb', line 60

def method_added(name)
  super

  return unless ENV['ENABLE_ASSERTION']
  return if @__contracts_for.has_key?(name)

  __contracts = @__contracts_for[name] ||= __contracts_for(name, @__contracts)
  @__contracts = Contracts.empty_contracts

  if !__contracts[:before].empty? || !__contracts[:after].empty?
    original_method_name = "#{name}__with_contracts"
    define_method(original_method_name, instance_method(name))

    count = 0
    before_contracts = __contracts[:before].reduce("") do |code, contract|
      type, *args = contract
      case type
      when :type
        classes = args[0]
        code << "if __args.size < #{classes.size} then\n"
        code << "  self.class.__contract_failure!('#{name}', \"need at least #{classes.size} arguments (%i given)\" % [__args.size], nil, *args)\n"
        code << "else\n"
        conditions = []
        classes.each_with_index{ |klass, i| conditions << "__args[#{i}].kind_of?(#{klass})" }
        code << "  if !(#{conditions.join(' && ')}) then\n"
        code << "    self.class.__contract_failure!('#{name}', 'input type error', nil, *__args)\n"
        code << "  end\n"
        code << "end\n"
        code

      when :params
        # Define a method that verify the assertion
        contract_method_name = "__verify_contract_#{name}_in_#{count = count + 1}"
        define_method(contract_method_name) { |*params| self.instance_exec(*params, &args[1]) }

        code << "if !#{contract_method_name}(*__args) then\n"
        code << "  self.class.__contract_failure!('#{name}', \"invalid precondition: #{args[0]}\", nil, *__args)\n"
        code << "end"
        code
      else
        code
      end
    end

    after_contracts = __contracts[:after].reduce("") do |code, contract|
      type, *args = contract
      case type
      when :type
        code << "if !result.kind_of?(#{args[0]}) then\n"
        code << "self.class.__contract_failure!(name, \"result must be a kind of '#{args[0]}' not '%s'\" % [result.class.to_s], result, *__args)\n"
        code << "end\n"
        code
      when :result
        # Define a method that verify the assertion
        contract_method_name = "__verify_contract_#{name}_out_#{count = count + 1}"
        define_method(contract_method_name) { |*params| self.instance_exec(*params, &args[1]) }

        code << "if !#{contract_method_name}(result, *__args) then\n"
        code << "  self.class.__contract_failure!('#{name}', \"invalid postcondition: #{args[0]}\", result, *__args)\n"
        code << "end"
        code
      else
        code
      end
    end

    method = "      def \#{name}(*args, &block)\n        __args = block.nil? ? args : args + [block]\n        \#{before_contracts}\n        result = \#{original_method_name}(*args, &block)\n        \#{after_contracts}\n        return result\n      end\n    EOM\n\n    class_eval method\n  end\nend\n"

#post(message = nil, &block) ⇒ Object



56
57
58
# File 'lib/ruby_contracts.rb', line 56

def post(message=nil, &block)
  @__contracts[:after] << [:result, message, block] if ENV['ENABLE_ASSERTION']
end

#pre(message = nil, &block) ⇒ Object



52
53
54
# File 'lib/ruby_contracts.rb', line 52

def pre(message=nil, &block)
  @__contracts[:before] << [:params, message, block] if ENV['ENABLE_ASSERTION']
end

#type(options) ⇒ Object



47
48
49
50
# File 'lib/ruby_contracts.rb', line 47

def type(options)
  @__contracts[:before] << [:type, options[:in]] if ENV['ENABLE_ASSERTION'] && options.has_key?(:in)
  @__contracts[:after] << [:type, options[:out]] if ENV['ENABLE_ASSERTION'] && options.has_key?(:out)
end