Sha256: e9115d6edebc526ba584f5dfe52f9eacd7aa12ded709b3ae6c85c6b9c9f9072f

Contents?: true

Size: 1.66 KB

Versions: 26

Compression:

Stored size: 1.66 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(*, processors: [], prioritize: true)
          self.prioritize = prioritize
          self.processors = prioritize ? processors.sort_by(&:priority) : processors
          super(*)
        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

26 entries across 26 versions & 1 rubygems

Version Path
foobara-0.0.7 projects/value/src/processor/multi.rb
foobara-0.0.6 projects/value/src/processor/multi.rb
foobara-0.0.5 projects/value/src/processor/multi.rb
foobara-0.0.4 projects/value/src/processor/multi.rb
foobara-0.0.3 projects/value/src/processor/multi.rb
foobara-0.0.2 projects/value/src/processor/multi.rb