Class: Amigo::Where

Inherits:
Object show all
Includes:
Hamster::Immutable
Defined in:
lib/amigo/where.rb

Instance Method Summary collapse

Constructor Details

#initialize(subject, predicate, object) ⇒ Where

Returns a new instance of Where.



15
16
17
18
19
20
21
# File 'lib/amigo/where.rb', line 15

def initialize(subject, predicate, object)
  @components = Hamster.list(
    component(:subject, subject),
    component(:predicate, predicate),
    component(:object, object)
  )
end

Instance Method Details

#ask(statements) ⇒ Object



23
24
25
# File 'lib/amigo/where.rb', line 23

def ask(statements)
  statements.any? { |statement| select?(statement) }
end

#bind(statement, row) ⇒ Object



51
52
53
# File 'lib/amigo/where.rb', line 51

def bind(statement, row)
  @components.reduce(row) { |row, component| component.bind(statement, row) }
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/amigo/where.rb', line 59

def eql?(other)
  return true if other.equal?(self)
  other.class.equal?(self.class) &&
    @components.eql?(other.instance_variable_get(:@components))
end

#hashObject



66
67
68
# File 'lib/amigo/where.rb', line 66

def hash
  @components.hash
end

#join(statements, rows) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/amigo/where.rb', line 34

def join(statements, rows)
  rows.reduce(Hamster.list) do |result, row|
    statements.reduce(result) do |result, statement|
      next result unless join?(statement, row)
      result.cons(bind(statement, row))
    end
  end
end

#join?(statement, row) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/amigo/where.rb', line 47

def join?(statement, row)
  select?(statement) && @components.all? { |component| component.join?(statement, row) }
end

#select(statements) ⇒ Object



27
28
29
30
31
32
# File 'lib/amigo/where.rb', line 27

def select(statements)
  statements.reduce(Hamster.list) do |result, statement|
    next result unless select?(statement)
    result.cons(bind(statement, Hamster.hash))
  end
end

#select?(statement) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/amigo/where.rb', line 43

def select?(statement)
  @components.all? { |component| component.select?(statement) }
end

#to_sparqlObject



55
56
57
# File 'lib/amigo/where.rb', line 55

def to_sparql
  "#{@components.map(&:to_sparql).join(" ")} ."
end