/
boolean.rb
120 lines (109 loc) · 2.9 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
module RDF; class Literal
##
# A boolean literal.
#
# @see http://www.w3.org/TR/xmlschema11-2/#boolean
# @since 0.2.1
class Boolean < Literal
DATATYPE = RDF::XSD.boolean
GRAMMAR = /^(true|false|1|0)$/i.freeze
TRUES = %w(true 1).freeze
FALSES = %w(false 0).freeze
##
# @param [String, Boolean] value
# @param (see Literal#initialize)
def initialize(value, datatype: nil, lexical: nil)
@datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
@string = lexical || (value if value.is_a?(String))
@object = case
when true.equal?(value) then true
when false.equal?(value) then false
when TRUES.include?(value.to_s.downcase) then true
when FALSES.include?(value.to_s.downcase) then false
else value
end
end
##
# Converts this literal into its canonical lexical representation.
#
# @return [RDF::Literal] `self`
# @see http://www.w3.org/TR/xmlschema11-2/#boolean-canonical-representation
def canonicalize!
@string = (@object ? :true : :false).to_s
self
end
##
# Compares this literal to `other` for sorting purposes.
#
# @param [Object] other
# @return [Integer] `-1`, `0`, or `1`
# @since 0.3.0
def <=>(other)
case other
when TrueClass, FalseClass
to_i <=> (other ? 1 : 0)
when RDF::Literal::Boolean
to_i <=> other.to_i
else super
end
end
##
# Returns `true` if this literal is equivalent to `other`.
#
# @param [Object] other
# @return [Boolean] `true` or `false`
# @since 0.3.0
def ==(other)
# If lexically invalid, use regular literal testing
return super unless self.valid?
other = Literal::Boolean.new(other) if other.class == TrueClass || other.class == FalseClass
case other
when Literal::Boolean
return super unless other.valid?
(cmp = (self <=> other)) ? cmp.zero? : false
else
super
end
end
##
# Returns the value as a string.
#
# @return [String]
def to_s
@string || @object.to_s
end
##
# Returns the value as an integer.
#
# @return [Integer] `0` or `1`
# @since 0.3.0
def to_i
@object ? 1 : 0
end
##
# Returns `true` if this value is `true`.
#
# @return [Boolean]
def true?
@object.equal?(true)
end
##
# Returns `true` if this value is `false`.
#
# @return [Boolean]
def false?
@object.equal?(false)
end
##
# Returns a developer-friendly representation of `self`.
#
# @return [String]
def inspect
case
when self.equal?(RDF::Literal::TRUE) then 'RDF::Literal::TRUE'
when self.equal?(RDF::Literal::FALSE) then 'RDF::Literal::FALSE'
else super
end
end
end # Boolean
end; end # RDF::Literal