Sha256: 5d547c8036f0262743333acf3d2274faef1d0854e6d55a4259e35cf6cee754c3

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module Contracts
  module Support
    def self.method_position(method)
      return method.method_position if method.is_a?(MethodReference)

      if RUBY_VERSION =~ /^1\.8/
        if method.respond_to?(:__file__)
          method.__file__ + ":" + method.__line__.to_s
        else
          method.inspect
        end
      else
        file, line = method.source_location
        file + ":" + line.to_s
      end
    end

    def self.method_name(method)
      method.is_a?(Proc) ? "Proc" : method.name
    end

    # Generates unique id, which can be used as a part of identifier
    #
    # Example:
    #    Contracts::Support.unique_id   # => "i53u6tiw5hbo"
    def self.unique_id
      # Consider using SecureRandom.hex here, and benchmark which one is better
      (Time.now.to_f * 1000).to_i.to_s(36) + rand(1_000_000).to_s(36)
    end

    def self.eigenclass_hierarchy_supported?
      return false if RUBY_PLATFORM == "java" && RUBY_VERSION.to_f < 2.0
      RUBY_VERSION.to_f > 1.8
    end

    def self.eigenclass_of(target)
      class << target; self; end
    end

    def self.eigenclass?(target)
      target <= eigenclass_of(Object)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
contracts-0.9 lib/contracts/support.rb
contracts-0.8 lib/contracts/support.rb