Module: Llull::PatternMatching

Included in:
Result
Defined in:
lib/llull/pattern_matching.rb

Overview

Pattern matching functionality for Result instances

Instance Method Summary collapse

Instance Method Details

#deconstructArray

Pattern matching support for Ruby 3+ (array-style)

Returns:

  • (Array)

    deconstructed result for pattern matching



8
9
10
# File 'lib/llull/pattern_matching.rb', line 8

def deconstruct
  success? ? [:success, value] : [:failure, error_type, error_data]
end

#deconstruct_keys(_keys) ⇒ Hash

Pattern matching support for Ruby 3+ (hash-style)

Parameters:

  • keys (Array<Symbol>)

    requested keys for pattern matching

Returns:

  • (Hash)

    deconstructed result for hash pattern matching



15
16
17
18
19
20
21
# File 'lib/llull/pattern_matching.rb', line 15

def deconstruct_keys(_keys)
  if success?
    { success: true, value: value }
  else
    { success: false, error_type: error_type, error_data: error_data }
  end
end

#match(&block) ⇒ Object

Advanced pattern matching with DSL

Parameters:

  • block (Proc)

    block that receives a Matcher for defining patterns

Returns:

  • (Object)

    the result of executing the matched handler



26
27
28
29
30
# File 'lib/llull/pattern_matching.rb', line 26

def match(&block)
  matcher = Matcher.new(self)
  block.call(matcher)
  matcher.execute
end