Class: ActiveInteractor::Organizer::InteractorInterface Private
- Inherits:
-
Object
- Object
- ActiveInteractor::Organizer::InteractorInterface
- Defined in:
- lib/active_interactor/organizer/interactor_interface.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
An interface object to facilitate conditionally calling .perform on an interactor
Constant Summary collapse
- CONDITIONAL_FILTERS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Keywords for conditional filters
i[if unless].freeze
Instance Attribute Summary collapse
-
#filters ⇒ Hash{Symbol=>Proc, Symbol}
readonly
private
Conditional options for the interactor class.
-
#interactor_class ⇒ Const
readonly
private
An interactor class.
- #perform_options ⇒ Hash{Symbol=>*} readonly private
Instance Method Summary collapse
-
#initialize(interactor_class, options = {}) ⇒ InteractorInterface
constructor
private
Initialize a new instance of InteractorInterface.
-
#perform(target, context, fail_on_error = false, perform_options = {}) ⇒ Class
private
Call the #interactor_class .perform or .perform! method if all conditions in #filters are properly met.
Constructor Details
#initialize(interactor_class, options = {}) ⇒ InteractorInterface
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a new instance of ActiveInteractor::Organizer::InteractorInterface
42 43 44 45 46 |
# File 'lib/active_interactor/organizer/interactor_interface.rb', line 42 def initialize(interactor_class, = {}) @interactor_class = interactor_class.to_s.classify.safe_constantize @filters = .select { |key, _value| CONDITIONAL_FILTERS.include?(key) } = .reject { |key, _value| CONDITIONAL_FILTERS.include?(key) } end |
Instance Attribute Details
#filters ⇒ Hash{Symbol=>Proc, Symbol} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Conditional options for the interactor class
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_interactor/organizer/interactor_interface.rb', line 29 class InteractorInterface attr_reader :filters, :interactor_class, :perform_options # Keywords for conditional filters # @return [Array<Symbol>] CONDITIONAL_FILTERS = i[if unless].freeze # Initialize a new instance of {InteractorInterface} # # @param interactor_class [Const] an {ActiveInteractor::Base interactor} class # @param options [Hash] options to use for the {ActiveInteractor::Base interactor's} # {Interactor::Perform::ClassMethods#perform .perform}. See {Interactor::Perform::Options}. # @return [InteractorInterface] a new instance of {InteractorInterface} def initialize(interactor_class, = {}) @interactor_class = interactor_class.to_s.classify.safe_constantize @filters = .select { |key, _value| CONDITIONAL_FILTERS.include?(key) } = .reject { |key, _value| CONDITIONAL_FILTERS.include?(key) } end # Call the {#interactor_class} {Interactor::Perform::ClassMethods#perform .perform} or # {Interactor::Perform::ClassMethods#perform! .perform!} method if all conditions in {#filters} are properly met. # # @param target [Class] the calling {Base organizer} instance # @param context [Class] an instance of {Context::Base context} # @param fail_on_error [Boolean] if `true` {Interactor::Perform::ClassMethods#perform! .perform!} will be called # on the {#interactor_class} other wise {Interactor::Perform::ClassMethods#perform .perform} will be called. # @param perform_options [Hash] additional {Interactor::Perform::Options} to merge with {#perform_options} # @raise [Error::ContextFailure] if `fail_on_error` is `true` and the {#interactor_class} # {Context::Status#fail! fails} its {Context::Base context}. # @return [Class] an instance of {Context::Base context} def perform(target, context, fail_on_error = false, = {}) return if check_conditionals(target, filters[:if]) == false return if check_conditionals(target, filters[:unless]) == true method = fail_on_error ? :perform! : :perform = self..merge() interactor_class.send(method, context, ) end private def check_conditionals(target, filter) return unless filter return target.send(filter) if filter.is_a?(Symbol) return target.instance_exec(&filter) if filter.is_a?(Proc) end end |
#interactor_class ⇒ Const (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
An interactor class
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_interactor/organizer/interactor_interface.rb', line 29 class InteractorInterface attr_reader :filters, :interactor_class, :perform_options # Keywords for conditional filters # @return [Array<Symbol>] CONDITIONAL_FILTERS = i[if unless].freeze # Initialize a new instance of {InteractorInterface} # # @param interactor_class [Const] an {ActiveInteractor::Base interactor} class # @param options [Hash] options to use for the {ActiveInteractor::Base interactor's} # {Interactor::Perform::ClassMethods#perform .perform}. See {Interactor::Perform::Options}. # @return [InteractorInterface] a new instance of {InteractorInterface} def initialize(interactor_class, = {}) @interactor_class = interactor_class.to_s.classify.safe_constantize @filters = .select { |key, _value| CONDITIONAL_FILTERS.include?(key) } = .reject { |key, _value| CONDITIONAL_FILTERS.include?(key) } end # Call the {#interactor_class} {Interactor::Perform::ClassMethods#perform .perform} or # {Interactor::Perform::ClassMethods#perform! .perform!} method if all conditions in {#filters} are properly met. # # @param target [Class] the calling {Base organizer} instance # @param context [Class] an instance of {Context::Base context} # @param fail_on_error [Boolean] if `true` {Interactor::Perform::ClassMethods#perform! .perform!} will be called # on the {#interactor_class} other wise {Interactor::Perform::ClassMethods#perform .perform} will be called. # @param perform_options [Hash] additional {Interactor::Perform::Options} to merge with {#perform_options} # @raise [Error::ContextFailure] if `fail_on_error` is `true` and the {#interactor_class} # {Context::Status#fail! fails} its {Context::Base context}. # @return [Class] an instance of {Context::Base context} def perform(target, context, fail_on_error = false, = {}) return if check_conditionals(target, filters[:if]) == false return if check_conditionals(target, filters[:unless]) == true method = fail_on_error ? :perform! : :perform = self..merge() interactor_class.send(method, context, ) end private def check_conditionals(target, filter) return unless filter return target.send(filter) if filter.is_a?(Symbol) return target.instance_exec(&filter) if filter.is_a?(Proc) end end |
#perform_options ⇒ Hash{Symbol=>*} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_interactor/organizer/interactor_interface.rb', line 29 class InteractorInterface attr_reader :filters, :interactor_class, :perform_options # Keywords for conditional filters # @return [Array<Symbol>] CONDITIONAL_FILTERS = i[if unless].freeze # Initialize a new instance of {InteractorInterface} # # @param interactor_class [Const] an {ActiveInteractor::Base interactor} class # @param options [Hash] options to use for the {ActiveInteractor::Base interactor's} # {Interactor::Perform::ClassMethods#perform .perform}. See {Interactor::Perform::Options}. # @return [InteractorInterface] a new instance of {InteractorInterface} def initialize(interactor_class, = {}) @interactor_class = interactor_class.to_s.classify.safe_constantize @filters = .select { |key, _value| CONDITIONAL_FILTERS.include?(key) } = .reject { |key, _value| CONDITIONAL_FILTERS.include?(key) } end # Call the {#interactor_class} {Interactor::Perform::ClassMethods#perform .perform} or # {Interactor::Perform::ClassMethods#perform! .perform!} method if all conditions in {#filters} are properly met. # # @param target [Class] the calling {Base organizer} instance # @param context [Class] an instance of {Context::Base context} # @param fail_on_error [Boolean] if `true` {Interactor::Perform::ClassMethods#perform! .perform!} will be called # on the {#interactor_class} other wise {Interactor::Perform::ClassMethods#perform .perform} will be called. # @param perform_options [Hash] additional {Interactor::Perform::Options} to merge with {#perform_options} # @raise [Error::ContextFailure] if `fail_on_error` is `true` and the {#interactor_class} # {Context::Status#fail! fails} its {Context::Base context}. # @return [Class] an instance of {Context::Base context} def perform(target, context, fail_on_error = false, = {}) return if check_conditionals(target, filters[:if]) == false return if check_conditionals(target, filters[:unless]) == true method = fail_on_error ? :perform! : :perform = self..merge() interactor_class.send(method, context, ) end private def check_conditionals(target, filter) return unless filter return target.send(filter) if filter.is_a?(Symbol) return target.instance_exec(&filter) if filter.is_a?(Proc) end end |
Instance Method Details
#perform(target, context, fail_on_error = false, perform_options = {}) ⇒ Class
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Call the #interactor_class .perform or .perform! method if all conditions in #filters are properly met.
59 60 61 62 63 64 65 66 |
# File 'lib/active_interactor/organizer/interactor_interface.rb', line 59 def perform(target, context, fail_on_error = false, = {}) return if check_conditionals(target, filters[:if]) == false return if check_conditionals(target, filters[:unless]) == true method = fail_on_error ? :perform! : :perform = self..merge() interactor_class.send(method, context, ) end |