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!(fb = @fb, id: '_id') ⇒ Integer
Delete all facts that match the query.
-
#each(fb = @fb, params = {}) {|Fact| ... } ⇒ Integer
Iterate facts one by one.
-
#initialize(maps, term, fb) ⇒ Query
constructor
Constructor.
-
#one(fb = @fb, params = {}) ⇒ String|Integer|Float|Time|Array|NilClass
Read a single value.
-
#to_s ⇒ String
Print it as a string.
Constructor Details
Instance Method Details
#delete!(fb = @fb, id: '_id') ⇒ Integer
Delete all facts that match the query.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/factbase/query.rb', line 82 def delete!(fb = @fb, id: '_id') deleted = 0 ids = [] each(fb) do |f| i = f[id] unless i ids = nil break end ids << i.first end @maps.delete_if do |m| d = if ids i = m[id]&.first i && ids.include?(i) else @term.evaluate(Factbase::Fact.new(m), @maps, fb) end deleted += 1 if d d end deleted end |
#each(fb = @fb, params = {}) {|Fact| ... } ⇒ Integer
Iterate facts one by one.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/factbase/query.rb', line 43 def each(fb = @fb, params = {}) return to_enum(__method__, fb, params) unless block_given? yielded = 0 params = params.transform_keys(&:to_s) if params.is_a?(Hash) maybe = @term.predict(@maps, fb, Factbase::Tee.new({}, params)) maybe ||= @maps unless maybe.equal?(@maps) maybe.each do |m| extras = {} f = Factbase::Fact.new(m) f = Factbase::Tee.new(f, params) a = Factbase::Accum.new(f, extras, false) r = @term.evaluate(a, @maps, fb) unless r.is_a?(TrueClass) || r.is_a?(FalseClass) raise "Unexpected evaluation result of type #{r.class}, must be Boolean at #{@term.inspect}" end next unless r yield Factbase::Accum.new(f, extras, true) yielded += 1 end yielded end |
#one(fb = @fb, params = {}) ⇒ String|Integer|Float|Time|Array|NilClass
Read a single value.
69 70 71 72 73 74 75 76 |
# File 'lib/factbase/query.rb', line 69 def one(fb = @fb, params = {}) params = params.transform_keys(&:to_s) if params.is_a?(Hash) r = @term.evaluate(Factbase::Tee.new(Factbase::Fact.new({}), params), @maps, fb) unless %w[String Integer Float Time Array NilClass].include?(r.class.to_s) raise "Incorrect type #{r.class} returned by #{@term.inspect}" end r end |
#to_s ⇒ String
Print it as a string.
34 35 36 |
# File 'lib/factbase/query.rb', line 34 def to_s @term.to_s end |