Module: AssertLatestAndGreatest

Included in:
AssertAgile
Defined in:
lib/assert_latest_and_greatest.rb

Instance Method Summary collapse

Instance Method Details

#assert_latest(*models, &block) ⇒ Object

This collects every model in the given class that appears while its block runs



5
6
7
8
9
10
11
# File 'lib/assert_latest_and_greatest.rb', line 5

def assert_latest(*models, &block)
  models, diagnostic = _get_latest_args(models, 'assert')
  latests = get_latest(models, &block)
  latests.compact.length == models.length and add_assertion or
    _flunk_latest(models, latests, diagnostic, true, block)
  return *latests
end

#deny_latest(*models, &block) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/assert_latest_and_greatest.rb', line 26

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

#get_latest(models, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/assert_latest_and_greatest.rb', line 34

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