Class: Factbase::Query

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(maps, term, fb) ⇒ Query

Constructor.

Parameters:

  • maps (Array<Fact>)

    Array of facts to start with

  • term (String|Factbase::Term)

    The query term



26
27
28
29
30
# File 'lib/factbase/query.rb', line 26

def initialize(maps, term, fb)
  @maps = maps
  @term = term.is_a?(String) ? Factbase::Syntax.new(term).to_term : term
  @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 to delete from

Returns:

  • (Integer)

    Total number of facts deleted



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/factbase/query.rb', line 81

def delete!(fb = @fb)
  deleted = 0
  @maps.delete_if do |m|
    f = Factbase::Fact.new(m)
    if @term.evaluate(f, @maps, fb)
      deleted += 1
      true
    else
      false
    end
  end
  deleted
end

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

Iterate facts one by one.

Parameters:

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

    The factbase

  • 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 (if block given), otherwise enumerator



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, 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.

Parameters:

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

    The factbase

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

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

Returns:

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

    The value evaluated



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_sString

Print it as a string.

Returns:

  • (String)

    The query as a string



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

def to_s
  @term.to_s
end