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:



140
141
142
143
144
145
# File 'lib/interaktor.rb', line 140

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

  new(context).tap(&:run).instance_variable_get(:@context)
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:



157
158
159
160
161
162
# File 'lib/interaktor.rb', line 157

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

  new(context).tap(&:run!).instance_variable_get(:@context)
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



120
121
122
# File 'lib/interaktor.rb', line 120

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>)


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

def failure_attributes
  @failure_attributes ||= []
end

#optional(*attributes, **options) ⇒ 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

  • options (Hash)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/interaktor.rb', line 87

def optional(*attributes, **options)
  optional_attributes.concat attributes

  attributes.each do |attribute|
    # Define getter
    define_method(attribute) { @context.send(attribute) }

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

      @context.send("#{attribute}=".to_sym, value)
    end

    # Handle options
    optional_defaults[attribute] = options[:default] if options[:default]
    options.delete(:default)

    raise "Unknown option(s): #{options.keys.join(", ")}" if options.any?
  end
end

#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

#optional_defaultsArray<Symbol>

A list of optional attributes and their default values.

Returns:

  • (Array<Symbol>)


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

def optional_defaults
  @optional_defaults ||= {}
end

#required(*attributes, **options) ⇒ 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

  • options (Hash)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/interaktor.rb', line 65

def required(*attributes, **options)
  required_attributes.concat attributes

  attributes.each do |attribute|
    # Define getter
    define_method(attribute) { @context.send(attribute) }

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

    raise "Unknown option(s): #{options.keys.join(", ")}" if options.any?
  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



129
130
131
# File 'lib/interaktor.rb', line 129

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>)


55
56
57
# File 'lib/interaktor.rb', line 55

def success_attributes
  @success_attributes ||= []
end