lib/eco/api/usecases/use_case.rb in eco-helpers-2.4.8 vs lib/eco/api/usecases/use_case.rb in eco-helpers-2.4.9

- old
+ new

@@ -15,10 +15,15 @@ @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. # => Moreover, the name and type will be the same as self Eco::API::UseCases::UseCaseChain.new(usecase: self, root: @root) end @@ -40,10 +45,11 @@ 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(session: uio.session, options: uio.options) if callback_from_loader? uio.output = @callback.call(*uio.params) @times_launched += 1 end end @@ -51,10 +57,30 @@ def callback @callback end - end + def callback_self + eval("self", @callback.binding) + 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(session:, options: @options) + return false unless callback_from_loader? + callback_self.instance_eval do + # `self` is the use case itself (when used the Loader) + @session = session + @options = options + end + true + end + end end end end