Class: Contracts::Runtime

Inherits:
Base
  • Object
show all
Defined in:
lib/contracts/runtime.rb

Overview

Author

radiospiel ([email protected])

Copyright

Copyright © 2011, 2012 radiospiel

License

Distributes under the terms of the Modified BSD License, see LICENSE.BSD for details.

++

Instance Attribute Summary collapse

Attributes inherited from Base

#method

Instance Method Summary collapse

Methods inherited from Base

#+@, #method_name

Constructor Details

#initialize(expected_runtime, options) ⇒ Runtime

Returns a new instance of Runtime.



11
12
13
14
15
16
# File 'lib/contracts/runtime.rb', line 11

def initialize(expected_runtime, options)
  @expected_runtime = expected_runtime
  @max = options[:max]

  expect! max.nil? || expected_runtime <= max
end

Instance Attribute Details

#expected_runtimeObject (readonly)

Returns the value of attribute expected_runtime.



9
10
11
# File 'lib/contracts/runtime.rb', line 9

def expected_runtime
  @expected_runtime
end

#maxObject (readonly)

Returns the value of attribute max.



9
10
11
# File 'lib/contracts/runtime.rb', line 9

def max
  @max
end

Instance Method Details

#after_call(starts_at, _rv, _receiver, *_args, &_blk) ⇒ Object



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

def after_call(starts_at, _rv, _receiver, *_args, &_blk)
  runtime = Time.now - starts_at

  if max && runtime >= max
    error! "#{method_name} took longer than allowed: %.02f secs > %.02f secs." % [runtime, expected_runtime]
  end

  if runtime >= expected_runtime
    Contracts.logger.warn "#{method_name} took longer than expected: %.02f secs > %.02f secs." % [runtime, expected_runtime]
  end
end

#before_call(_receiver, *_args, &_blk) ⇒ Object



18
19
20
# File 'lib/contracts/runtime.rb', line 18

def before_call(_receiver, *_args, &_blk)
  Time.now
end

#loggerObject



34
35
36
# File 'lib/contracts/runtime.rb', line 34

def logger
  self.class.logger
end