Class: FlatKit::FieldType::BooleanType
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)
CoerceFailure
Class Method Summary
collapse
best_guess, candidate_types, weight
#children, #find_child, #find_children, #inherited
Class Method Details
.coerce(data) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/flat_kit/field_type/boolean_type.rb', line 30
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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/flat_kit/field_type/boolean_type.rb', line 13
def self.matches?(data)
case data
when TrueClass
true
when FalseClass
true
when String
REGEX.match?(data)
when Integer
return true if data.zero?
return true if data == 1
return false
else
false
end
end
|
.type_name ⇒ Object
9
10
11
|
# File 'lib/flat_kit/field_type/boolean_type.rb', line 9
def self.type_name
"boolean"
end
|