Class: Mona::Result::Action::Ephemeral

Inherits:
Object
  • Object
show all
Includes:
Mona::Result::Action
Defined in:
lib/mona/result/action.rb

Overview

You can create an Ephemeral Action as follows:

Example:

compute = Mona::Result::Action::Ephemeral.new do |x, y|
  set :numerator, x
  puts "set numerator: #{numerator}"
  set :denominator, y.zero? ? Result.failure(y, :zero) : y
  puts "set denominator: #{denominator}"
  set :answer, numerator / denominator
  puts "answer: #{answer}"
end

> compute.call(10,2)
set numerator: 10
set denominator: 2
answer: 5
=> #<Result success: {:numerator=>10, :denominator=>2, :answer=>5}>

> compute.call(10,0)
set numerator: 10
=> #<Result failure: {:numerator=>10, :denominator=>0}, error: {:error=>:zero, :on=>:denominator}>

Mona::Result.action is a shortcut for this

Since:

  • 0.1.0

Instance Method Summary collapse

Methods included from Mona::Result::Action

#call

Constructor Details

#initialize(&perform) ⇒ Ephemeral

Returns a new instance of Ephemeral.

Since:

  • 0.1.0



57
58
59
# File 'lib/mona/result/action.rb', line 57

def initialize(&perform)
  @perform = perform
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mona::Result::Action

Instance Method Details

#perform(*args, **kwargs) ⇒ Object

Since:

  • 0.1.0



61
# File 'lib/mona/result/action.rb', line 61

def perform(*args, **kwargs) = instance_exec(*args, **kwargs, &@perform)