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.
It is NOT thread-safe!
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024-2025 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
-
#delete! ⇒ Integer
Delete all facts that match the query.
-
#each(params = {}) {|Fact| ... } ⇒ Integer
Iterate facts one by one.
-
#initialize(fb, maps, mutex, query) ⇒ Query
constructor
Constructor.
-
#one(params = {}) ⇒ Object
Read a single value.
-
#to_s ⇒ String
Print it as a string.
Constructor Details
#initialize(fb, maps, mutex, query) ⇒ Query
Constructor.
28 29 30 31 32 33 |
# File 'lib/factbase/query.rb', line 28 def initialize(fb, maps, mutex, query) @fb = fb @maps = maps @mutex = mutex @query = query end |
Instance Method Details
#delete! ⇒ Integer
Delete all facts that match the query.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/factbase/query.rb', line 81 def delete! term = Factbase::Syntax.new(@fb, @query).to_term deleted = 0 @mutex.synchronize do @maps.delete_if do |m| f = Factbase::Fact.new(@fb, @mutex, m) if term.evaluate(f, @maps) deleted += 1 true else false end end end deleted end |
#each(params = {}) {|Fact| ... } ⇒ Integer
Iterate facts one by one.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/factbase/query.rb', line 45 def each(params = {}) return to_enum(__method__, params) unless block_given? term = Factbase::Syntax.new(@fb, @query).to_term yielded = 0 @maps.each do |m| extras = {} f = Factbase::Fact.new(@fb, @mutex, m) params = params.transform_keys(&:to_s) if params.is_a?(Hash) f = Factbase::Tee.new(f, params) a = Factbase::Accum.new(f, extras, false) r = term.evaluate(a, @maps) unless r.is_a?(TrueClass) || r.is_a?(FalseClass) raise "Unexpected evaluation result of type #{r.class}, must be Boolean at #{@query.inspect}" end next unless r yield Factbase::Accum.new(f, extras, true) yielded += 1 end yielded end |
#one(params = {}) ⇒ Object
Read a single value.
69 70 71 72 73 74 75 76 77 |
# File 'lib/factbase/query.rb', line 69 def one(params = {}) term = Factbase::Syntax.new(@fb, @query).to_term params = params.transform_keys(&:to_s) if params.is_a?(Hash) r = term.evaluate(Factbase::Tee.new(nil, params), @maps) unless %w[String Integer Float Time Array NilClass].include?(r.class.to_s) raise "Incorrect type #{r.class} returned by #{@query.inspect}" end r end |
#to_s ⇒ String
Print it as a string.
37 38 39 |
# File 'lib/factbase/query.rb', line 37 def to_s @query.to_s end |