module Contracts::Support
Public Class Methods
contract_id(contract)
click to toggle source
# File lib/contracts/support.rb, line 23 def contract_id(contract) contract.object_id end
eigenclass?(target)
click to toggle source
# File lib/contracts/support.rb, line 31 def eigenclass?(target) module_eigenclass?(target) || target <= eigenclass_of(Object) end
eigenclass_of(target)
click to toggle source
# File lib/contracts/support.rb, line 27 def eigenclass_of(target) class << target; self; end end
method_name(method)
click to toggle source
# File lib/contracts/support.rb, line 10 def method_name(method) method.is_a?(Proc) ? "Proc" : method.name end
method_position(method)
click to toggle source
# 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_id()
click to toggle source
Generates unique id, which can be used as a part of identifier
Example:
Contracts::Support.unique_id # => "i53u6tiw5hbo"
# 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
Private Class Methods
module_eigenclass?(target)
click to toggle source
Module eigenclass can be detected by its ancestor chain containing a Module
# File lib/contracts/support.rb, line 40 def module_eigenclass?(target) target < Module end