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
- DATATYPE =
XSD.boolean
- GRAMMAR =
/^(true|false|1|0)$/i.freeze
- TRUES =
%w(true 1).freeze
- FALSES =
%w(false 0).freeze
Constants inherited from RDF::Literal
Instance Attribute Summary
Attributes inherited from RDF::Literal
Instance Method Summary (collapse)
-
- (Integer) <=>(other)
Compares this literal to
otherfor sorting purposes. -
- (Boolean) ==(other)
Returns
trueif this literal is equivalent toother. -
- (RDF::Literal) canonicalize!
Converts this literal into its canonical lexical representation.
-
- (Boolean) false?
Returns
trueif this value isfalse. -
- (Boolean) initialize(value, options = {})
constructor
A new instance of Boolean.
-
- (String) inspect
Returns a developer-friendly representation of
self. -
- (Integer) to_i
Returns the value as an integer.
-
- (String) to_s
Returns the value as a string.
-
- (Boolean) true?
Returns
trueif this value istrue.
Methods inherited from RDF::Literal
#anonymous?, #canonicalize, #comperable_datatype?, #eql?, #has_datatype?, #has_language?, #hash, #invalid?, #literal?, #object, #plain?, #valid?, #validate!, #value
Methods included from Term
Methods included from Value
#graph?, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_quad, #to_rdf, #type_error, #uri?, #variable?
Constructor Details
- (Boolean) initialize(value, options = {})
A new instance of Boolean
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rdf/model/literal/boolean.rb', line 16 def initialize(value, = {}) @datatype = RDF::URI([:datatype] || self.class.const_get(:DATATYPE)) @string = [:lexical] if .has_key?(:lexical) @string ||= 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 |
Instance Method Details
- (Integer) <=>(other)
Compares this literal to other for sorting purposes.
45 46 47 48 49 50 51 52 53 |
# File 'lib/rdf/model/literal/boolean.rb', line 45 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 |
- (Boolean) ==(other)
Returns true if this literal is equivalent to other.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rdf/model/literal/boolean.rb', line 61 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 |
- (RDF::Literal) canonicalize!
Converts this literal into its canonical lexical representation.
34 35 36 37 |
# File 'lib/rdf/model/literal/boolean.rb', line 34 def canonicalize! @string = (@object ? :true : :false).to_s self end |
- (Boolean) false?
Returns true if this value is false.
105 106 107 |
# File 'lib/rdf/model/literal/boolean.rb', line 105 def false? @object.equal?(false) end |
- (String) inspect
Returns a developer-friendly representation of self.
113 114 115 116 117 118 119 |
# File 'lib/rdf/model/literal/boolean.rb', line 113 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 |
- (Integer) to_i
Returns the value as an integer.
89 90 91 |
# File 'lib/rdf/model/literal/boolean.rb', line 89 def to_i @object ? 1 : 0 end |
- (String) to_s
Returns the value as a string.
80 81 82 |
# File 'lib/rdf/model/literal/boolean.rb', line 80 def to_s @string || @object.to_s end |
- (Boolean) true?
Returns true if this value is true.
97 98 99 |
# File 'lib/rdf/model/literal/boolean.rb', line 97 def true? @object.equal?(true) end |