Module: Interaktor::Callable::ClassMethods

Defined in:
lib/interaktor/callable.rb

Instance Method Summary collapse

Instance Method Details

#call(args = {}) ⇒ Interaktor::Interaction

Invoke an Interaktor. This is the primary public API method to an interaktor. Interaktor failures will not raise an exception.

Parameters:

Returns:



286
287
288
# File 'lib/interaktor/callable.rb', line 286

def call(args = {})
  execute(args, raise_exception: false)
end

#call!(args = {}) ⇒ Interaktor::Interaction

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

Parameters:

Returns:



298
299
300
# File 'lib/interaktor/callable.rb', line 298

def call!(args = {})
  execute(args, raise_exception: true)
end

#failure(schema = nil) { ... } ⇒ Object

Parameters:

  • schema (Dry::Schema::Params, nil) (defaults to: nil)

    a predefined schema object

Yields:

  • a new Dry::Schema::Params definition block



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/interaktor/callable.rb', line 183

def failure(schema = nil, &block)
  raise "No schema or schema definition block provided to interaktor failure method." if schema.nil? && !block

  raise "Provided both a schema and a schema definition block for interaktor failure method." if schema && block

  if schema
    raise "Provided argument is not a Dry::Schema::Params object." unless schema.is_a?(Dry::Schema::Params)

    @failure_schema = schema
  elsif block
    @failure_schema = Dry::Schema.Params { instance_eval(&block) }
  end
end

#failure_attributesArray<Symbol>

The complete list of failure attributes.

Returns:

  • (Array<Symbol>)


154
155
156
# File 'lib/interaktor/callable.rb', line 154

def failure_attributes
  required_failure_attributes + optional_failure_attributes
end

#failure_schemaDry::Schema::Params

Get the failure attribute schema. Fall back to an empty schema with a configuration that will deny ALL provided attributes - not defining an failure schema should mean the interaktor has no failure attributes.

Returns:

  • (Dry::Schema::Params)


163
164
165
# File 'lib/interaktor/callable.rb', line 163

def failure_schema
  @failure_schema || Dry::Schema.Params
end

#input(schema = nil) { ... } ⇒ Object

Parameters:

  • schema (Dry::Schema::Params, nil) (defaults to: nil)

    a predefined schema object

Yields:

  • a new Dry::Schema::Params definition block



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

def input(schema = nil, &block)
  raise "No schema or schema definition block provided to interaktor input." if schema.nil? && !block

  raise "Provided both a schema and a schema definition block for interaktor input." if schema && block

  if schema
    raise "Provided argument is not a Dry::Schema::Params object." unless schema.is_a?(Dry::Schema::Params)

    @input_schema = schema
  elsif block
    @input_schema = Dry::Schema.Params { instance_eval(&block) }
  end

  # define the getters and setters for the input attributes
  @input_schema.key_map.keys.each do |key| # rubocop:disable Style/HashEachMethods
    attribute_name = key.id

    # Define getter
    define_method(attribute_name) { @interaction.send(attribute_name) }

    # Define setter
    define_method(:"#{attribute_name}=") do |value|
      @interaction.send(:"#{attribute_name}=", value)
    end
  end
end

#input_attributesArray<Symbol>

The complete list of input attributes.

Returns:

  • (Array<Symbol>)


58
59
60
# File 'lib/interaktor/callable.rb', line 58

def input_attributes
  required_input_attributes + optional_input_attributes
end

#input_schemaDry::Schema::Params

Get the input attribute schema. Fall back to an empty schema with a configuration that will deny ALL provided attributes - not defining an input schema should mean the interaktor has no input attributes.

Returns:

  • (Dry::Schema::Params)


67
68
69
# File 'lib/interaktor/callable.rb', line 67

def input_schema
  @input_schema || Dry::Schema.Params
end

#optional_failure_attributesArray<Symbol>

The list of attributes which are not required to be provided when failing the interaktor.

Returns:

  • (Array<Symbol>)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/interaktor/callable.rb', line 130

def optional_failure_attributes
  # Adding an optional attribute with NO predicates with Dry::Schema is
  # sort of a "nothing statement" - the schema can sort of ignore it. The
  # problem is that the optional-with-no-predicate key is not included in
  # the #info results, so we need to find an list of keys elsewhere, find
  # the ones that are listed there but not in the #info results, and find
  # the difference. The result are the keys that are omitted from the #info
  # result because they are optional and have no predicates.
  #
  # See https://github.com/dry-rb/dry-schema/issues/347
  @optional_failure_attributes ||= begin
    attributes_in_info = failure_schema.info[:keys].keys
    all_attributes = failure_schema.key_map.keys.map(&:id)
    optional_attributes_by_exclusion = all_attributes - attributes_in_info

    explicitly_optional_attributes = failure_schema.info[:keys].reject { |_, info| info[:required] }.keys

    explicitly_optional_attributes + optional_attributes_by_exclusion
  end
end

#optional_input_attributesArray<Symbol>

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

Returns:

  • (Array<Symbol>)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/interaktor/callable.rb', line 34

