Class: RDF::Query::Variable
- Inherits:
-
Object
- Object
- RDF::Query::Variable
- Includes:
- Term
- Defined in:
- lib/rdf/query/variable.rb
Overview
An RDF query variable.
Instance Attribute Summary collapse
-
#name ⇒ Symbol
(also: #to_sym)
The variable’s name.
-
#value ⇒ RDF::Term
The variable’s value.
Instance Method Summary collapse
-
#===(other) ⇒ Boolean
Compares this variable with the given value.
-
#bind(value) ⇒ RDF::Term
(also: #bind!)
Rebinds this variable to the given ‘value`.
-
#bindings ⇒ Hash{Symbol => RDF::Term}
Returns this variable’s bindings (if any) as a ‘Hash`.
-
#bound? ⇒ Boolean
Returns ‘true` if this variable is bound.
-
#distinguished=(value) ⇒ Boolean
Sets if variable is distinguished or non-distinguished.
-
#distinguished? ⇒ Boolean
Returns ‘true` if this variable is distinguished.
-
#eql?(other) ⇒ Boolean
(also: #==)
Returns ‘true` if this variable is equivalent to a given `other` variable.
-
#hash ⇒ Fixnum
Returns a hash code for this variable.
-
#initialize(name = nil, value = nil) ⇒ Variable
constructor
A new instance of Variable.
-
#named? ⇒ Boolean
Returns ‘true` if this variable has a name.
-
#to_s ⇒ String
Returns a string representation of this variable.
-
#unbind ⇒ RDF::Term
(also: #unbind!)
Unbinds this variable, discarding any currently bound value.
-
#unbound? ⇒ Boolean
Returns ‘true` if this variable is unbound.
-
#variable? ⇒ Boolean
Returns ‘true`.
-
#variables ⇒ Hash{Symbol => RDF::Query::Variable}
(also: #to_hash)
Returns this variable as ‘Hash`.
Methods included from Term
Methods included from Value
#graph?, #inspect, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_quad, #to_rdf, #type_error, #uri?
Constructor Details
#initialize(name = nil, value = nil) ⇒ Variable
Returns a new instance of Variable.
66 67 68 69 |
# File 'lib/rdf/query/variable.rb', line 66 def initialize(name = nil, value = nil) @name = (name || "g#{__id__.to_i.abs}").to_sym @value = value end |
Instance Attribute Details
#name ⇒ Symbol Also known as: to_sym
The variable’s name.
52 53 54 |
# File 'lib/rdf/query/variable.rb', line 52 def name @name end |
#value ⇒ RDF::Term
The variable’s value.
59 60 61 |
# File 'lib/rdf/query/variable.rb', line 59 def value @value end |
Instance Method Details
#===(other) ⇒ Boolean
Compares this variable with the given value.
194 195 196 197 198 199 200 |
# File 'lib/rdf/query/variable.rb', line 194 def ===(other) if unbound? other.is_a?(RDF::Term) # match any Term when unbound else value === other end end |
#bind(value) ⇒ RDF::Term Also known as: bind!
Rebinds this variable to the given ‘value`.
127 128 129 130 131 |
# File 'lib/rdf/query/variable.rb', line 127 def bind(value) old_value = self.value self.value = value old_value end |
#bindings ⇒ Hash{Symbol => RDF::Term}
Returns this variable’s bindings (if any) as a ‘Hash`.
158 159 160 |
# File 'lib/rdf/query/variable.rb', line 158 def bindings unbound? ? {} : {name => value} end |
#bound? ⇒ Boolean
Returns ‘true` if this variable is bound.
93 94 95 |
# File 'lib/rdf/query/variable.rb', line 93 def bound? !unbound? end |
#distinguished=(value) ⇒ Boolean
Sets if variable is distinguished or non-distinguished. By default, variables are distinguished
118 119 120 |
# File 'lib/rdf/query/variable.rb', line 118 def distinguished=(value) @distinguished = value end |
#distinguished? ⇒ Boolean
Returns ‘true` if this variable is distinguished.
109 110 111 |
# File 'lib/rdf/query/variable.rb', line 109 def distinguished? @distinguished.nil? || @distinguished end |
#eql?(other) ⇒ Boolean Also known as: ==
Returns ‘true` if this variable is equivalent to a given `other` variable. Or, to another Term if bound, or to any other Term
178 179 180 181 182 183 184 185 186 |
# File 'lib/rdf/query/variable.rb', line 178 def eql?(other) if unbound? other.is_a?(RDF::Term) # match any Term when unbound elsif other.is_a?(RDF::Query::Variable) @name.eql?(other.name) else value.eql?(other) end end |
#hash ⇒ Fixnum
Returns a hash code for this variable.
167 168 169 |
# File 'lib/rdf/query/variable.rb', line 167 def hash @name.hash end |
#named? ⇒ Boolean
Returns ‘true` if this variable has a name.
85 86 87 |
# File 'lib/rdf/query/variable.rb', line 85 def named? true end |
#to_s ⇒ String
Returns a string representation of this variable.
Distinguished variables are indicated with a single ‘?`.
Non-distinguished variables are indicated with a double ‘??`
216 217 218 219 |
# File 'lib/rdf/query/variable.rb', line 216 def to_s prefix = distinguished? ? '?' : "??" unbound? ? "#{prefix}#{name}" : "#{prefix}#{name}=#{value}" end |
#unbind ⇒ RDF::Term Also known as: unbind!
Unbinds this variable, discarding any currently bound value.
138 139 140 141 142 |
# File 'lib/rdf/query/variable.rb', line 138 def unbind old_value = self.value self.value = nil old_value end |
#unbound? ⇒ Boolean
Returns ‘true` if this variable is unbound.
101 102 103 |
# File 'lib/rdf/query/variable.rb', line 101 def unbound? value.nil? end |
#variable? ⇒ Boolean
Returns ‘true`.
77 78 79 |
# File 'lib/rdf/query/variable.rb', line 77 def variable? true end |
#variables ⇒ Hash{Symbol => RDF::Query::Variable} Also known as: to_hash
Returns this variable as ‘Hash`.
149 150 151 |
# File 'lib/rdf/query/variable.rb', line 149 def variables {name => self} end |