Class: RDF::Literal::Boolean

Inherits:
RDF::Literal show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/model/literal/boolean.rb

Overview

A boolean literal.

Constant Summary collapse

DATATYPE =

Since:

  • 0.2.1

RDF::URI("http://www.w3.org/2001/XMLSchema#boolean")
GRAMMAR =

Since:

  • 0.2.1

/^(true|false|1|0)$/.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, XSD_STRING, ZERO

Constants included from N3::Terminals

N3::Terminals::ANON, N3::Terminals::BASE, N3::Terminals::BLANK_NODE_LABEL, N3::Terminals::DECIMAL, N3::Terminals::DOUBLE, N3::Terminals::ECHAR, N3::Terminals::ESCAPE_CHAR4, N3::Terminals::ESCAPE_CHAR8, N3::Terminals::EXPONENT, N3::Terminals::INTEGER, N3::Terminals::IPLSTART, N3::Terminals::IRIREF, N3::Terminals::IRI_RANGE, N3::Terminals::LANGTAG, N3::Terminals::PERCENT, N3::Terminals::PLX, N3::Terminals::PNAME_LN, N3::Terminals::PNAME_NS, N3::Terminals::PN_CHARS, N3::Terminals::PN_CHARS_BASE, N3::Terminals::PN_CHARS_BODY, N3::Terminals::PN_CHARS_U, N3::Terminals::PN_LOCAL, N3::Terminals::PN_LOCAL_BODY, N3::Terminals::PN_LOCAL_ESC, N3::Terminals::PN_PREFIX, N3::Terminals::PREFIX, N3::Terminals::QUICK_VAR_NAME, N3::Terminals::STRING_LITERAL_LONG_QUOTE, N3::Terminals::STRING_LITERAL_LONG_SINGLE_QUOTE, N3::Terminals::STRING_LITERAL_QUOTE, N3::Terminals::STRING_LITERAL_SINGLE_QUOTE, N3::Terminals::UCHAR, N3::Terminals::U_CHARS1, N3::Terminals::U_CHARS2, N3::Terminals::WS

Constants included from SPARQL::Algebra::Expression

SPARQL::Algebra::Expression::PATTERN_PARENTS

Constants included from Util::Logger

Util::Logger::IOWrapper

Instance Attribute Summary

Attributes inherited from RDF::Literal

#datatype, #direction, #language

Instance Method Summary collapse

Methods inherited from RDF::Literal

#as_datetime, #as_number, #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?, #valid_extended?, #validate!, #value, #value_hash

Methods included from Term

#aggregate?, #as_datetime, #as_number, #compatible?, #eql?, #escape, #evaluate, #ndvars, #optimize, #sameTerm?, #term?, #terms, #to_base, #to_sparql, #to_term, #vars

Methods included from SPARQL::Algebra::Expression

cast, #constant?, #evaluate, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Methods included from Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Methods included from Value

#anonymous?, #canonicalize, #constant?, #formula?, #graph?, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #start_with?, #statement?, #term?, #to_ndvar, #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.

Parameters:

  • value (String, Boolean)
  • value (Object)
  • direction (Symbol)

    (nil) Initial text direction.

  • language (Symbol)

    (nil) Language is downcased to ensure proper matching

  • lexical (String) (defaults to: nil)

    (nil) Supplied lexical representation of this literal, otherwise it comes from transforming value to a string form..

  • datatype (URI) (defaults to: nil)

    (nil)

  • validate (Boolean)

    (false)

  • canonicalize (Boolean)

    (false)

Since:

  • 0.2.1



16
17
18
19
20
21
22
23
24
25
26
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/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

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.

Parameters:

Returns:

Since:

  • 0.3.0



44
45
46
47
48
49
50
51
52
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/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.

Parameters:

Returns:

Since:

  • 0.3.0



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/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 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/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.

Returns:

Since:

  • 0.2.1



104
105
106
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/model/literal/boolean.rb', line 104

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

#inspectString

Returns a developer-friendly representation of self.

Returns:

Since:

  • 0.2.1



112
113
114
115
116
117
118
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/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_iInteger

Returns the value as an integer.

Returns:

Since:

  • 0.3.0



88
89
90
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/model/literal/boolean.rb', line 88

def to_i
  @object ? 1 : 0
end

#to_sString

Returns the value as a string.

Returns:

Since:

  • 0.2.1



79
80
81
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/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.

Returns:

Since:

  • 0.2.1



96
97
98
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/model/literal/boolean.rb', line 96

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