Class: Kafo::DataTypes::Boolean

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

Instance Method Summary collapse

Methods inherited from Kafo::DataType

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

Constructor Details

#initialize(*permitted) ⇒ Boolean

Returns a new instance of Boolean.



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

def initialize(*permitted)
  @permitted = permitted
end

Instance Method Details

#typecast(value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/kafo/data_types/boolean.rb', line 8

def typecast(value)
  case value
    when '0', 'false', 'f', 'n', false
      false
    when '1', 'true', 't', 'y', true
      true
    else
      value
  end
end

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

Returns:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kafo/data_types/boolean.rb', line 19

def valid?(input, errors = [])
  (input.is_a?(::TrueClass) || input.is_a?(::FalseClass)).tap do |valid|
    errors << "#{input.inspect} is not a valid boolean" unless valid
  end

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

  return errors.empty?
end