Class: Factbase::IndexedQuery

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

Overview

Query with an index, a decorator of another query.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(origin, idx, fb) ⇒ IndexedQuery

Constructor.

Parameters:

[View source]

18
19
20
21
22
# File 'lib/factbase/indexed/indexed_query.rb', line 18

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

[View source]

52
53
54
55
# File 'lib/factbase/indexed/indexed_query.rb', line 52

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

[View source]

34
35
36
37
38
39
# File 'lib/factbase/indexed/indexed_query.rb', line 34

def each(fb = @fb, params = {})
  return to_enum(__method__, fb, params) unless block_given?
  @origin.each(fb, params).to_a.each do |f|
    yield Factbase::IndexedFact.new(f, @idx)
  end
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

[View source]

45
46
47
# File 'lib/factbase/indexed/indexed_query.rb', line 45

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

[View source]

26
27
28
# File 'lib/factbase/indexed/indexed_query.rb', line 26

def to_s
  @origin.to_s
end