Module: Assume

Included in:
Object
Defined in:
lib/assume.rb,
lib/assume/version.rb

Defined Under Namespace

Classes: BadAssumption

Constant Summary collapse

DEFAULT_HANDLER =
lambda do |result, block|
  filename, line = block.source_location
  begin
    lines = File.readlines(filename)
    line_content = lines[line - 1]
  rescue Errno::ENOENT => err
    line_content = "<unable to open source code file>"
  end

  raise BadAssumption, "\nin #{filename}\nsource" \
                       "code (line #{line}):\n#{line_content}\n" \
                       "result was: #{result.inspect}"
end
VERSION =
"0.1.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.broken_assumption_handlerObject



53
54
55
# File 'lib/assume.rb', line 53

def broken_assumption_handler
  @assumption_handler || DEFAULT_HANDLER
end

.enabled=(val) ⇒ Object



45
46
47
# File 'lib/assume.rb', line 45

def enabled=(val)
  @enabled = !!val
end

.enabled?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/assume.rb', line 49

def enabled?
  @enabled
end

.handler=(handler) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/assume.rb', line 37

def handler=(handler)
  unless handler.respond_to?(:call)
    raise ArgumentError, "Assume::handler must respond to #call"
  end

  @assumption_handler = handler
end

Instance Method Details

#assume(&block) ⇒ Object Also known as: assumption

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/assume.rb', line 22

def assume(&block)
  raise ArgumentError, "assumptions need a block" if block.nil?
  return unless Assume.enabled?

  result = block.call

  unless result
    Assume.broken_assumption_handler.call(result, block)
  end

  nil
end