Module: RailLine::ResultDo

Defined in:
lib/rail_line/result_do.rb,
lib/rail_line/result_do/thread_context.rb

Overview

Utilized for enabling RailLine functionality

Use include RailLine::ResultDo or include RailLine::ResultDo to include handle_result functionality.

Examples:

class MyService
  include RailLine::ResultDo

  def call
    handle_result do
      # Your service logic here
      RailLine::Success.new(message: "It worked!")
    end
  end
end

Defined Under Namespace

Modules: ClassMethods, ThreadContext

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*method_names) ⇒ Module

Enables the use of the automatic handling of results.

Examples:

class MyAwesomeService
  include RailLine::ResultDo[:call]

  def call
    # ...
  end
end

Parameters:

  • method_names (Array<Symbol>)

    The names of the methods to wrap.

Returns:

  • (Module)

    The module that wraps the methods.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rail_line/result_do.rb', line 38

def self.[](*method_names)
  Module.new do
    define_singleton_method(:included) do |base|
      base.extend(RailLine::ResultDo::ClassMethods)

      wrapped_methods = Module.new do
        method_names.each do |method_name|
          define_method(method_name) do |*args, **kwargs|
            base.handle_result { super(*args, **kwargs) }
          end
        end
      end

      base.prepend(wrapped_methods)
      base.singleton_class.prepend(wrapped_methods)
    end
  end
end

.included(base) ⇒ Object



21
22
23
# File 'lib/rail_line/result_do.rb', line 21

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#handle_result { ... } ⇒ RailLine::Failure, RailLine::Success

Returns The result of the block.

Yields:

  • The block to execute

Returns:



122
123
124
# File 'lib/rail_line/result_do.rb', line 122

def handle_result(&block)
  self.class.handle_result(&block)
end