Conditionator 0.3 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/conditionator.rb +1 -1
- data/lib/conditionator/hooks.rb +139 -138
- metadata +1 -1
data/lib/conditionator.rb
CHANGED
data/lib/conditionator/hooks.rb
CHANGED
@@ -1,141 +1,142 @@
|
|
1
1
|
module ConditionatorHooks
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
3
|
+
class PreconditionsNotMet < StandardError
|
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
|
+
def failsafe_for method
|
35
|
+
self.class.data[self.class.name][method][:pre][:failsafe]
|
36
|
+
end
|
37
|
+
|
38
|
+
#Creates all methods needed for the hooks to take place.
|
39
|
+
def load_conditions
|
40
|
+
|
41
|
+
_data = self.class.data
|
42
|
+
_data[self.class.name] = {} if _data[self.class.name].nil?
|
43
|
+
|
44
|
+
_data[self.class.name].each { |method, type|
|
45
|
+
|
46
|
+
pre_arr_methods = type[:pre][:methods] if !type[:pre].nil?
|
47
|
+
post_arr_methods = type[:post][:methods] if !type[:post].nil?
|
48
|
+
|
49
|
+
if !self.respond_to? "#{method}_with_cond".to_sym
|
50
|
+
self.class.send :define_method, "#{method}_with_cond".to_sym do |*p|
|
51
|
+
execute_method = true
|
52
|
+
if(type.key? :pre)
|
53
|
+
returns = pre_arr_methods.collect do |m|
|
54
|
+
self.send(m, *p) ? true : false
|
55
|
+
end
|
56
|
+
returns.uniq!
|
57
|
+
|
58
|
+
#if one of the preconditions returned false, we act accordingly
|
59
|
+
if returns.include? false
|
60
|
+
execute_method = false
|
61
|
+
if !type[:pre][:failsafe].nil? #if we had setup a failsafe method, we use that
|
62
|
+
ret_value = self.send(type[:pre][:failsafe], *p) #if we execute the failsafe, that method will give us the returning value
|
63
|
+
else #otherwise, we raise the exception if the dev didn't mute it
|
64
|
+
raise PreconditionsNotMet if !type[:pre][:mute]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
if execute_method
|
69
|
+
ret_value = self.send "#{method}_without_cond".to_sym, *p
|
70
|
+
end
|
71
|
+
if(type.key? :post)
|
72
|
+
if execute_method
|
73
|
+
post_arr_methods.each do |m|
|
74
|
+
self.send m, *p, ret_value
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
return ret_value
|
79
|
+
end
|
80
|
+
self.class.send :alias_method, "#{method}_without_cond".to_sym, method
|
81
|
+
self.class.send :alias_method, method, "#{method}_with_cond".to_sym
|
82
|
+
end
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
86
|
+
module ClassLevelConditionator
|
87
|
+
private
|
88
|
+
|
89
|
+
#Adds a pre or post condition to the list of conditions to be used
|
90
|
+
def add_condition_for type, for_method, conditions, options = {}
|
91
|
+
key = self.name.to_s
|
92
|
+
if conditions.is_a? Array
|
93
|
+
condition_list = conditions
|
94
|
+
else
|
95
|
+
condition_list = [conditions]
|
96
|
+
end
|
97
|
+
|
98
|
+
_data = data
|
99
|
+
_data[key] = {} if _data[key].nil? #HookData.new if data[key].nil?
|
100
|
+
_data[key][for_method] = Hash.new if _data[key][for_method].nil?
|
101
|
+
|
102
|
+
_data[key][for_method][type] = Hash.new if _data[key][for_method][type].nil?
|
103
|
+
_data[key][for_method][type][:methods] = condition_list
|
104
|
+
_data[key][for_method][type][:failsafe] = options[:failsafe]
|
105
|
+
_data[key][for_method][type][:mute] = options[:mute]
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
public
|
111
|
+
def data
|
112
|
+
@data = {} if @data.nil?
|
113
|
+
@data
|
114
|
+
end
|
115
|
+
def data=(value)
|
116
|
+
@data = value
|
117
|
+
end
|
118
|
+
|
119
|
+
#Adds a precondition for the method 'method_name'
|
120
|
+
#method_name can be an array of methods, in which case, we add the same
|
121
|
+
#preconditions for all methos inside it.
|
122
|
+
def precondition_for method_name, preconditions, options = {}
|
123
|
+
if method_name.is_a? Array
|
124
|
+
method_name.each do |m|
|
125
|
+
add_condition_for :pre, m, preconditions, options
|
126
|
+
end
|
127
|
+
else
|
128
|
+
add_condition_for :pre, method_name, preconditions, options
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def postcondition_for method_name, conditions, options = {}
|
133
|
+
if method_name.is_a? Array
|
134
|
+
method_name.each do |m|
|
135
|
+
add_condition_for :post, m, conditions, options
|
136
|
+
end
|
137
|
+
else
|
138
|
+
add_condition_for :post, method_name, conditions, options
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
141
142
|
end
|