Module: Interaktor::ClassMethods
- Defined in:
- lib/interaktor.rb
Instance Method Summary collapse
-
#call(context = {}) ⇒ Interaktor::Context
Invoke an Interaktor.
-
#call!(context = {}) ⇒ Interaktor::Context
Invoke an Interaktor.
-
#failure(*attributes) ⇒ 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. -
#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) ⇒ 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.
with attributes or an already-built context
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
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.
120 121 122 |
# File 'lib/interaktor.rb', line 120 def failure(*attributes) failure_attributes.concat attributes end |
#failure_attributes ⇒ Array<Symbol>
The list of attributes which are required to be passed in when calling #fail! from within the interaktor.
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.
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, **) 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] = [:default] if [:default] .delete(:default) raise "Unknown option(s): #{.keys.join(", ")}" if .any? end end |
#optional_attributes ⇒ Array<Symbol>
The list of attributes which are NOT required to be passed in when calling the interaktor.
32 33 34 |
# File 'lib/interaktor.rb', line 32 def optional_attributes @optional_attributes ||= [] end |
#optional_defaults ⇒ Array<Symbol>
A list of optional attributes and their default values.
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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/interaktor.rb', line 65 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 "Unknown option(s): #{.keys.join(", ")}" if .any? end end |
#required_attributes ⇒ Array<Symbol>
The list of attributes which are required to be passed in when calling the interaktor.
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.
129 130 131 |
# File 'lib/interaktor.rb', line 129 def success(*attributes) success_attributes.concat attributes end |
#success_attributes ⇒ Array<Symbol>
The list of attributes which are required to be passed in when calling #fail! from within the interaktor.
55 56 57 |
# File 'lib/interaktor.rb', line 55 def success_attributes @success_attributes ||= [] end |