lib/eco/api/usecases/use_case.rb in eco-helpers-2.7.20 vs lib/eco/api/usecases/use_case.rb in eco-helpers-2.7.21

- old
+ new

@@ -6,39 +6,50 @@ attr_reader :name, :type, :times_launched attr_reader :options def initialize(name, type:, root:, &block) - raise InvalidType.new("Invalid type for '#{name}'.", type: type, types: self.class.types) unless self.class.valid_type?(type) + invalid_type_err = InvalidType.new( + "Invalid type for '#{name}'.", + type: type, + types: self.class.types + ) + raise invalid_type_err unless self.class.valid_type?(type) - self.root = root - @callback = block - @name = name - @type = type + super() + + self.root = root + @callback = block + @name = name + @type = type @times_launched = 0 end def source_object return nil unless callback_from_loader? + callback_self end def chainer - # TODO: root is a Eco::API::UseCases that will not point to this new case. + # @todo: root is a Eco::API::UseCases that will not point to this new case. # => Moreover, the name and type will be the same as self Eco::API::UseCases::UseCaseChain.new(usecase: self, root: @root) end def root=(value) - raise "Root should be a Eco::API::UseCases. Given: #{value.class}" if !value.is_a?(Eco::API::UseCases) + msg = "Root should be a Eco::API::UseCases. Given: #{value.class}" + raise ArgumentError, msg unless value.is_a?(Eco::API::UseCases) + @root = value end # Actual launch of the usecase # @param io [Eco::API::UseCases::BaseIO] an input/output helper with the necessary parameters. # @param kargs [Hash] hash with symbol keys. - # @option kargs [Eco::API::Common::People::Entries, Eco::API::Organization::People] :input the input data of reference. + # @option kargs [Eco::API::Common::People::Entries, Eco::API::Organization::People] :input + # the input data of reference. # @option kargs [Eco::API::Organization::People] :people object. # @option kargs [Eco::API:Session] :session # @option kargs [Hash] :options hash with symbol keys (i.e. behaviour modifiers, cli trackers, filters, etc.) # @return [Eco::API::UseCases::UseCaseIO] an io with the result in output def launch(io: nil, **kargs) @@ -46,12 +57,14 @@ kargs = params.merge(kargs).merge(usecase: self) UseCaseIO.new(**kargs).tap do |uio| @options = uio.options uio.session.logger.debug("#{self.class}: going to process '#{name}'") + set_session_n_options(uio) if callback_from_loader? - uio.output = callback.call(*uio.params) + + uio.output = callback.call(*uio.params) @times_launched += 1 end end # When it was defined from a Loader class it retrieves the object. @@ -60,52 +73,57 @@ callback_self if callback_from_loader? end protected - def callback - @callback - end + attr_reader :callback def callback_self - eval("self", callback.binding) + eval("self", callback.binding, __FILE__, __LINE__) end def callback_from_loader? callback_self.is_a?(Eco::API::Common::Loaders::Base) end # Set the instance variables `@session` and `@options` # in the use case definition # @note this only works when the use case was defined # via an children class of `Eco::API::Common::Loaders::Base` - def set_session_n_options(uio) + def set_session_n_options(uio) # rubocop:disable Naming/AccessorMethodName return false unless callback_from_loader? - opts = uio.options || @options - sess = uio.session - peo = uio.people - ent = uio.input + + opts = uio.options || @options + sess = uio.session + peo = uio.people + ent = uio.input use_case_self = self callback_self.instance_eval do @session = sess @options = opts + if peo @people = peo - self.singleton_class.attr_reader(:people) unless respond_to?(:people) + singleton_class.attr_reader(:people) unless respond_to?(:people) end if ent # entries/input @input = ent - self.singleton_class.attr_reader(:input) unless respond_to?(:input) + singleton_class.attr_reader(:input) unless respond_to?(:input) + + # Below problematic code... usage has cases where entries = (gets assigned) + # @entries = ent + # singleton_class.attr_reader(:entries) unless respond_to?(:entries) end # `self` is the use case itself (when used the Loader) - next unless self.is_a?(Eco::API::Common::Loaders::CaseBase) + next unless is_a?(Eco::API::Common::Loaders::CaseBase) @usecase = use_case_self - self.singleton_class.attr_reader(:usecase) unless respond_to?(:usecase) + singleton_class.attr_reader(:usecase) unless respond_to?(:usecase) end + true end end end end