Class: Factbase::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/query.rb

Overview

Query.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(maps, mutex, query) ⇒ Query

Returns a new instance of Query.



32
33
34
35
36
# File 'lib/factbase/query.rb', line 32

def initialize(maps, mutex, query)
  @maps = maps
  @mutex = mutex
  @query = query
end

Instance Method Details

#delete!Integer

Delete all facts that match the query.

Returns:

  • (Integer)

    Total number of facts deleted



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/factbase/query.rb', line 56

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.matches?(f)
        deleted += 1
        true
      else
        false
      end
    end
  end
  deleted
end

#each {|Fact| ... } ⇒ Integer

Iterate them one by one.

Yields:

  • (Fact)

    Facts one-by-one

Returns:

  • (Integer)

    Total number of facts yielded



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/factbase/query.rb', line 41

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)
    next unless term.matches?(f)
    yield f
    yielded += 1
  end
  yielded
end