Class: Protocol::Postcondition

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/post_condition.rb

Overview

This class is a proxy that stores postcondition blocks, which are called after the result of the wrapped method was determined.

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Postcondition

Returns a new instance of Postcondition.



9
10
11
12
# File 'lib/protocol/post_condition.rb', line 9

def initialize(object)
  @object = object
  @blocks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*a, &b) ⇒ Object

Send all remaining messages to the object.



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

def method_missing(*a, &b)
  @object.__send__(*a, &b)
end

Instance Method Details

#__add__(block) ⇒ Object



46
47
48
49
# File 'lib/protocol/post_condition.rb', line 46

def __add__(block)
  @blocks << block
  self
end

#__check__Object



42
43
44
# File 'lib/protocol/post_condition.rb', line 42

def __check__
  @blocks.all? { |block| instance_eval(&block) }
end

#__result__Object

This is the alternative result “keyword”.



15
16
17
# File 'lib/protocol/post_condition.rb', line 15

def __result__
  @result
end

#__result__=(result) ⇒ Object

:stopdoc:



38
39
40
# File 'lib/protocol/post_condition.rb', line 38

def __result__=(result)
  @result = result
end

#myselfObject

This is the “keyword” to be used instead of self to refer to current object.



33
34
35
# File 'lib/protocol/post_condition.rb', line 33

def myself
  @object
end

#resultObject

This is the result “keyword” which can be used to query the result of wrapped method in a postcondition clause.



21
22
23
24
25
26
27
28
29
# File 'lib/protocol/post_condition.rb', line 21

def result
  if @object.respond_to? :result
    warn "#{@object.class} already defines a result method, "\
      "try __result__ instead"
    @object.__send__(:result)
  else
    @result
  end
end