Method: Contracts.common

Defined in:
lib/contracts.rb

.common(base) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/contracts.rb', line 20

def self.common(base)
  Eigenclass.lift(base)

  return if base.respond_to?(:Contract)

  base.extend(MethodDecorators)

  base.instance_eval do
    def functype(funcname)
      contracts = decorated_methods[:class_methods][funcname]
      if contracts.nil?
        "No contract for #{self}.#{funcname}"
      else
        "#{funcname} :: #{contracts[0]}"
      end
    end
  end

  base.class_eval do
    unless base.instance_of?(Module)
      def Contract(*args)
        return if ENV["NO_CONTRACTS"]
        if self.class == Module
          puts %{
Warning: You have added a Contract on a module function
without including Contracts::Modules. Your Contract will
just be ignored. Please include Contracts::Modules into
your module.}
        end
        self.class.Contract(*args)
      end
    end

    def functype(funcname)
      contracts = self.class.decorated_methods[:instance_methods][funcname]
      if contracts.nil?
        "No contract for #{self.class}.#{funcname}"
      else
        "#{funcname} :: #{contracts[0]}"
      end
    end
  end
end