Module: Solana::Ruby::Kit::Options

Extended by:
T::Sig
Defined in:
lib/solana/ruby/kit/options/option.rb

Defined Under Namespace

Classes: None, Some

Constant Summary collapse

Option =

Sorbet type alias: Option<T> is either Some or None. Because Sorbet generics on non-class types are limited, we use T.untyped for the contained value at the type-alias level; callers rely on Some’s generic parameter for per-use type safety.

T.type_alias { T.any(Solana::Ruby::Kit::Options::Some[T.untyped], None) }
OptionOrNullable =

OptionOrNullable mirrors TypeScript’s ‘OptionOrNullable<T>`:

accepts Option<T>, T, or nil 
T.type_alias { T.untyped }

Class Method Summary collapse

Class Method Details

.noneObject



70
71
72
# File 'lib/solana/ruby/kit/options/option.rb', line 70

def none
  None::INSTANCE
end

.none?(opt) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/solana/ruby/kit/options/option.rb', line 91

def none?(opt)
  opt.is_a?(None)
end

.option?(input) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/solana/ruby/kit/options/option.rb', line 77

def option?(input)
  input.is_a?(Some) || input.is_a?(None)
end

.some(value) ⇒ Object



64
65
66
# File 'lib/solana/ruby/kit/options/option.rb', line 64

def some(value)
  Some.new(value)
end

.some?(opt) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/solana/ruby/kit/options/option.rb', line 84

def some?(opt)
  opt.is_a?(Some)
end

.unwrap_option(opt, fallback = nil) ⇒ Object



101
102
103
104
105
# File 'lib/solana/ruby/kit/options/option.rb', line 101

def unwrap_option(opt, fallback = nil)
  return opt.value if opt.is_a?(Some)

  fallback ? fallback.call : nil
end

.unwrap_option_recursively(input, fallback = nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/solana/ruby/kit/options/option.rb', line 120

def unwrap_option_recursively(input, fallback = nil)
  nxt = ->(x) { unwrap_option_recursively(x, fallback) }

  case input
  when Some then nxt.call(input.value)
  when None then fallback ? fallback.call : nil
  when Array then input.map { |el| nxt.call(el) }
  when Hash  then input.transform_values { |v| nxt.call(v) }
  else input   # primitives and opaque objects pass through
  end
end

.wrap_nullable(nullable) ⇒ Object



110
111
112
# File 'lib/solana/ruby/kit/options/option.rb', line 110

def wrap_nullable(nullable)
  nullable.nil? ? none : some(nullable)
end