Class: Linearly::Step::Static

Inherits:
Object
  • Object
show all
Extended by:
Mixins::FlowBuilder
Defined in:
lib/linearly/step/static.rb

Overview

Static is a type of Step whose operation solely depends on the content of the State passed to its Static.call method. It’s the best and most deterministic type of a Step, hence we provided a helper. Inheriting from Static will still require you to implement three methods: class inputs and outputs methods, and an instance call method. What you get though is that your call method does not need to take any parameters and will have access to the private state instance variable. What’s more, any unknown messages will be forwarded to state, so that your code can be shorter and more expressive. Also, you don’t have to explicitly rescue exceptions - the static Static.call method will catch those and fail the state accordingly.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::FlowBuilder

>>, >>

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Dynamically pass valid inputs to the wrapped State

Parameters:

  • name (Symbol|String)
  • args (Array<Object>)
  • block (Proc)

Returns:

  • (Object)

Raises:

  • (NoMethodError)


100
101
102
103
# File 'lib/linearly/step/static.rb', line 100

def method_missing(name, *args, &block)
  allowed = [:correlation_id] + state.methods + self.class.inputs.keys
  allowed.include?(name) ? state.send(name, *args, &block) : super
end

Class Method Details

.call(state) ⇒ Statefully::State

Main entry point to Linearly::Step::Static

Examples:

class FindUser < Linearly::Step::Static
  def self.inputs
    { user_id: Integer }
  end

  def self.outputs
    { user: User }
  end

  def call
    succeed(user: User.find(user_id))
  end
end
FindUser.call(Statefully::State.create(user_id: params[:id]))

Parameters:

  • state (Statefully::State)

Returns:

  • (Statefully::State)


38
39
40
41
42
# File 'lib/linearly/step/static.rb', line 38

def self.call(state)
  new(state).call
rescue StandardError => err
  state.fail(err)
end

.inputsHash<Symbol, Expectation>

Inputs for a step

Examples:

FindUser.inputs
=> { user_id: Integer }

Returns:

  • (Hash<Symbol, Expectation>)

Raises:

  • (NotImplementedError)


59
60
61
# File 'lib/linearly/step/static.rb', line 59

def self.inputs
  raise NotImplementedError
end

.outputsHash<Symbol, Expectation>

Outputs for a step

Examples:

FindUser.outputs
=> { user: User }

Returns:

  • (Hash<Symbol, Expectation>)


70
71
72
# File 'lib/linearly/step/static.rb', line 70

def self.outputs
  {}
end

Instance Method Details

#callStatefully::State

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

User-defined logic for this Step

Returns:

  • (Statefully::State)

Raises:

  • (NotImplementedError)


48
49
50
# File 'lib/linearly/step/static.rb', line 48

def call
  raise NotImplementedError
end