Class: Factbase::QueryOnce

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

Overview

Query with a cache, a decorator of another query.

It is NOT thread-safe!

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(fb, query, maps) ⇒ QueryOnce

Constructor.

Parameters:



37
38
39
40
41
# File 'lib/factbase/query_once.rb', line 37

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

Instance Method Details

#delete!Integer

Delete all facts that match the query.

Returns:

  • (Integer)

    Total number of facts deleted



67
68
69
70
# File 'lib/factbase/query_once.rb', line 67

def delete!
  @fb.cache.clear
  @query.delete!
end

#each(params = {}) {|Fact| ... } ⇒ Integer

Iterate facts one by one.

Parameters:

  • params (Hash) (defaults to: {})

    Optional params accessible in the query via the “$” symbol

Yields:

  • (Fact)

    Facts one-by-one

Returns:

  • (Integer)

    Total number of facts yielded



47
48
49
50
51
52
53
54
55
56
# File 'lib/factbase/query_once.rb', line 47

def each(params = {}, &)
  unless block_given?
    return to_enum(__method__, params) if Factbase::Syntax.new(@fb, @query).to_term.abstract?
    key = [@query.to_s, @maps.object_id]
    before = @fb.cache[key]
    @fb.cache[key] = to_enum(__method__, params).to_a if before.nil?
    return @fb.cache[key]
  end
  @query.each(params, &)
end

#one(params = {}) ⇒ Object

Read a single value.

Parameters:

  • params (Hash) (defaults to: {})

    Optional params accessible in the query via the “$” symbol

Returns:

  • The value evaluated



61
62
63
# File 'lib/factbase/query_once.rb', line 61

def one(params = {})
  @query.one(params)
end