Module: Interaktor::Callable::ClassMethods
- Defined in:
- lib/interaktor/callable.rb
Instance Method Summary collapse
-
#call(context = {}) ⇒ Interaktor::Context
Invoke an Interaktor.
-
#call!(context = {}) ⇒ Interaktor::Context
Invoke an Interaktor.
-
#failure(*attributes, **options) ⇒ void
A DSL method for documenting required interaktor failure attributes.
-
#failure_attributes ⇒ Array<Symbol>
The list of attributes which are required to be passed in when calling
#fail!from within the interaktor. -
#input_attributes ⇒ Array<Symbol>
A list of attributes which could be passed when calling the interaktor.
-
#optional(*attributes, **options) ⇒ void
A DSL method for documenting optional interaktor attributes.
-
#optional_attributes ⇒ Array<Symbol>
The list of attributes which are NOT required to be passed in when calling the interaktor.
-
#optional_defaults ⇒ Array<Symbol>
A list of optional attributes and their default values.
-
#required(*attributes, **options) ⇒ void
A DSL method for documenting required interaktor attributes.
-
#required_attributes ⇒ Array<Symbol>
The list of attributes which are required to be passed in when calling the interaktor.
-
#success(*attributes, **options) ⇒ void
A DSL method for documenting required interaktor success attributes.
-
#success_attributes ⇒ Array<Symbol>
The list of attributes which are required to be passed in when calling
#fail!from within the interaktor.
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.
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.
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.
115 116 117 118 119 120 121 122 |
# File 'lib/interaktor/callable.rb', line 115 def failure(*attributes, **) failure_attributes.concat attributes attributes.each do |attribute| # Handle options raise Interaktor::Error::UnknownOptionError.new(self.class.to_s, ) if .any? end end |
#failure_attributes ⇒ Array<Symbol>
The list of attributes which are required to be passed in when calling #fail! from within the interaktor.
45 46 47 |
# File 'lib/interaktor/callable.rb', line 45 def failure_attributes @failure_attributes ||= [] end |
#input_attributes ⇒ Array<Symbol>
A list of attributes which could be passed when calling the interaktor.
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.
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, **) 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] = [:default] if [:default] .delete(:default) raise Interaktor::Error::UnknownOptionError.new(self.class.to_s, ) if .any? end end |
#optional_attributes ⇒ Array<Symbol>
The list of attributes which are NOT required to be passed in when calling the interaktor.
23 24 25 |
# File 'lib/interaktor/callable.rb', line 23 def optional_attributes @optional_attributes ||= [] end |
#optional_defaults ⇒ Array<Symbol>
A list of optional attributes and their default values.
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.
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, **) 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, ) if .any? end end |
#required_attributes ⇒ Array<Symbol>
The list of attributes which are required to be passed in when calling the interaktor.
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.
130 131 132 133 134 135 136 137 |
# File 'lib/interaktor/callable.rb', line 130 def success(*attributes, **) success_attributes.concat attributes attributes.each do |attribute| # Handle options raise Interaktor::Error::UnknownOptionError.new(self.class.to_s, ) if .any? end end |
#success_attributes ⇒ Array<Symbol>
The list of attributes which are required to be passed in when calling #fail! from within the interaktor.
53 54 55 |
# File 'lib/interaktor/callable.rb', line 53 def success_attributes @success_attributes ||= [] end |