Class: Amigo::Where
- Includes:
- Hamster::Immutable
- Defined in:
- lib/amigo/where.rb
Instance Method Summary collapse
- #ask(statements) ⇒ Object
- #bind(statement, row) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(subject, predicate, object) ⇒ Where
constructor
A new instance of Where.
- #join(statements, rows) ⇒ Object
- #join?(statement, row) ⇒ Boolean
- #select(statements) ⇒ Object
- #select?(statement) ⇒ Boolean
- #to_sparql ⇒ Object
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: ==
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 |
#hash ⇒ Object
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
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
43 44 45 |
# File 'lib/amigo/where.rb', line 43 def select?(statement) @components.all? { |component| component.select?(statement) } end |
#to_sparql ⇒ Object
55 56 57 |
# File 'lib/amigo/where.rb', line 55 def to_sparql "#{@components.map(&:to_sparql).join(" ")} ." end |