class Kind::Maybe::Some

Constants

KindSymbol
VALUE_CANT_BE_NONE

Public Class Methods

[](value) click to toggle source
# File lib/kind/maybe/some.rb, line 13
def self.[](value)
  return new(value) if !KIND.nil_or_undefined?(value)

  raise ArgumentError, VALUE_CANT_BE_NONE
end

Public Instance Methods

accept(method_name = UNDEFINED, &fn)
Alias for: check
and_then(method_name = UNDEFINED, &fn)
Alias for: map
and_then!(method_name = UNDEFINED, &fn)
Alias for: map!
check(method_name = UNDEFINED, &fn) click to toggle source
# File lib/kind/maybe/some.rb, line 49
def check(method_name = UNDEFINED, &fn)
  result = if UNDEFINED != method_name
    if method_name.kind_of?(::Symbol)
      @value.respond_to?(method_name) ? @value.public_send(method_name) : NONE_INSTANCE
    else
      method_name === @value
    end
  else
    fn.call(@value)
  end

  !result || KIND.nil_or_undefined?(result) ? NONE_INSTANCE : self
end
Also aliased as: accept
dig(*keys) click to toggle source
# File lib/kind/maybe/some.rb, line 97
def dig(*keys)
  resolve(Dig.call!(value, keys))
end
inspect() click to toggle source
# File lib/kind/maybe/some.rb, line 105
def inspect
  '#<%s value=%s>' % ['Kind::Some', value.inspect]
end
map(method_name = UNDEFINED, &fn) click to toggle source
# File lib/kind/maybe/some.rb, line 25
def map(method_name = UNDEFINED, &fn)
  map!(method_name, &fn)
rescue StandardError => exception
  None.new(exception)
end
Also aliased as: then, and_then
map!(method_name = UNDEFINED, &fn) click to toggle source
# File lib/kind/maybe/some.rb, line 34
def map!(method_name = UNDEFINED, &fn)
  result = if UNDEFINED != method_name
    return NONE_INSTANCE unless @value.respond_to?(KindSymbol[method_name])

    @value.public_send(method_name)
  else
    fn.call(@value)
  end

  resolve(result)
end
Also aliased as: then!, and_then!
none?() click to toggle source
# File lib/kind/maybe/some.rb, line 23
def none?; false; end
presence() click to toggle source
# File lib/kind/maybe/some.rb, line 101
def presence
  resolve(Presence.(value))
end
reject(method_name = UNDEFINED, &fn) click to toggle source
# File lib/kind/maybe/some.rb, line 65
def reject(method_name = UNDEFINED, &fn)
  result = if UNDEFINED != method_name
    if method_name.kind_of?(::Symbol)
      @value.respond_to?(method_name) ? @value.public_send(method_name) : NONE_INSTANCE
    else
      method_name === @value
    end
  else
    fn.call(@value)
  end

  result || KIND.nil_or_undefined?(result) ? NONE_INSTANCE : self
end
then(method_name = UNDEFINED, &fn)
Alias for: map
then!(method_name = UNDEFINED, &fn)
Alias for: map!
try(method_name = UNDEFINED, *args, &block) click to toggle source
# File lib/kind/maybe/some.rb, line 87
def try(method_name = UNDEFINED, *args, &block)
  return __try_block__(block, args) if block

  return __try_method__(method_name, args) if value.respond_to?(method_name)

  NONE_INSTANCE
rescue TypeError
  NONE_INSTANCE
end
try!(method_name = UNDEFINED, *args, &block) click to toggle source
# File lib/kind/maybe/some.rb, line 79
def try!(method_name = UNDEFINED, *args, &block)
  return __try_block__(block, args) if block

  return __try_method__(method_name, args) if UNDEFINED != method_name

  raise ArgumentError, 'method name or a block must be present'
end
value_or(_default = UNDEFINED, &block) click to toggle source
# File lib/kind/maybe/some.rb, line 19
def value_or(_default = UNDEFINED, &block)
  @value
end

Private Instance Methods

__try_block__(block, args) click to toggle source
# File lib/kind/maybe/some.rb, line 115
def __try_block__(block, args)
  result = args.empty? ? block.call(value) : block.call(*args.unshift(value))

  resolve(result)
end
__try_method__(method_name, args) click to toggle source
# File lib/kind/maybe/some.rb, line 111
def __try_method__(method_name, args)
  __try_block__(KindSymbol[method_name].to_proc, args)
end
resolve(result) click to toggle source
# File lib/kind/maybe/some.rb, line 121
def resolve(result)
  return result if Maybe::None === result
  return NONE_INSTANCE if KIND.nil_or_undefined?(result)
  return None.new(result) if ::Exception === result

  Some.new(result)
end