-
Notifications
You must be signed in to change notification settings - Fork 382
/
boolean.rb
44 lines (41 loc) · 888 Bytes
/
boolean.rb
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
# encoding: UTF-8
module MongoMapper
module Extensions
module Boolean
Mapping = {
true => true,
'true' => true,
'TRUE' => true,
'True' => true,
't' => true,
'T' => true,
'1' => true,
1 => true,
1.0 => true,
false => false,
'false' => false,
'FALSE' => false,
'False' => false,
'f' => false,
'F' => false,
'0' => false,
0 => false,
0.0 => false,
nil => nil
}
def to_mongo(value)
if value.is_a?(Boolean)
value
else
Mapping[value]
end
end
def from_mongo(value)
value.nil? ? nil : !!value
end
end
end
end
class Boolean
extend MongoMapper::Extensions::Boolean
end