Sha256: 94f7f42b6685f8e11d7625587fabb5613666893eba1123c31798212ebad6654a

Contents?: true

Size: 711 Bytes

Versions: 2

Compression:

Stored size: 711 Bytes

Contents

module EnsureIt
  patch Object do
    def ensure_instance_of(klass, default: nil, **opts)
      unless klass.is_a?(Class)
        fail(
          ArgumentError,
          'Wrong class argument for #ensure_instance_of specified'
        )
      end
      is_a?(klass) ? self : default
    end

    def ensure_instance_of!(klass, default: nil, **opts)
      unless klass.is_a?(Class)
        fail(
          ArgumentError,
          'Wrong class argument for #ensure_instance_of specified'
        )
      end
      return self if is_a?(klass)
      opts[:message] ||=
        "\#{subject} should be an instance of '#{klass.name}' class"
      EnsureIt.raise_error(:ensure_instance_of!, **opts)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ensure_it-0.1.3 lib/ensure_it/ensure_instance_of.rb
ensure_it-0.1.2 lib/ensure_it/ensure_instance_of.rb