class RDF::Literal::Boolean

A boolean literal.

@see www.w3.org/TR/xmlschema11-2/#boolean @since 0.2.1

Constants

DATATYPE
FALSES
GRAMMAR
TRUES

Public Class Methods

new(value, datatype: nil, lexical: nil, **options) click to toggle source

@param [String, Boolean] value @param (see Literal#initialize)

# File lib/rdf/model/literal/boolean.rb, line 16
def initialize(value, datatype: nil, lexical: nil, **options)
  @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

Public Instance Methods

<=>(other) click to toggle source

Compares this literal to ‘other` for sorting purposes.

@param [Object] other @return [Integer] ‘-1`, `0`, or `1` @since 0.3.0

Calls superclass method RDF::Literal#<=>
# 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) click to toggle source

Returns ‘true` if this literal is equivalent to `other`.

@param [Object] other @return [Boolean] ‘true` or `false` @since 0.3.0

Calls superclass method RDF::Literal#==
# 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!() click to toggle source

Converts this literal into its canonical lexical representation.

@return [RDF::Literal] ‘self` @see www.w3.org/TR/xmlschema11-2/#boolean-canonical-representation

# File lib/rdf/model/literal/boolean.rb, line 33
def canonicalize!
  @string = (@object ? :true : :false).to_s
  self
end
false?() click to toggle source

Returns ‘true` if this value is `false`.

@return [Boolean]

# File lib/rdf/model/literal/boolean.rb, line 104
def false?
  @object.equal?(false)
end
inspect() click to toggle source

Returns a developer-friendly representation of ‘self`.

@return [String]

Calls superclass method RDF::Literal#inspect
# 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() click to toggle source

Returns the value as an integer.

@return [Integer] ‘0` or `1` @since 0.3.0

# File lib/rdf/model/literal/boolean.rb, line 88
def to_i
  @object ? 1 : 0
end
to_s() click to toggle source

Returns the value as a string.

@return [String]

# File lib/rdf/model/literal/boolean.rb, line 79
def to_s
  @string || @object.to_s
end
true?() click to toggle source

Returns ‘true` if this value is `true`.

@return [Boolean]

# File lib/rdf/model/literal/boolean.rb, line 96
def true?
  @object.equal?(true)
end