File: core.rb

package info (click to toggle)
ruby-contracts 0.17-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 624 kB
  • sloc: ruby: 3,805; makefile: 4; sh: 2
file content (54 lines) | stat: -rw-r--r-- 1,401 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
# frozen_string_literal: true

module Contracts
  module Core
    def self.included(base)
      common(base)
    end

    def self.extended(base)
      common(base)
    end

    def self.common(base)
      base.extend(MethodDecorators)

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

      # NOTE: Workaround for `defined?(super)` bug in ruby 1.9.2
      # source: http://stackoverflow.com/a/11181685
      # bug: https://bugs.ruby-lang.org/issues/6644
      base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
        # TODO: deprecate
        # Required when contracts are included in global scope
        def Contract(*args)
          if defined?(super)
            super
          else
            self.class.Contract(*args)
          end
        end
      RUBY

      base.class_eval do
        def functype(funcname)
          contracts = Engine.fetch_from(self.class).decorated_methods_for(:instance_methods, funcname)
          if contracts.nil?
            "No contract for #{self.class}.#{funcname}"
          else
            "#{funcname} :: #{contracts[0]}"
          end
        end
      end
    end
  end
end