Module: Interaktor::Callable::ClassMethods

Defined in:
lib/interaktor/callable.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. Interaktor failures will not raise an exception.

Parameters:

  • context (Hash, Interaktor::Context) (defaults to: {})

    the context object as a hash with attributes or an already-built context

Returns:



146
147
148
# File 'lib/interaktor/callable.rb', line 146

def call(context = {})
  execute(context, false)
end

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

Invoke an Interaktor. This method behaves identically to #call, but if the interaktor is failed, Interaktor::Failure is raised.

Parameters:

  • context (Hash, Interaktor::Context) (defaults to: {})

    the context object as a hash with attributes or an already-built context

Returns:



159
160
161
# File 'lib/interaktor/callable.rb', line 159

def call!(context = {})
  execute(context, true)
end

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

  • options (Hash)


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

def failure(*attributes, **options)
  failure_attributes.concat attributes

  attributes.each do |attribute|
    # Handle options
    raise Interaktor::Error::UnknownOptionError.new(self.class.to_s, options) if options.any?
  end
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>)


45
46
47
# File 'lib/interaktor/callable.rb', line 45

def failure_attributes
  @failure_attributes ||= []
end

#input_attributesArray<Symbol>

A list of attributes which could be passed when calling the interaktor.

Returns:

  • (Array<Symbol>)


37
38
39
# File 'lib/interaktor/callable.rb', line 37

def input_attributes
  required_attributes + optional_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)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/interaktor/callable.rb', line 85

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 Interaktor::Error::DisallowedAttributeAssignmentError.new(self.class.to_s, [attribute])
      end

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

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

    raise Interaktor::Error::UnknownOptionError.new(self.class.to_s, options) 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>)


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

def optional_attributes
  @optional_attributes ||= []
end

#optional_defaultsArray<Symbol>

A list of optional attributes and their default values.

Returns:

  • (Array<Symbol>)


30
31
32
# File 'lib/interaktor/callable.rb', line 30

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)


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

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 Interaktor::Error::UnknownOptionError.new(self.class.to_s, options) 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>)


15
16
17
# File 'lib/interaktor/callable.rb', line 15

def required_attributes
  @required_attributes ||= []
end

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

  • options (Hash)


130
131
132
133
134
135
136
137
# File 'lib/interaktor/callable.rb', line 130

def success(*attributes, **options)
  success_attributes.concat attributes

  attributes.each do |attribute|
    # Handle options
    raise Interaktor::Error::UnknownOptionError.new(self.class.to_s, options) if options.any?
  end
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>)


53
54
55
# File 'lib/interaktor/callable.rb', line 53

def success_attributes
  @success_attributes ||= []
end