Module: Contracts::Support

Defined in:
lib/contracts/support.rb

Class Method Summary collapse

Class Method Details

.contract_id(contract) ⇒ Object


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

def contract_id(contract)
  contract.object_id
end

.eigenclass?(target) ⇒ Boolean

Returns:

  • (Boolean)

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

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

.eigenclass_hierarchy_supported?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'lib/contracts/support.rb', line 34

def eigenclass_hierarchy_supported?
  RUBY_PLATFORM != "java" || RUBY_VERSION.to_f >= 2.0
end

.eigenclass_of(target) ⇒ Object


38
39
40
# File 'lib/contracts/support.rb', line 38

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

.indent_string(string, amount) ⇒ Object


47
48
49
50
51
52
# File 'lib/contracts/support.rb', line 47

def indent_string(string, amount)
  string.gsub(
    /^(?!$)/,
    (string[/^[ \t]/] || " ") * amount,
  )
end

.method_name(method) ⇒ Object


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

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

.method_position(method) ⇒ Object


6
7
8
9
10
11
12
13
14
15
# File 'lib/contracts/support.rb', line 6

def method_position(method)
  return method.method_position if method.is_a?(MethodReference)

  file, line = method.source_location
  if file.nil? || line.nil?
    ""
  else
    "#{file}:#{line}"
  end
end

.unique_idObject

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

Example:

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

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

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