Sha256: 9fe845f1da28887aec882d1a9374a784833f38d68f2c59ddecda33ceaf76a076

Contents?: true

Size: 946 Bytes

Versions: 8

Compression:

Stored size: 946 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

      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

8 entries across 8 versions & 1 rubygems

Version Path
formulario-0.1.6 lib/formulario/fields/exceptional_value.rb
formulario-0.1.5 lib/formulario/fields/exceptional_value.rb
formulario-0.1.4 lib/formulario/fields/exceptional_value.rb
formulario-0.1.3 lib/formulario/fields/exceptional_value.rb
formulario-0.1.2 lib/formulario/fields/exceptional_value.rb
formulario-0.1.1b lib/formulario/fields/exceptional_value.rb
formulario-0.1.1 lib/formulario/fields/exceptional_value.rb
formulario-0.1.0 lib/formulario/fields/exceptional_value.rb