Sha256: 9e6eeff32e2b249d2021d809515045e9bbcea9910d4ba7cc7076ad3981989c03

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module Foobara
  module Value
    class Processor
      class Multi < Processor
        attr_accessor :processors, :prioritize

        class << self
          def requires_declaration_data?
            false
          end
        end

        def initialize(*args, processors: [], prioritize: true)
          self.prioritize = prioritize
          self.processors = prioritize ? processors.sort_by(&:priority) : processors
          super(*args)
        end

        def processor_names
          processors.map(&:name)
        end

        def error_classes
          normalize_error_classes([*super, *processors.map(&:error_classes).flatten])
        end

        def applicable?(value)
          super || processors.any? { |processor| processor.applicable?(value) }
        end

        def possible_errors
          return @possible_errors if @possible_errors

          h = super.to_h do |possible_error|
            [possible_error.key.to_s, possible_error]
          end

          processors.map(&:possible_errors).flatten.each do |possible_error|
            h[possible_error.key.to_s] = possible_error
          end

          # TODO: change this back to a hash
          @possible_errors = h.values
        end

        def register(processor)
          self.processors = [*processors, processor].sort_by(&:priority)
        end

        private

        def normalize_error_classes(error_classes)
          cannot_cast_errors, others = error_classes.partition do |c|
            # TODO: some kind of dependency issue here??
            c == Foobara::Value::Processor::Casting::CannotCastError
          end

          [*cannot_cast_errors.uniq, *others]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
foobara-0.0.1 projects/value/src/processor/multi.rb