Class: Factbase::Logged::Query

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

Overview

Query decorator.

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

Instance Method Summary collapse

Constructor Details

#initialize(term, maps, tube, fb) ⇒ Query

Returns a new instance of Query.


127
128
129
130
131
132
# File 'lib/factbase/logged.rb', line 127

def initialize(term, maps, tube, fb)
  @term = term
  @maps = maps
  @tube = tube
  @fb = fb
end

Instance Method Details

#delete!(_fb = nil) ⇒ Object


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/factbase/logged.rb', line 184

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

#each(fb = nil, params = {}) ⇒ Object


134
135
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/logged.rb', line 134

def each(fb = nil, params = {}, &)
  return to_enum(__method__, fb, params) unless block_given?
  start = Time.now
  q = Factbase::Syntax.new(@term).to_term.to_s
  if block_given?
    r = nil
    tail =
      Factbase::Logged.elapsed do
        r = @fb.query(@term, @maps).each(@fb, params, &)
      end
    raise ".each of #{@term.class} returned #{r.class}" unless r.is_a?(Integer)
    if r.zero?
      @tube.say(start, "Nothing found by '#{q}' #{tail}")
    else
      @tube.say(start, "Found #{r} fact(s) by '#{q}' #{tail}")
    end
    r
  else
    array = []
    tail =
      Factbase::Logged.elapsed do
        @fb.query(@term, @maps).each(@fb, params) do |f|
          array << f
        end
      end
    if array.empty?
      @tube.say(start, "Nothing found by '#{q}' #{tail}")
    else
      @tube.say(start, "Found #{array.size} fact(s) by '#{q}' #{tail}")
    end
    array
  end
end

#one(_fb = nil, params = {}) ⇒ Object


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

def one(_fb = nil, params = {})
  start = Time.now
  q = Factbase::Syntax.new(@term).to_term.to_s
  r = nil
  tail =
    Factbase::Logged.elapsed do
      r = @fb.query(@term, @maps).one(@fb, params)
    end
  if r.nil?
    @tube.say(start, "Nothing found by '#{q}' #{tail}")
  else
    @tube.say(start, "Found #{r} (#{r.class}) by '#{q}' #{tail}")
  end
  r
end