Class: RDF::Literal::Boolean

Inherits:
RDF::Literal show all
Defined in:
lib/rdf/model/literal/boolean.rb

Overview

A boolean literal.

See Also:

Since:

Constant Summary

DATATYPE =

Since:

  • 0.2.1

XSD.boolean
GRAMMAR =

Since:

  • 0.2.1

/^(true|false|1|0)$/i.freeze
TRUES =

Since:

  • 0.2.1

%w(true  1).freeze
FALSES =

Since:

  • 0.2.1

%w(false 0).freeze

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Instance Method Summary (collapse)

Methods inherited from RDF::Literal

#anonymous?, #canonicalize, #eql?, #has_datatype?, #has_language?, #hash, #invalid?, #literal?, #object, #to_s, #valid?, #validate!, #value

Methods included from Term

#constant?, #variable?

Methods included from Value

#graph?, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_rdf, #uri?, #variable?

Constructor Details

- (Boolean) initialize(value, options = {})

A new instance of Boolean

Parameters:

  • (Boolean) value
  • (Hash) options (defaults to: {})

    a customizable set of options

Options Hash (options):

  • (String) :lexical — default: nil

Since:

  • 0.2.1



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rdf/model/literal/boolean.rb', line 16

def initialize(value, options = {})
  @datatype = RDF::URI(options[:datatype] || DATATYPE)
  @string   = options[:lexical] if options.has_key?(:lexical)
  @string   = value if !defined?(@string) && 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.

Parameters:

  • (Object) other

Returns:

Since:

  • 0.3.0



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) Also known as: ===

Returns true if this literal is equivalent to other.

Parameters:

  • (Object) other

Returns:

Since:

  • 0.3.0



61
62
63
# File 'lib/rdf/model/literal/boolean.rb', line 61

def ==(other)
  (cmp = (self <=> other)) ? cmp.zero? : false
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.

Returns:

Since:

  • 0.2.1



95
96
97
# File 'lib/rdf/model/literal/boolean.rb', line 95

def false?
  @object.equal?(false)
end

- (String) inspect

Returns a developer-friendly representation of self.

Returns:

  • (String)

Since:

  • 0.2.1



103
104
105
106
107
108
109
# File 'lib/rdf/model/literal/boolean.rb', line 103

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.

Returns:

Since:

  • 0.3.0



79
80
81
# File 'lib/rdf/model/literal/boolean.rb', line 79

def to_i
  @object ? 1 : 0
end

- (Boolean) true?

Returns true if this value is true.

Returns:

Since:

  • 0.2.1



87
88
89
# File 'lib/rdf/model/literal/boolean.rb', line 87

def true?
  @object.equal?(true)
end