Class: Kafo::DataTypes::Enum

Inherits:
Kafo::DataType show all
Defined in:
lib/kafo/data_types/enum.rb

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(*permitted) ⇒ Enum

Returns a new instance of Enum.



4
5
6
# File 'lib/kafo/data_types/enum.rb', line 4

def initialize(*permitted)
  @permitted = permitted
end

Instance Method Details

#to_sObject



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

def to_s
  @permitted.map(&:inspect).join(' or ')
end

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

Returns:



12
13
14
15
16
17
18
19
20
# File 'lib/kafo/data_types/enum.rb', line 12

def valid?(input, errors = [])
  unless input.is_a?(::String)
    errors << "#{input.inspect} is not a valid string"
    return false
  end

  errors << "#{input} must be one of #{@permitted.join(', ')}" unless @permitted.include?(input)
  return errors.empty?
end