Module: Assertions

Included in:
AlterEgo, AlterEgo::State, AlterEgo::State
Defined in:
lib/assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert(*values, &block) ⇒ Object

Assert that no values are nil or false. Returns the last value.



7
8
9
10
11
# File 'lib/assertions.rb', line 7

def assert(*values, &block)
  iterate_and_return_last(values, block) do |v|
    raise_assertion_error unless v
  end
end

#assert_exists(*values, &block) ⇒ Object

Assert that no values are nil. Returns the last value.



21
22
23
# File 'lib/assertions.rb', line 21

def assert_exists(*values, &block)
  iterate_and_return_last(values, block) {  |value| deny(value.nil?) }
end

#assert_keys(hash, *keys) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/assertions.rb', line 35

def assert_keys(hash, *keys)
  assert_exists(hash)
  assert(hash.respond_to?(:[]))
  values = keys.inject([]) { |vals, k| vals << assert_exists(hash[k]) }
  assert(yield(*values)) if block_given?
  hash
end

#assert_one_or_more(*values, &block) ⇒ Object

Assert that values are collections that contain at least one element. Returns the last value.



27
28
29
30
31
32
33
# File 'lib/assertions.rb', line 27

def assert_one_or_more(*values, &block)
  iterate_and_return_last(values, block) do |value|
    assert_exists(value)
    deny(value.kind_of?(String))
    deny(value.empty?)
  end
end

#deny(*values) ⇒ Object

The opposite of #assert.



14
15
16
17
18
# File 'lib/assertions.rb', line 14

def deny(*values)
  assert(*values.map{ |v| !v})
  assert(yield(*values)) if block_given?
  values.last
end