def optional_input_attributes
  # Adding an optional attribute with NO predicates with Dry::Schema is
  # sort of a "nothing statement" - the schema can sort of ignore it. The
  # problem is that the optional-with-no-predicate key is not included in
  # the #info results, so we need to find an list of keys elsewhere, find
  # the ones that are listed there but not in the #info results, and find
  # the difference. The result are the keys that are omitted from the #info
  # result because they are optional and have no predicates.
  #
  # See https://github.com/dry-rb/dry-schema/issues/347
  @optional_input_attributes ||= begin
    attributes_in_info = input_schema.info[:keys].keys
    all_attributes = input_schema.key_map.keys.map(&:id)
    optional_attributes_by_exclusion = all_attributes - attributes_in_info

    explicitly_optional_attributes = input_schema.info[:keys].reject { |_, info| info[:required] }.keys

    explicitly_optional_attributes + optional_attributes_by_exclusion
  end
end

#optional_success_attributesArray<Symbol>

The list of attributes which are not required to be provided when failing the interaktor.

Returns:

  • (Array<Symbol>)


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/interaktor/callable.rb', line 215

def optional_success_attributes
  # Adding an optional attribute with NO predicates with Dry::Schema is
  # sort of a "nothing statement" - the schema can sort of ignore it. The
  # problem is that the optional-with-no-predicate key is not included in
  # the #info results, so we need to find an list of keys elsewhere, find
  # the ones that are listed there but not in the #info results, and find
  # the difference. The result are the keys that are omitted from the #info
  # result because they are optional and have no predicates.
  #
  # See https://github.com/dry-rb/dry-schema/issues/347
  @optional_success_attributes ||= begin
    attributes_in_info = success_schema.info[:keys].keys
    all_attributes = success_schema.key_map.keys.map(&:id)
    optional_attributes_by_exclusion = all_attributes - attributes_in_info

    explicitly_optional_attributes = success_schema.info[:keys].reject { |_, info| info[:required] }.keys

    explicitly_optional_attributes + optional_attributes_by_exclusion
  end
end

#required_failure_attributesArray<Symbol>

The list of attributes which are required to be provided when failing the interaktor.

Returns:

  • (Array<Symbol>)


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

def required_failure_attributes
  @required_failure_attributes ||= failure_schema.info[:keys]
    .select { |_, info| info[:required] }
    .keys
end

#required_input_attributesArray<Symbol>

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

Returns:

  • (Array<Symbol>)


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

def required_input_attributes
  @required_input_attributes ||= input_schema
    .info[:keys]
    .select { |_, info| info[:required] }
    .keys
end

#required_success_attributesArray<Symbol>

The list of attributes which are required to be provided when the interaktor succeeds.

Returns:

  • (Array<Symbol>)


205
206
207
208
209
# File 'lib/interaktor/callable.rb', line 205

def required_success_attributes
  @required_success_attributes ||= success_schema.info[:keys]
    .select { |_, info| info[:required] }
    .keys
end

#success(schema = nil) { ... } ⇒ Object

Parameters:

  • schema (Dry::Schema::Params, nil) (defaults to: nil)

    a predefined schema object

Yields:

  • a new Dry::Schema::Params definition block



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/interaktor/callable.rb', line 266

def success(schema = nil, &block)
  raise "No schema or schema definition block provided to interaktor success method." if schema.nil? && !block

  raise "Provided both a schema and a schema definition block for interaktor success method." if schema && block

  if schema
    raise "Provided argument is not a Dry::Schema::Params object." unless schema.is_a?(Dry::Schema::Params)

    @success_schema = schema
  elsif block
    @success_schema = Dry::Schema.Params { instance_eval(&block) }
  end
end

#success_attributesArray<Symbol>

The complete list of success attributes.

Returns:

  • (Array<Symbol>)


239
240
241
# File 'lib/interaktor/callable.rb', line 239

def success_attributes
  required_success_attributes + optional_success_attributes
end

#success_schemaDry::Schema::Params

Get the success attribute schema. Fall back to an empty schema with a configuration that will deny ALL provided attributes - not defining an success schema should mean the interaktor has no success attributes.

Returns:

  • (Dry::Schema::Params)


248
249
250
# File 'lib/interaktor/callable.rb', line 248

def success_schema
  @success_schema || Dry::Schema.Params
end

#validate_failure_schema(args) ⇒ void

This method returns an undefined value.

Parameters:

  • args (Hash)


170
171
172
173
174
175
176
177
178
179
# File 'lib/interaktor/callable.rb', line 170

def validate_failure_schema(args)
  return if !failure_schema

  if (errors = failure_schema.call(args).errors).any?
    raise Interaktor::Error::AttributeSchemaValidationError.new(
      self,
      errors.to_h
    )
  end
end

#validate_input_schema(args) ⇒ Object

Parameters:

  • args (Hash)


72
73
74
75
76
77
78
79
80
81
# File 'lib/interaktor/callable.rb', line 72

def validate_input_schema(args)
  return if !input_schema

  if (errors = input_schema.call(args).errors).any?
    raise Interaktor::Error::AttributeSchemaValidationError.new(
      self,
      errors.to_h
    )
  end
end

#validate_success_schema(args) ⇒ Object

Parameters:

  • args (Hash)


253
254
255
256
257
258
259
260
261
262
# File 'lib/interaktor/callable.rb', line 253

def validate_success_schema(args)
  return if !success_schema

  if (errors = success_schema.call(args).errors).any?
    raise Interaktor::Error::AttributeSchemaValidationError.new(
      self,
      errors.to_h
    )
  end
end