Class: None

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

Overview

Represents an empty value

Defined Under Namespace

Classes: ValueExpectedException

Instance Method Summary collapse

Methods inherited from Maybe

#==, #inspect, #to_ary

Constructor Details

#initialize(inst_method = nil, parent_stack = []) ⇒ None

Returns a new instance of None.


146
147
148
149
# File 'lib/possibly.rb', line 146

def initialize(inst_method = nil, parent_stack = [])
  @inst_method = inst_method || ["None.new", []]
  @parent_stack = parent_stack
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

rubocop:enable PredicateName


200
201
202
# File 'lib/possibly.rb', line 200

def method_missing(method_sym, *args, &block)
  None([method_sym, args, block], self.stack)
end

Instance Method Details

#getObject


151
152
153
154
# File 'lib/possibly.rb', line 151

def get
  msg ||= "`get` called to None. A value was expected."
  raise ValueExpectedException.new(print_error(msg))
end

#is_none?Boolean

Returns:

  • (Boolean)

195
196
197
# File 'lib/possibly.rb', line 195

def is_none?
  true
end

#is_some?Boolean

rubocop:disable PredicateName

Returns:

  • (Boolean)

191
192
193
# File 'lib/possibly.rb', line 191

def is_some?
  false
end

#or_else(els = nil) ⇒ Object


156
157
158
# File 'lib/possibly.rb', line 156

def or_else(els = nil)
  block_given? ? yield : els
end

#or_nilObject


186
187
188
# File 'lib/possibly.rb', line 186

def or_nil
  nil
end

#or_raise(*args) ⇒ Object


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/possibly.rb', line 160

def or_raise(*args)
  opts, args = extract_opts(args)
  msg_or_exception, msg = args
  default_message = "`or_raise` called to None. A value was expected."

  exception_object =
    if msg_or_exception.respond_to? :exception
      if msg
        msg_or_exception.exception(msg)
      else
        msg_or_exception.exception
      end
    else
      ValueExpectedException.new(msg_or_exception || default_message)
    end

  exception_and_stack =
    if opts[:print_stack] == false
      exception_object
    else
      exception_object.exception(print_error(exception_object.message))
    end

  raise exception_and_stack
end

#to_sObject


204
205
206
# File 'lib/possibly.rb', line 204

def to_s
  "None"
end