Module: Minidbc

Defined in:
lib/minidbc.rb,
lib/minidbc/version.rb

Defined Under Namespace

Modules: ClassMethods, SubclassMethods Classes: Cond, DesignContractViolationException

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(clz) ⇒ Object



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

def self.included(clz)
  clz.extend ClassMethods
  clz.minidbc_init
end

Instance Method Details

#minidbc_call_wrap(method_name, new_method_name, check_pre, check_post, pre_check_invariants, post_check_invariants, *arg) ⇒ Object



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

def minidbc_call_wrap( method_name, new_method_name, check_pre, check_post, pre_check_invariants, post_check_invariants, *arg )
  minidbc_check_condition(minidbc_pre(method_name), *arg) if check_pre
  minidbc_check_condition(minidbc_invariants) if pre_check_invariants
  #TODO need to handle exception
  ret = send( new_method_name, *arg )
  minidbc_check_condition(minidbc_invariants) if post_check_invariants
  minidbc_check_condition(minidbc_post(method_name), ret, *arg) if check_post
  ret
end

#minidbc_check_condition(condition_list, *args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/minidbc.rb', line 12

def minidbc_check_condition( condition_list, *args )
  condition_list.each do |cond| 
    if !instance_exec(*args, &cond.blk) 
      ( file, lineno ) = cond.location.split(":")
      puts "Contract Violation at #{cond.location}"
      if File.exist? file
        puts File.open(file).readlines[lineno.to_i - 1]
      end
      raise DesignContractViolationException.new( "Contact failed at #{cond.location}" )
    end
  end
end

#minidbc_invariantsObject



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

def minidbc_invariants
  # byebug
  self.class.invariants
end

#minidbc_post(method_name) ⇒ Object



41
42
43
# File 'lib/minidbc.rb', line 41

def minidbc_post(method_name)
  self.class.postconds(method_name) || []
end

#minidbc_pre(method_name) ⇒ Object



37
38
39
# File 'lib/minidbc.rb', line 37

def minidbc_pre(method_name)
  self.class.preconds(method_name) || []
end