/
bool.cr
51 lines (46 loc) · 1.15 KB
/
bool.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module Marten
module DB
module Field
class Bool < Base
getter default
def initialize(
@id : ::String,
@primary_key = false,
@default : ::Bool? = nil,
@blank = false,
@null = false,
@unique = false,
@index = false,
@db_column = nil
)
end
def from_db(value) : ::Bool?
null? && value.nil? ? nil : [true, "true", 1, "1", "yes"].includes?(value)
end
def from_db_result_set(result_set : ::DB::ResultSet) : ::Bool?
from_db(result_set.read(::Bool | Int8 | Int16 | Int32 | Int64 | Nil | ::String))
end
def to_column : Management::Column::Base?
Management::Column::Bool.new(
db_column!,
primary_key?,
null?,
unique?,
index?,
to_db(default)
)
end
def to_db(value) : ::DB::Any
case value
when Nil
nil
when ::Bool
value
else
raise_unexpected_field_value(value)
end
end
end
end
end
end