Module: Interaktor::ClassMethods

Defined in:
lib/interaktor.rb

Instance Method Summary collapse

Instance Method Details

#call(context = {}) ⇒ Interaktor::Context

Invoke an Interaktor. This is the primary public API method to an interaktor.

with attributes or an already-built context

Parameters:

Returns:



117
118
119
120
121
122
123
# File 'lib/interaktor.rb', line 117

def call(context = {})
  verify_attribute_presence(context)

  catch(:early_return) do
    new(context).tap(&:run).instance_variable_get(:@context)
  end
end

#call!(context = {}) ⇒ Interaktor::Context

Invoke an Interaktor. This method behaves identically to ‘#call`, with one notable exception - if the context is failed during the invocation of the interaktor, `Interaktor::Failure` is raised.

with attributes or an already-built context

Parameters:

Returns:



135
136
137
138
139
140
141
# File 'lib/interaktor.rb', line 135

def call!(context = {})
  verify_attribute_presence(context)

  catch(:early_return) do
    new(context).tap(&:run!).instance_variable_get(:@context)
  end
end

#failure(*attributes) ⇒ void

This method returns an undefined value.

A DSL method for documenting required interaktor failure attributes.

Parameters:

  • attributes (Symbol, Array<Symbol>)

    the list of attribute names



97
98
99
# File 'lib/interaktor.rb', line 97

def failure(*attributes)
  failure_attributes.concat attributes
end

#failure_attributesArray<Symbol>

The list of attributes which are required to be passed in when calling ‘#fail!` from within the interaktor.

Returns:

  • (Array<Symbol>)


40
41
42
# File 'lib/interaktor.rb', line 40

def failure_attributes
  @failure_attributes ||= []
end

#optional(*attributes) ⇒ void

This method returns an undefined value.

A DSL method for documenting optional interaktor attributes.

Parameters:

  • attributes (Symbol, Array<Symbol>)

    the list of attribute names



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/interaktor.rb', line 73

def optional(*attributes)
  optional_attributes.concat attributes

  attributes.each do |attribute|
    define_method(attribute) { @context.send(attribute) }
    define_method("#{attribute}=".to_sym) do |value|
      unless @context.to_h.key?(attribute)
        raise "                You can't assign a value to an optional parameter if you\n                didn't initialize the interaktor with it in the first\n                place.\n              ERROR\n      end\n\n      @context.send(\"\#{attribute}=\".to_sym, value)\n    end\n  end\nend\n"

#optional_attributesArray<Symbol>

The list of attributes which are NOT required to be passed in when calling the interaktor.

Returns:

  • (Array<Symbol>)


32
33
34
# File 'lib/interaktor.rb', line 32

def optional_attributes
  @optional_attributes ||= []
end

#required(*attributes) ⇒ void

This method returns an undefined value.

A DSL method for documenting required interaktor attributes.

Parameters:

  • attributes (Symbol, Array<Symbol>)

    the list of attribute names



57
58
59
60
61
62
63
64
65
66
# File 'lib/interaktor.rb', line 57

def required(*attributes)
  required_attributes.concat attributes

  attributes.each do |attribute|
    define_method(attribute) { @context.send(attribute) }
    define_method("#{attribute}=".to_sym) do |value|
      @context.send("#{attribute}=".to_sym, value)
    end
  end
end

#required_attributesArray<Symbol>

The list of attributes which are required to be passed in when calling the interaktor.

Returns:

  • (Array<Symbol>)


24
25
26
# File 'lib/interaktor.rb', line 24

def required_attributes
  @required_attributes ||= []
end

#success(*attributes) ⇒ void

This method returns an undefined value.

A DSL method for documenting required interaktor success attributes.

Parameters:

  • attributes (Symbol, Array<Symbol>)

    the list of attribute names



106
107
108
# File 'lib/interaktor.rb', line 106

def success(*attributes)
  success_attributes.concat attributes
end

#success_attributesArray<Symbol>

The list of attributes which are required to be passed in when calling ‘#fail!` from within the interaktor.

Returns:

  • (Array<Symbol>)


48
49
50
# File 'lib/interaktor.rb', line 48

def success_attributes
  @success_attributes ||= []
end