Conditionator 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ require 'conditionator/hooks'
2
+
3
+ #Extends all objects to check for conditions
4
+ class Object
5
+ include Conditionator
6
+
7
+ class << self
8
+ alias_method :old_new, :new
9
+
10
+ def new *args
11
+ o = old_new(*args)
12
+ o.load_conditions
13
+ o
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,129 @@
1
+ module Conditionator
2
+
3
+ class PreconditionsNotMet < Exception
4
+ end
5
+
6
+ private
7
+ def conditions type
8
+ conds = {}
9
+ data = self.class.data
10
+
11
+ data[self.class.name].each do |method, filter|
12
+ if filter.key? type
13
+ conds[method] = [] if conds[method].nil?
14
+ conds[method] = filter[type][:methods]
15
+ end
16
+ end
17
+ conds
18
+ end
19
+
20
+ def self.included(base)
21
+ base.send :extend, ClassLevelConditionator
22
+ end
23
+
24
+ public
25
+ #Returns a list of all preconditions, grouped by the method they're a precondition to.
26
+ def preconditions
27
+ conditions :pre
28
+ end
29
+
30
+ def postconditions
31
+ conditions :post
32
+ end
33
+
34
+ #Creates all methods needed for the hooks to take place.
35
+ def load_conditions
36
+
37
+ _data = self.class.data
38
+ _data[self.class.name] = {} if _data[self.class.name].nil?
39
+
40
+ _data[self.class.name].each { |method, type|
41
+ type.each { |_when, data|
42
+
43
+ arr_methods = data[:methods]
44
+
45
+ if !self.respond_to? "#{method}_with_#{_when}_cond".to_sym
46
+ self.class.send :define_method, "#{method}_with_#{_when}_cond".to_sym do |*p|
47
+ if(_when == :pre)
48
+ returns = arr_methods.collect do |m|
49
+ self.send(m) ? true : false
50
+ end
51
+ returns.uniq!
52
+ if returns.length == 1 and returns.include? true
53
+ data[:block].call(self) if !data[:block].nil?
54
+ else
55
+ raise PreconditionsNotMet
56
+ end
57
+ end
58
+ self.send "#{method}_without_#{_when}_cond".to_sym, *p
59
+ if(_when == :post)
60
+ arr_methods.each do |m| self.send m end
61
+ data[:block].call(self) if !data[:block].nil?
62
+ end
63
+
64
+ end
65
+ self.class.send :alias_method, "#{method}_without_#{_when}_cond".to_sym, method
66
+ self.class.send :alias_method, method, "#{method}_with_#{_when}_cond".to_sym
67
+ end
68
+ }
69
+ }
70
+ end
71
+
72
+ module ClassLevelConditionator
73
+ private
74
+
75
+ #Adds a pre or post condition to the list of conditions to be used
76
+ def add_condition_for type, for_method, conditions
77
+ key = self.name.to_s
78
+ if conditions.is_a? Array
79
+ condition_list = conditions
80
+ else
81
+ condition_list = [conditions]
82
+ end
83
+
84
+ _data = data
85
+ _data[key] = {} if _data[key].nil? #HookData.new if data[key].nil?
86
+ _data[key][for_method] = Hash.new if _data[key][for_method].nil?
87
+
88
+ _data[key][for_method][type] = Hash.new if _data[key][for_method][type].nil?
89
+ _data[key][for_method][type][:methods] = condition_list
90
+ end
91
+
92
+
93
+ public
94
+ def data
95
+ @data = {} if @data.nil?
96
+ @data
97
+ end
98
+ def data=(value)
99
+ @data = value
100
+ end
101
+
102
+ #Adds a precondition for the method 'method_name'
103
+ #method_name can be an array of methods, in which case, we add the same
104
+ #preconditions for all methos inside it.
105
+ def precondition_for method_name, preconditions
106
+ if method_name.is_a? Array
107
+ method_name.each do |m|
108
+ add_condition_for :pre, m, preconditions
109
+ end
110
+ else
111
+ add_condition_for :pre, method_name, preconditions
112
+ end
113
+ end
114
+
115
+ def postcondition_for method_name, conditions
116
+ if method_name.is_a? Array
117
+ method_name.each do |m|
118
+ add_condition_for :post, m, conditions
119
+ end
120
+ else
121
+ add_condition_for :post, method_name, conditions
122
+ end
123
+ end
124
+
125
+
126
+ end
127
+
128
+
129
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Conditionator
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fernando Doglio
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-15 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: This gem allows you to set pre and post conditions to your methods, in
15
+ order to assure their successfull excecution
16
+ email: deleteman@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/conditionator.rb
22
+ - lib/conditionator/hooks.rb
23
+ homepage:
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.15
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Add pre and post conditions to your methods
47
+ test_files: []