Class: Errgonomic::Option::Some

Inherits:
Any show all
Defined in:
lib/errgonomic/option.rb,
lib/errgonomic/rails/active_record_optional.rb

Overview

A belongs_to declared touch: true reads the associated record back through the public reader after a save, then asks it to touch itself.

Constant Summary

Constants inherited from Any

Any::NUDGED, Any::RUST_SPELLINGS

Instance Method Summary collapse

Methods inherited from Any

#!=, #<=>, #==, #===, #and, #and_then, #as_json, #blank?, #blank_or, #blank_or_else, #blank_or_raise!, #deconstruct, #each, #eql?, #expect!, #filter, #flatten, #hash, #map, #map_or, #map_or_else, #method_missing, #none_or, #ok_or, #ok_or_else, #or, #or_else, #presence, #present?, #present_or, #present_or_else, #present_or_raise!, #pretty_print, #respond_to_missing?, #some_and, #tap_some, #to_a, #to_json, #to_option, #to_s, #try, #try!, #unwrap!, #unwrap_or, #unwrap_or_else, #xor, #zip, #zip_with

Constructor Details

#initialize(value) ⇒ Some

A Some is a value, not a slot: nothing outside reads the inner value without handling the None branch, and nothing swaps it out from under another reference or a Hash key.

Examples:

the inner value is reached through a combinator, never a reader

begin
  Some(1).value
rescue NoMethodError => e
  e.class
end # => Errgonomic::UnwrappedAccessError
Some(1).respond_to?(:value) # => false

a Some cannot be mutated through an alias

a = Some(1)
b = a
begin
  b.value = 99
rescue NoMethodError => e
  e.class
end # => Errgonomic::UnwrappedAccessError
a # => Some(1)
Some(1).frozen? # => true
begin
  Some(1).instance_variable_set(:@value, 2)
rescue FrozenError => e
  e.class
end # => FrozenError

a Some keeps its place as a Hash key

k = Some(1)
h = { k => :v }
begin
  k.value = 2
rescue NoMethodError
  nil
end
h[k] # => :v


963
964
965
966
967
# File 'lib/errgonomic/option.rb', line 963

def initialize(value)
  super()
  @value = value
  freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Errgonomic::Option::Any

Instance Method Details

#inspectObject

Render like Rust's Debug, delegating to the inner value's inspect so nesting stays unambiguous.

Examples:

Some(5).inspect # => "Some(5)"
Some("x").inspect # => "Some(\"x\")"
Some(nil).inspect # => "Some(nil)"
Some(Some(1)).inspect # => "Some(Some(1))"


985
986
987
# File 'lib/errgonomic/option.rb', line 985

def inspect
  "Some(#{value.inspect})"
end

#none?Boolean

Returns:

  • (Boolean)


973
974
975
# File 'lib/errgonomic/option.rb', line 973

def none?
  false
end

#some?Boolean

Returns:

  • (Boolean)


969
970
971
# File 'lib/errgonomic/option.rb', line 969

def some?
  true
end