Class: Llull::Matcher
- Inherits:
-
Object
- Object
- Llull::Matcher
- Defined in:
- lib/llull/result.rb
Overview
Matcher class for the pattern matching DSL
Instance Method Summary collapse
-
#execute ⇒ Object
Execute the appropriate handler based on the result.
-
#failure(type = nil, &block) ⇒ Matcher
Register a handler for failed results.
-
#initialize(result) ⇒ Matcher
constructor
A new instance of Matcher.
-
#success(&block) ⇒ Matcher
Register a handler for successful results.
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
#execute ⇒ Object
Execute the appropriate handler based on the result
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
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
194 195 196 197 |
# File 'lib/llull/result.rb', line 194 def success(&block) @success_handlers << block self end |