Class: Llull::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/llull/result.rb

Overview

Matcher class for the pattern matching DSL

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Matcher

Returns a new instance of Matcher.



184
185
186
187
188
189
# File 'lib/llull/result.rb', line 184

def initialize(result)
  @result = result
  @success_handlers = []
  @failure_handlers = {}
  @executed = false
end

Instance Method Details

#executeObject

Execute the appropriate handler based on the result

Returns:

  • (Object)

    the result of executing the matched handler



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/llull/result.rb', line 215

def execute
  return if @executed

  @executed = true

  if @result.success?
    @success_handlers.each { |handler| handler.call(@result.value) }
  elsif @failure_handlers.key?(@result.error_type)
    # Try specific error type handler first
    @failure_handlers[@result.error_type].call(@result.error_data)
  elsif @failure_handlers.key?(:catch_all)
    @failure_handlers[:catch_all].call(@result.error_type, @result.error_data)
  end
end

#failure(type = nil, &block) ⇒ Matcher

Register a handler for failed results

Parameters:

  • type (Symbol, nil) (defaults to: nil)

    specific error type to match, or nil for catch-all

  • block (Proc)

    handler that receives (error_type, error_data) for catch-all or just (error_data) for specific type matches

Returns:



204
205
206
207
208
209
210
211
# File 'lib/llull/result.rb', line 204

def failure(type = nil, &block)
  if type.nil?
    @failure_handlers[:catch_all] = block
  else
    @failure_handlers[type] = block
  end
  self
end

#success(&block) ⇒ Matcher

Register a handler for successful results

Parameters:

  • block (Proc)

    handler that receives the success value

Returns:



194
195
196
197
# File 'lib/llull/result.rb', line 194

def success(&block)
  @success_handlers << block
  self
end