Class: Factbase::IndexedQuery

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/factbase/indexed/indexed_query.rb

Overview

Query with an index, a decorator of another query.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(origin, idx, fb) ⇒ IndexedQuery

Constructor.

Parameters:



20
21
22
23
24
# File 'lib/factbase/indexed/indexed_query.rb', line 20

def initialize(origin, idx, fb)
  @origin = origin
  @idx = idx
  @fb = fb
end

Instance Method Details

#delete!(fb = @fb) ⇒ Integer

Delete all facts that match the query.

Parameters:

  • fb (Factbase) (defaults to: @fb)

    The factbase

Returns:

  • (Integer)

    Total number of facts deleted



56
57
58
59
# File 'lib/factbase/indexed/indexed_query.rb', line 56

def delete!(fb = @fb)
  @idx.clear
  @origin.delete!(fb)
end

#each(fb = @fb, 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



36
37
38
39
40
41
42
43
# File 'lib/factbase/indexed/indexed_query.rb', line 36

def each(fb = @fb, params = {})
  return to_enum(__method__, fb, params) unless block_given?
  a = @origin.each(fb, params).to_a
  a.each do |f|
    yield Factbase::IndexedFact.new(f, @idx)
  end
  a.size
end

#one(fb = @fb, params = nil) ⇒ String|Integer|Float|Time|Array|NilClass

Read a single value.

Parameters:

  • fb (Factbase) (defaults to: @fb)

    The factbase

  • params (Hash) (defaults to: nil)

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

Returns:

  • (String|Integer|Float|Time|Array|NilClass)

    The value evaluated



49
50
51
# File 'lib/factbase/indexed/indexed_query.rb', line 49

def one(fb = @fb, params = nil)
  @origin.one(fb, params)
end

#to_sString

Print it as a string.

Returns:

  • (String)

    The query as a string



28
29
30
# File 'lib/factbase/indexed/indexed_query.rb', line 28

def to_s
  @origin.to_s
end