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.



129
130
131
132
133
134
# File 'lib/factbase/logged.rb', line 129

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

Instance Method Details

#delete!(fb = @fb) ⇒ Object

Raises:

  • (StandardError)


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/factbase/logged.rb', line 178

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

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/factbase/logged.rb', line 140

def each(fb = @fb, params = {}, &)
  return to_enum(__method__, fb, params) unless block_given?
  mono = Process.clock_gettime(Factbase::Logged::MONO)
  r = nil
  qry = @fb.query(@term, @maps)
  tail =
    Factbase::Logged.elapsed do
      r = qry.each(fb, params, &)
    end
  unless r.is_a?(Integer)
    raise(StandardError, ".query(#{@term.to_s.inspect}).each() of #{qry.class} returned #{r.class}")
  end
  q = Factbase::Syntax.new(@term).to_term.to_s
  q = "#{q} with {#{params.map { |k, v| "#{k}=#{v}" }.join(', ')}}" if params.is_a?(Hash) && !params.empty?
  if r.zero?
    @tube.say(mono, "Zero/#{@fb.size} facts found by #{q} #{tail}")
  else
    @tube.say(mono, "Found #{r}/#{@fb.size} fact(s) by #{q} #{tail}")
  end
  r
end

#one(fb = @fb, params = {}) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/factbase/logged.rb', line 162

def one(fb = @fb, params = {})
  mono = Process.clock_gettime(Factbase::Logged::MONO)
  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(mono, "Nothing found by '#{q}' #{tail}")
  else
    @tube.say(mono, "Found #{r} (#{r.class}) by '#{q}' #{tail}")
  end
  r
end

#to_sObject



136
137
138
# File 'lib/factbase/logged.rb', line 136

def to_s
  @term.to_s
end