Sha256: 7d70f2ba137aa71bc5a6979af828b79fb9044f50c30c28b2f2e927c859f34782

Contents?: true

Size: 1002 Bytes

Versions: 5

Compression:

Stored size: 1002 Bytes

Contents

module Formulario
  class Field
    class ExceptionalValue < Field
      include Enumerable

      attr_reader :reasons

      def self.for(raw_value, reasons: Undefined)
        new(raw_value, reasons: reasons)
      end

      def exceptional?
        true
      end

      def inspect
        "#{self.class}[#{value} (#{reasons.join(' | ')})]"
      end

      def to_a
        reasons
      end

      def each(&block)
        reasons.each(&block)
      end

      def exceptional_class
        self.class
      end

      private

      def initialize(raw_value, reasons: Undefined)
        the_reasons = reasons == Undefined ? ["is invalid"] : Array(reasons)

        if raw_value.is_a?(self.class)
          new_reasons = raw_value.reasons.concat(Array(the_reasons))
          value       = raw_value.raw_value
        else
          new_reasons = Array(the_reasons)
          value       = raw_value
        end

        @reasons = new_reasons

        super(value)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
formulario-0.1.12 lib/formulario/fields/exceptional_value.rb
formulario-0.1.11 lib/formulario/fields/exceptional_value.rb
formulario-0.1.10 lib/formulario/fields/exceptional_value.rb
formulario-0.1.9 lib/formulario/fields/exceptional_value.rb
formulario-0.1.8 lib/formulario/fields/exceptional_value.rb