Sha256: e40393c02f00d4655d25ecc877e8947f483b3c0f79925ef8f61a2665666352f1

Contents?: true

Size: 697 Bytes

Versions: 3

Compression:

Stored size: 697 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, **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

3 entries across 3 versions & 1 rubygems

Version Path
ensure_it-1.0.0 lib/ensure_it/ensure_instance_of.rb
ensure_it-0.1.5 lib/ensure_it/ensure_instance_of.rb
ensure_it-0.1.4 lib/ensure_it/ensure_instance_of.rb