Class: FlatKit::FieldType::BooleanType

Inherits:
FlatKit::FieldType show all
Defined in:
lib/flat_kit/field_type/boolean_type.rb

Overview

Internal: Implemenation of the boolean type and coercion to the type

Constant Summary collapse

TRUTHY_REGEX =
/\A(true|t|1|yes|y|on)\Z/i
FALSEY_REGEX =
/\A(false|f|0|no|n|off)\Z/i
REGEX =
Regexp.union(TRUTHY_REGEX, FALSEY_REGEX)

Constants inherited from FlatKit::FieldType

CoerceFailure

Class Method Summary collapse

Methods inherited from FlatKit::FieldType

best_guess, candidate_types, weight, weights

Methods included from DescendantTracker

#children, #find_child, #find_children, #inherited

Class Method Details

.coerce(data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/flat_kit/field_type/boolean_type.rb', line 32

def self.coerce(data)
  case data
  when TrueClass
    true
  when FalseClass
    false
  when Numeric
    return false if data.zero?
    return true  if data == 1

    CoerceFailure
  when String
    return true  if TRUTHY_REGEX.match?(data)
    return false if FALSEY_REGEX.match?(data)

    CoerceFailure
  end
end

.matches?(data) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/flat_kit/field_type/boolean_type.rb', line 16

def self.matches?(data)
  case data
  when TrueClass, FalseClass
    true
  when String
    REGEX.match?(data)
  when Integer
    return true if data.zero?
    return true if data == 1

    false
  else
    false
  end
end

.type_nameObject



12
13
14
# File 'lib/flat_kit/field_type/boolean_type.rb', line 12

def self.type_name
  "boolean"
end