Module: Contracts::Support

Defined in:
lib/contracts/support.rb

Class Method Summary collapse

Class Method Details

.contract_id(contract) ⇒ Object



23
24
25
# File 'lib/contracts/support.rb', line 23

def contract_id(contract)
  contract.object_id
end

.eigenclass?(target) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/contracts/support.rb', line 31

def eigenclass?(target)
  module_eigenclass?(target) ||
    target <= eigenclass_of(Object)
end

.eigenclass_of(target) ⇒ Object



27
28
29
# File 'lib/contracts/support.rb', line 27

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

.method_name(method) ⇒ Object



10
11
12
# File 'lib/contracts/support.rb', line 10

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

.method_position(method) ⇒ Object



4
5
6
7
8
# File 'lib/contracts/support.rb', line 4

def method_position(method)
  return method.method_position if method.is_a?(MethodReference)
  file, line = method.source_location
  "#{file}:#{line}"
end

.unique_idObject

Generates unique id, which can be used as a part of identifier

Example:

Contracts::Support.unique_id   # => "i53u6tiw5hbo"


18
19
20
21
# File 'lib/contracts/support.rb', line 18

def 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