Sha256: ee3b894b6f757a8130dc0dbdc008302ede625f1f32a63b029f730a02f251fb41

Contents?: true

Size: 958 Bytes

Versions: 6

Compression:

Stored size: 958 Bytes

Contents

require 'forwardable'

module Kafo
  module DataTypes
    class Optional < DataType
      extend Forwardable
      def_delegators :@inner_type, :condition_value, :dump_default, :multivalued?, :typecast
      attr_reader :inner_type, :inner_value

      def initialize(inner_type_or_value)
        @inner_type = DataType.new_from_string(inner_type_or_value)
        @inner_value = nil
      rescue ConfigurationException
        @inner_type = nil
        @inner_value = inner_type_or_value
      end

      def to_s
        if @inner_type
          "#{@inner_type} or undef"
        else
          "#{@inner_value.inspect} or undef"
        end
      end

      def valid?(input, errors = [])
        return true if input.nil?
        return true if @inner_type && @inner_type.valid?(input, errors)
        return true if @inner_value && @inner_value == input
        return false
      end
    end

    DataType.register_type('Optional', Optional)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kafo-7.6.0 lib/kafo/data_types/optional.rb
kafo-7.5.1 lib/kafo/data_types/optional.rb
kafo-7.5.0 lib/kafo/data_types/optional.rb
kafo-7.4.0 lib/kafo/data_types/optional.rb
kafo-7.3.0 lib/kafo/data_types/optional.rb
kafo-7.2.0 lib/kafo/data_types/optional.rb