Class: Some

Inherits:
Maybe show all
Defined in:
lib/indubitably.rb

Overview

Represents a non-empty value

Instance Method Summary collapse

Methods inherited from Maybe

concat, empty_value?, join?, none, #or_nil, seq, #to_ary

Constructor Details

#initialize(value) ⇒ Some

Returns a new instance of Some.



57
58
59
# File 'lib/indubitably.rb', line 57

def initialize(value)
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args, &block) ⇒ Object

This strips the leading underscore if the method is sent with that prefix; this allows us to force dispatch to the contained object. It is inline in two places because this avoids a function call performance hit.



109
110
111
112
# File 'lib/indubitably.rb', line 109

def method_missing(method_sym, *args, &block)
  method_sym = method_sym.slice(1, method_sym.length) if method_sym[0] == "_"
  map { |value| value.send(method_sym, *args, &block) }
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



97
98
99
# File 'lib/indubitably.rb', line 97

def ==(other)
  super && get == other.get
end

#===(other) ⇒ Object



102
103
104
# File 'lib/indubitably.rb', line 102

def ===(other)
  other && other.class == self.class && @value === other.get
end

#getObject



61
62
63
# File 'lib/indubitably.rb', line 61

def get
  @value
end

#if_some(val = nil, &blk) ⇒ Object



69
70
71
# File 'lib/indubitably.rb', line 69

def if_some(val = nil, &blk)
  Maybe(val.nil? ? blk.call : val)
end

#is_none?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/indubitably.rb', line 78

def is_none?
  false
end

#is_some?Boolean

rubocop:disable PredicateName

Returns:

  • (Boolean)


74
75
76
# File 'lib/indubitably.rb', line 74

def is_some?
  true
end

#joinObject

rubocop:enable PredicateName



83
84
85
# File 'lib/indubitably.rb', line 83

def join
  @value.is_a?(Maybe) ? @value : self
end

#join!Object



87
88
89
90
91
92
93
94
95
# File 'lib/indubitably.rb', line 87

def join!
  if @value.is_a?(Some)
    @value.join!
  elsif @value.is_a?(None)
    @value
  else
    self
  end
end

#or_elseObject



65
66
67
# File 'lib/indubitably.rb', line 65

def or_else(*)
  @value
end