Class: Kafo::DataTypes::Optional

Inherits:
Kafo::DataType show all
Extended by:
Forwardable
Defined in:
lib/kafo/data_types/optional.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Kafo::DataType

#condition_value, #dump_default, #multivalued?, new_from_string, parse_hash, register_type, split_arguments, #typecast, types, unregister_type

Constructor Details

#initialize(inner_type_or_value) ⇒ Optional

Returns a new instance of Optional.



10
11
12
13
14
15
16
17
18
# File 'lib/kafo/data_types/optional.rb', line 10

def initialize(inner_type_or_value)
  begin
    @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
end

Instance Attribute Details

#inner_typeObject (readonly)

Returns the value of attribute inner_type.



8
9
10
# File 'lib/kafo/data_types/optional.rb', line 8

def inner_type
  @inner_type
end

#inner_valueObject (readonly)

Returns the value of attribute inner_value.



8
9
10
# File 'lib/kafo/data_types/optional.rb', line 8

def inner_value
  @inner_value
end

Instance Method Details

#to_sObject



20
21
22
23
24
25
26
# File 'lib/kafo/data_types/optional.rb', line 20

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

#valid?(input, errors = []) ⇒ Boolean

Returns:



28
29
30
31
32
33
# File 'lib/kafo/data_types/optional.rb', line 28

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