Module: Llull
- Defined in:
- lib/llull/result.rb,
lib/llull.rb,
lib/llull/version.rb,
lib/llull/pattern_matching.rb
Overview
Llull provides composable Result types for Ruby applications. Inspired by functional programming and railway-oriented programming patterns.
Defined Under Namespace
Modules: PatternMatching Classes: Error, Matcher, Result, ResultError
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
-
.collect(results) ⇒ Result
(also: all)
Collect multiple Results into a single Result Returns Success with array of all values if all Results are successful, otherwise returns the first Failure encountered.
-
.Failure(error_type, error_data = nil) ⇒ Result
Create a failed Result.
-
.partition(results) ⇒ Array
Partition an array of Results into successes and failures.
-
.sequence(result) ⇒ Array<Result>
Convert a Result containing an array into an array of Results.
-
.Success(value) ⇒ Result
Create a successful Result.
-
.traverse(collection) ⇒ Result
Transform a collection by applying a function that returns Results Similar to collect but maps over input and collects the results.
-
.validate_results_array(results) ⇒ Object
Validate that all elements in array are Results.
Class Method Details
.collect(results) ⇒ Result Also known as: all
Collect multiple Results into a single Result Returns Success with array of all values if all Results are successful, otherwise returns the first Failure encountered
265 266 267 268 269 270 271 |
# File 'lib/llull/result.rb', line 265 def collect(results) results.each do |result| raise TypeError, "All elements must be Results" unless result.is_a?(Result) return result if result.failure? end Llull::Success(results.map(&:value)) end |
.Failure(error_type, error_data = nil) ⇒ Result
Create a failed Result
256 257 258 |
# File 'lib/llull/result.rb', line 256 def Failure(error_type, error_data = nil) Result.new(false, nil, error_type, error_data) end |
.partition(results) ⇒ Array
Partition an array of Results into successes and failures
305 306 307 308 309 310 |
# File 'lib/llull/result.rb', line 305 def partition(results) validate_results_array(results) successes, failures = results.partition(&:success?) [successes.map(&:value), failures.map { |f| [f.error_type, f.error_data] }] end |
.sequence(result) ⇒ Array<Result>
Convert a Result containing an array into an array of Results
288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/llull/result.rb', line 288 def sequence(result) raise TypeError, "Argument must be a Result" unless result.is_a?(Result) if result.success? raise TypeError, "Result value must be an Array" unless result.value.is_a?(Array) result.value.map { |item| Llull::Success(item) } else [result] # Return array containing the single failure end end |
.Success(value) ⇒ Result
Create a successful Result
248 249 250 |
# File 'lib/llull/result.rb', line 248 def Success(value) Result.new(true, value) end |
.traverse(collection) ⇒ Result
Transform a collection by applying a function that returns Results Similar to collect but maps over input and collects the results
280 281 282 283 |
# File 'lib/llull/result.rb', line 280 def traverse(collection, &) results = collection.map(&) collect(results) end |
.validate_results_array(results) ⇒ Object
Validate that all elements in array are Results
317 318 319 320 321 |
# File 'lib/llull/result.rb', line 317 def validate_results_array(results) results.each do |result| raise TypeError, "All elements must be Results" unless result.is_a?(Result) end end |