Class: RDF::Query::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/extensions/variable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, value = nil) ⇒ Variable

Returns a new instance of Variable.



6
7
8
9
10
11
# File 'lib/lib/extensions/variable.rb', line 6

def initialize(name = nil, value = nil)
  name = 'v%s' % rand(100000) if name.nil?    
  @name, @values = name.to_sym, []
  @strict = true
  bind(value) unless value.nil?
end

Instance Attribute Details

#modifierObject (readonly)

Returns the value of attribute modifier.



4
5
6
# File 'lib/lib/extensions/variable.rb', line 4

def modifier
  @modifier
end

Instance Method Details

#bind(value, modifier = "=", strict = true) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lib/extensions/variable.rb', line 29

def bind(value, modifier = "=", strict = true)
  value = if value.respond_to?(:object)
    value.object
  else
    value
  end

  @modifier = case modifier
    when :eq then "="
    when :nq then "!="
    when :lt then "<"
    when :lte then "<="
    when :gt then ">"
    when :gte then ">="
    else modifier
  end
  
  @strict = strict
  add(value, modifier)
end

#literal?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/lib/extensions/variable.rb', line 17

def literal?
  value.class == String && values.length == 1 && modifier == "="
end

#strict?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/lib/extensions/variable.rb', line 50

def strict?
  @strict
end

#to_sObject



54
55
56
# File 'lib/lib/extensions/variable.rb', line 54

def to_s
  unbound? ? "?#{name}" : "#{name}#{modifier}#{value}"
end

#unbound?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/lib/extensions/variable.rb', line 13

def unbound?
  @values.empty?
end

#valueObject



21
22
23
# File 'lib/lib/extensions/variable.rb', line 21

def value
  @values.first.nil? ? nil : @values.first.last
end

#valuesObject



25
26
27
# File 'lib/lib/extensions/variable.rb', line 25

def values
  @values.dup
end