Module: Mona::Result::Action

Included in:
Ephemeral
Defined in:
lib/mona/result/action.rb

Overview

mixin for objects that return Result::Dict results

define #perform to do the work, use #set to add results to the Result::Dict, the first failure will abort the rest of the #perform method and the result will be returned. This works like monadic ‘do’ notation.

After a result is set at a key, it can be accessed via its key name as a method.

use the Action with #call

example:

class UpdateUser
  inlcude Mona::Result::Action
  include Auditing # for example, adds #audit(model, input) method

  def perform(user_id, attributes)
    set :user, UserRepo.find(user_id) # if find is Err, the block exits with failure of :user
    set :input, UserInput.valid(attributes) # likewise if valid is Err, the block exits
    set [:user, :input], UserRepo.update(user.id, input) # note that #input and #user are available methods
                                                         # if update is Err it is set on the :input key
    audit(user, input) # this only runs if all of the above set successful results
  end
end

UpdateUser.new.call(user_id, attributes) # => Result

Since:

  • 0.1.0

Defined Under Namespace

Classes: Ephemeral

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args) ⇒ Object (private)

Raises:

  • (NoMethodError)

Since:

  • 0.1.0



77
78
79
80
81
# File 'lib/mona/result/action.rb', line 77

def method_missing(key, *args)
  return @sequence.fetch(key) if args.empty? && @sequence.key?(key)

  raise NoMethodError, "no method `#{key}' for #{self}"
end

Instance Method Details

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

Since:

  • 0.1.0



64
65
66
67
68
69
# File 'lib/mona/result/action.rb', line 64

def call(*args, **kwargs)
  @sequence = Sequence.new
  @sequence.call { |_sequence| perform(*args, **kwargs) }
ensure
  remove_instance_variable :@sequence
end