lib/eco/api/usecases/base_io.rb in eco-helpers-2.6.4 vs lib/eco/api/usecases/base_io.rb in eco-helpers-2.7.0

- old
+ new

@@ -9,11 +9,11 @@ include Eco::Language::AuxiliarLogger class MissingParameter < StandardError attr_reader :type, :required, :given - def initialize(msg = nil, type: nil, required:, given:) + def initialize(msg = nil, required:, given:, type: nil) @type = type @required = required @given = given msg += " of type '#{type}'" if type msg += " requires an object '#{required}'. Given: #{given}." @@ -21,15 +21,15 @@ end end class << self def input_required?(type) - !valid_type?(type) || [:import, :sync].include?(type) + !valid_type?(type) || %i[import sync].include?(type) end def people_required?(type) - !valid_type?(type) || [:filter, :transform, :sync, :error_handler, :export].include?(type) + !valid_type?(type) || %i[filter transform sync error_handler export].include?(type) end end attr_reader :input, :people, :session, :options attr_reader :type @@ -38,14 +38,18 @@ # @param type [Symbol] a valid type (among `self.class.types`) # @param input [Eco::API::Common::People::Entries, Eco::API::Organization::People] the input data of reference. # @param people [Eco::API::Organization::People] people object. # @param session [Eco::API:Session] # @param options [Hash] hash with symbol keys (i.e. behaviour modifiers, cli trackers, filters, etc.) - def initialize(type: nil, input: nil, people: nil, session:, options: {}, output: nil, validate: true) - self.type = type if type + def initialize( # rubocop:disable Lint/MissingSuper + session:, + type: nil, input: nil, people: nil, + options: {}, output: nil, validate: true + ) + self.type = type if type - if self.type && validate + if self.type && validate # rubocop:disable Style/IfUnlessModifier validate_args(input: input, people: people, session: session, options: options) end @output = output @input = input @@ -62,35 +66,35 @@ # Helper to obtain an `BaseIO` object from any child class. # @return [Eco::API::UseCases::BaseIO] def base kargs = params(keyed: true).merge({ - type: self.type, - output: self.output, + type: type, + output: output }).slice(:type, :input, :people, :session, :options, :output) # :validate <- ? Eco::API::UseCases::BaseIO.new(**kargs) end # @see Eco::API::UseCases::BaseIO#initialize # @return [Eco::API::UseCases::BaseIO] def new(**kargs) default = { - type: self.type, - input: self.input, - people: self.people, - session: self.session, - options: self.options, - output: self.output, + type: type, + input: input, + people: people, + session: session, + options: options, + output: output, validate: true } self.class.new(**default.merge(kargs)) end # Helper to build a `Hash` of symbol keys or `Array` with params to do callbacks. def params(keyed: false, all: false) kargs = {} - kargs.merge!(input: input) if input_required? || all + kargs.merge!(input: input) if input_required? || all kargs.merge!(people: people) if people_required? || all kargs.merge!(session: session, options: options) keyed ? kargs : kargs.values end @@ -106,45 +110,44 @@ # Does the switch from output to result in a new IO object. # @note if there isn't `output` it doesn't do the switch, # provided that it preserves the target parameter # (otherise it would blank it) # @return [Eco::API::UseCases::BaseIO] - def chained(as: self.type) + def chained(as: type) # rubocop:disable Naming/MethodParameterName base.tap do |io_base| next unless io_base.output case as when :import io_base.output_be_input! when :filter io_base.output_be_people! - when :transform, :sync, :export, :error_handler, :other + # when :transform, :sync, :export, :error_handler, :other end end end protected def output_be_input! - @input, @output = output, nil + @input, @output = output, nil # rubocop:disable Style/ParallelAssignment input end def output_be_people! - @people, @output = output, nil + @people, @output = output, nil # rubocop:disable Style/ParallelAssignment people end private def validate_args(input:, people:, session:, options:) - case - when !session.is_a?(Eco::API::Session) + if !session.is_a?(Eco::API::Session) raise MissingParameter.new("UseCase", required: :session, given: session.class) - when input_required? && !input + elsif input_required? && !input raise MissingParameter.new("UseCase", type: type, required: :input, given: input.class) - when people_required? && !people.is_a?(Eco::API::Organization::People) + elsif people_required? && !people.is_a?(Eco::API::Organization::People) raise MissingParameter.new("UseCase", type: type, required: :people, given: people.class) - when !options || (options && !options.is_a?(Hash)) + elsif !options || (options && !options.is_a?(Hash)) raise MissingParameter.new("Use Case options", required: :Hash, given: options.class) end true end