Class: RDF::Literal::Boolean
- Inherits:
-
RDF::Literal
- Object
- RDF::Literal
- RDF::Literal::Boolean
- Defined in:
- lib/rdf/model/literal/boolean.rb
Overview
A boolean literal.
Constant Summary collapse
- DATATYPE =
RDF::URI("http://www.w3.org/2001/XMLSchema#boolean")
- GRAMMAR =
/^(true|false|1|0)$/.freeze
- TRUES =
%w(true 1).freeze
- FALSES =
%w(false 0).freeze
Constants inherited from RDF::Literal
FALSE, TRUE, XSD_STRING, ZERO
Instance Attribute Summary
Attributes inherited from RDF::Literal
#datatype, #direction, #language
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compares this literal to
other
for sorting purposes. -
#==(other) ⇒ Boolean
Returns
true
if this literal is equivalent toother
. -
#canonicalize! ⇒ RDF::Literal
Converts this literal into its canonical lexical representation.
-
#false? ⇒ Boolean
Returns
true
if this value isfalse
. -
#initialize(value, datatype: nil, lexical: nil, **options) ⇒ Boolean
constructor
A new instance of Boolean.
-
#inspect ⇒ String
Returns a developer-friendly representation of
self
. -
#to_i ⇒ Integer
Returns the value as an integer.
-
#to_s ⇒ String
Returns the value as a string.
-
#true? ⇒ Boolean
Returns
true
if this value istrue
.
Methods inherited from RDF::Literal
#compatible?, #comperable_datatype2?, #comperable_datatype?, #datatype?, #direction?, #english?, #eql?, #escape, #hash, #humanize, #language?, #literal?, #method_missing, #object, #plain?, #respond_to_missing?, #simple?, #squish, #squish!, #valid?, #validate!, #value, #value_hash
Methods included from Term
#compatible?, #eql?, #escape, #term?, #terms, #to_base, #to_term
Methods included from Value
#anonymous?, #canonicalize, #constant?, #graph?, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #start_with?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!, #variable?
Constructor Details
#initialize(value, datatype: nil, lexical: nil, **options) ⇒ Boolean
Returns a new instance of Boolean.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rdf/model/literal/boolean.rb', line 16 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RDF::Literal
Instance Method Details
#<=>(other) ⇒ Integer
Compares this literal to other
for sorting purposes.
44 45 46 47 48 49 50 51 52 |
# File 'lib/rdf/model/literal/boolean.rb', line 44 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 |
#==(other) ⇒ Boolean
Returns true
if this literal is equivalent to other
.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rdf/model/literal/boolean.rb', line 60 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 |
#canonicalize! ⇒ RDF::Literal
Converts this literal into its canonical lexical representation.
33 34 35 36 |
# File 'lib/rdf/model/literal/boolean.rb', line 33 def canonicalize! @string = (@object ? :true : :false).to_s self end |
#false? ⇒ Boolean
Returns true
if this value is false
.
104 105 106 |
# File 'lib/rdf/model/literal/boolean.rb', line 104 def false? @object.equal?(false) end |
#inspect ⇒ String
Returns a developer-friendly representation of self
.
112 113 114 115 116 117 118 |
# File 'lib/rdf/model/literal/boolean.rb', line 112 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 |
#to_i ⇒ Integer
Returns the value as an integer.
88 89 90 |
# File 'lib/rdf/model/literal/boolean.rb', line 88 def to_i @object ? 1 : 0 end |
#to_s ⇒ String
Returns the value as a string.
79 80 81 |
# File 'lib/rdf/model/literal/boolean.rb', line 79 def to_s @string || @object.to_s end |
#true? ⇒ Boolean
Returns true
if this value is true
.
96 97 98 |
# File 'lib/rdf/model/literal/boolean.rb', line 96 def true? @object.equal?(true) end |