Sha256: b153b8629c0c8ce9747ed3e4c1710fbe995141bf1e17e01d3cdeef00f3e783f5

Contents?: true

Size: 1.47 KB

Versions: 15

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Contracts
  module Support
    class << self
      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

      def 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 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 contract_id(contract)
        contract.object_id
      end

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

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

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

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

      private

      # Module eigenclass can be detected by its ancestor chain
      # containing a Module
      def module_eigenclass?(target)
        target < Module
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 5 rubygems

Version Path
contracts-0.17.2 lib/contracts/support.rb
contracts-0.17.1 lib/contracts/support.rb
entitlements-app-1.1.0 lib/contracts-ruby3/lib/contracts/support.rb
entitlements-app-1.0.0 lib/contracts-ruby3/lib/contracts/support.rb
entitlements-app-0.3.4 lib/contracts-ruby3/lib/contracts/support.rb
entitlements-app-0.3.1 lib/contracts-ruby3/lib/contracts/support.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/contracts-0.17/lib/contracts/support.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/contracts-0.17/lib/contracts/support.rb
entitlements-app-0.3.0 lib/contracts-ruby3/lib/contracts/support.rb
entitlements-0.2.1 lib/contracts-ruby3/lib/contracts/support.rb
entitlements-app-0.2.1 lib/contracts-ruby3/lib/contracts/support.rb
entitlements-0.2.0 lib/contracts-ruby3/lib/contracts/support.rb
entitlements-app-0.2.0 lib/contracts-ruby3/lib/contracts/support.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/contracts-0.17/lib/contracts/support.rb
contracts-0.17 lib/contracts/support.rb