Sha256: e9115d6edebc526ba584f5dfe52f9eacd7aa12ded709b3ae6c85c6b9c9f9072f

Contents?: true

Size: 1.66 KB

Versions: 25

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

25 entries across 25 versions & 1 rubygems

Version Path
foobara-0.0.26 projects/value/src/processor/multi.rb
foobara-0.0.25 projects/value/src/processor/multi.rb
foobara-0.0.24 projects/value/src/processor/multi.rb
foobara-0.0.23 projects/value/src/processor/multi.rb
foobara-0.0.22 projects/value/src/processor/multi.rb
foobara-0.0.21 projects/value/src/processor/multi.rb
foobara-0.0.20 projects/value/src/processor/multi.rb
foobara-0.0.19 projects/value/src/processor/multi.rb
foobara-0.0.18 projects/value/src/processor/multi.rb
foobara-0.0.17 projects/value/src/processor/multi.rb
foobara-0.0.16 projects/value/src/processor/multi.rb
foobara-0.0.15 projects/value/src/processor/multi.rb
foobara-0.0.14 projects/value/src/processor/multi.rb
foobara-0.0.13 projects/value/src/processor/multi.rb
foobara-0.0.12 projects/value/src/processor/multi.rb
foobara-0.0.11 projects/value/src/processor/multi.rb
foobara-0.0.10 projects/value/src/processor/multi.rb
foobara-0.0.9 projects/value/src/processor/multi.rb
foobara-0.0.8 projects/value/src/processor/multi.rb
foobara-0.0.7 projects/value/src/processor/multi.rb