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:



65
66
67
# File 'lib/interaktor/callable.rb', line 65

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:



77
78
79
# File 'lib/interaktor/callable.rb', line 77

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

#failure(&block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/interaktor/callable.rb', line 31

def failure(&block)
  raise "Failure block already defined" if defined?(self::FailureAttributesModel)

  # Define self::FailureAttributesModel
  Class.new(Interaktor::Attributes, &block).tap do |klass|
    klass.define_singleton_method(:inspect) { name.to_s }
    klass.define_singleton_method(:to_s) { inspect }

    const_set(:FailureAttributesModel, klass)

    klass.check_for_disallowed_attribute_names!
  end
end

#input(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/interaktor/callable.rb', line 13

def input(&block)
  raise "Input block already defined" if defined?(self::InputAttributesModel)

  # Define self::InputAttributesModel
  Class.new(Interaktor::Attributes, &block).tap do |klass|
    klass.define_singleton_method(:inspect) { name.to_s }
    klass.define_singleton_method(:to_s) { inspect }

    const_set(:InputAttributesModel, klass)

    klass.check_for_disallowed_attribute_names!

    klass.attribute_names.each do |name|
      define_method(name) { @interaction.send(name) }
    end
  end
end

#success(&block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/interaktor/callable.rb', line 45

def success(&block)
  raise "Success block already defined" if defined?(self::SuccessAttributesModel)

  # Define self::SuccessAttributesModel
  Class.new(Interaktor::Attributes, &block).tap do |klass|
    klass.define_singleton_method(:inspect) { name.to_s }
    klass.define_singleton_method(:to_s) { inspect }

    const_set(:SuccessAttributesModel, klass)

    klass.check_for_disallowed_attribute_names!
  end
end