Class: Factbase::Query
- Inherits:
-
Object
- Object
- Factbase::Query
- Defined in:
- lib/factbase/query.rb
Overview
Query.
This is an internal class, it is not supposed to be instantiated directly. It is created by the query() method of the Factbase
class.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
-
#delete! ⇒ Integer
Delete all facts that match the query.
-
#each {|Fact| ... } ⇒ Integer
Iterate them one by one.
-
#initialize(maps, mutex, query) ⇒ Query
constructor
Constructor.
Constructor Details
#initialize(maps, mutex, query) ⇒ Query
Constructor.
40 41 42 43 44 |
# File 'lib/factbase/query.rb', line 40 def initialize(maps, mutex, query) @maps = maps @mutex = mutex @query = query end |
Instance Method Details
#delete! ⇒ Integer
Delete all facts that match the query.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/factbase/query.rb', line 68 def delete! term = Factbase::Syntax.new(@query).to_term deleted = 0 @mutex.synchronize do @maps.delete_if do |m| f = Factbase::Fact.new(@mutex, m) if term.evaluate(f, @maps) deleted += 1 true else false end end end deleted end |
#each {|Fact| ... } ⇒ Integer
Iterate them one by one.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/factbase/query.rb', line 49 def each return to_enum(__method__) unless block_given? term = Factbase::Syntax.new(@query).to_term yielded = 0 @maps.each do |m| f = Factbase::Fact.new(@mutex, m) r = term.evaluate(f, @maps) unless r.is_a?(TrueClass) || r.is_a?(FalseClass) raise "Unexpected evaluation result (#{r.class}), must be Boolean at #{@query}" end next unless r yield f yielded += 1 end yielded end |