Module: Speck::Mixins::Object

Defined in:
lib/speck/core_ext/check_mixins.rb

Constant Summary collapse

Target =
::Object

Instance Method Summary collapse

Instance Method Details

#check(&check) ⇒ Object

This method is responsible for running some sort of test on the receiver.

It expects a block (returning true or false) to be passed. The block will be passed the receiver, so you can run comparators on it, or whatever else you like.

#check can also be called without block on truthy objects (true, false, or nil, any other object will be treated as true)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/speck/core_ext/check_mixins.rb', line 27

def check &check
  check = ->(_){self} unless block_given?
  
  # TODO: Should we allow specks in the root environment? Could be useful
  #       for quick checks…
  raise Exception::NoEnvironment unless Speck.current
  
  file, line, _ = Kernel::caller.first.split(':')
  source = File.open(file).readlines[line.to_i - 1]
  source.strip!
  source = source.partition(".check").first
  # TODO: Get rid of the "(…)" around the resulting string.
  
  Speck::Check.new(->(){ check[self] }, source)
    .tap {|check| Speck.current.checks << check }
end