Module: AssertEfficientSql

Defined in:
lib/assert_efficient_sql.rb,
lib/assert_efficient_sql.rb

Defined Under Namespace

Classes: BufferStdout, SqlEfficiencyAsserter

Instance Method Summary collapse

Instance Method Details

#_exec(cmd) ⇒ Object

:nodoc:



241
242
243
# File 'lib/assert_efficient_sql.rb', line 241

def _exec(cmd) #:nodoc:
  ActiveRecord::Base.connection.execute(cmd)
end

#assert_efficient_sql(options = {}, &block) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/assert_efficient_sql.rb', line 245

def assert_efficient_sql(options = {}, &block)
  options = { :verbose => true } if options == :verbose
  
  if options.class == Hash
    options.reverse_merge! default_options

    if current_adapter?(:MysqlAdapter)
      return assert_efficient_mysql(options, &block)
    else
      warn_adapter_required(options)
      block.call if block
    end
  else
    print_syntax
  end
  
  return []
end

#assert_latest(*models, &block) ⇒ Object



188
189
190
191
# File 'lib/assert_efficient_sql.rb', line 188

def assert_latest(*models, &block)
  models, diagnostic = _get_latest_args(models, 'assert')
  get_latest(models, &block) or _flunk_latest(models, diagnostic, true, block)
end

#assert_stdout(matcher = nil, diagnostic = nil) ⇒ Object

:nodoc:



271
272
273
274
275
276
277
278
279
# File 'lib/assert_efficient_sql.rb', line 271

def assert_stdout(matcher = nil, diagnostic = nil)  #:nodoc:
  waz = $stdout
  $stdout = BufferStdout.new
  yield
  assert_match matcher, $stdout.output, diagnostic  if matcher
  return $stdout.output
ensure
  $stdout = waz
end

#deny_latest(*models, &block) ⇒ Object



206
207
208
209
210
211
# File 'lib/assert_efficient_sql.rb', line 206

def deny_latest(*models, &block)
  models, diagnostic = _get_latest_args(models, 'deny')
  return unless got = get_latest(models, &block)
  models = [got].flatten.compact.map(&:class)
 _flunk_latest(models, diagnostic, false, block)
end

#deny_stdout(unmatcher, diagnostic = nil, &block) ⇒ Object

:nodoc:



281
282
283
284
# File 'lib/assert_efficient_sql.rb', line 281

def deny_stdout(unmatcher, diagnostic = nil, &block) #:nodoc:
  got = assert_stdout(nil, nil, &block)
  assert_no_match unmatcher, got, diagnostic
end

#get_latest(models, &block) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/assert_efficient_sql.rb', line 213

def get_latest(models, &block)
  max_ids = models.map{|model| model.maximum(:id) || 0 }
  block.call
  index = -1
  return *models.map{|model|
    all = *model.find( :all,
                      :conditions => "id > #{max_ids[index += 1]}",
                      :order => "id asc" )
    all # * returns nil for [], 
          #     one object for [x], 
          #     or an array with more than one item
  }
end