Class: Factbase::Looged::Query

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

Overview

Query decorator.

This is an internal class, it is not supposed to be instantiated directly.

Instance Method Summary collapse

Constructor Details

#initialize(fb, expr, loog) ⇒ Query

Returns a new instance of Query.



115
116
117
118
119
# File 'lib/factbase/looged.rb', line 115

def initialize(fb, expr, loog)
  @fb = fb
  @expr = expr
  @loog = loog
end

Instance Method Details

#delete!Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/factbase/looged.rb', line 168

def delete!
  r = nil
  before = @fb.size
  tail =
    Factbase::Looged.elapsed do
      r = @fb.query(@expr).delete!
    end
  raise ".delete! of #{@query.class} returned #{r.class}" unless r.is_a?(Integer)
  if before.zero?
    @loog.debug("There were no facts, nothing deleted by '#{@expr}' #{tail}")
  elsif r.zero?
    @loog.debug("No facts out of #{before} deleted by '#{@expr}' #{tail}")
  else
    @loog.debug("Deleted #{r} fact(s) out of #{before} by '#{@expr}' #{tail}")
  end
  r
end

#each(params = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/factbase/looged.rb', line 136

def each(params = {}, &)
  q = Factbase::Syntax.new(@expr).to_term.to_s
  if block_given?
    r = nil
    tail =
      Factbase::Looged.elapsed do
        r = @fb.query(@expr).each(params, &)
      end
    raise ".each of #{@query.class} returned #{r.class}" unless r.is_a?(Integer)
    if r.zero?
      @loog.debug("Nothing found by '#{q}' #{tail}")
    else
      @loog.debug("Found #{r} fact(s) by '#{q}' #{tail}")
    end
    r
  else
    array = []
    tail =
      Factbase::Looged.elapsed do
        @fb.query(@expr).each(params) do |f|
          array << f
        end
      end
    if array.empty?
      @loog.debug("Nothing found by '#{q}' #{tail}")
    else
      @loog.debug("Found #{array.size} fact(s) by '#{q}' #{tail}")
    end
    array
  end
end

#one(params = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/factbase/looged.rb', line 121

def one(params = {})
  q = Factbase::Syntax.new(@expr).to_term.to_s
  r = nil
  tail =
    Factbase::Looged.elapsed do
      r = @fb.query(@expr).one(params)
    end
  if r.nil?
    @loog.debug("Nothing found by '#{q}' #{tail}")
  else
    @loog.debug("Found #{r} (#{r.class}) by '#{q}' #{tail}")
  end
  r
end