Conditionator 0.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ require 'conditionator/hooks'
2
2
 
3
3
  #Main module, including this module in your class will attach it the methods
4
4
  module Conditionator
5
- VERSION = 0.3
5
+ VERSION = "0.3.1"
6
6
 
7
7
  def self.included base
8
8
  base.send :include, ConditionatorHooks
@@ -1,141 +1,142 @@
1
1
  module ConditionatorHooks
2
2
 
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
- type.each { |_when, data|
46
-
47
- arr_methods = data[:methods]
48
-
49
- if !self.respond_to? "#{method}_with_#{_when}_cond".to_sym
50
- self.class.send :define_method, "#{method}_with_#{_when}_cond".to_sym do |*p|
51
- execute_method = true
52
- if(_when == :pre)
53
- returns = 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 !data[:failsafe].nil? #if we had setup a failsafe method, we use that
62
- ret_value = self.send(data[: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 !data[:mute]
65
- end
66
- end
67
- end
68
- if execute_method
69
- ret_value = self.send "#{method}_without_#{_when}_cond".to_sym, *p
70
- end
71
- if(_when == :post)
72
- arr_methods.each do |m|
73
- self.send m, *p, ret_value
74
- end
75
- end
76
- return ret_value
77
- end
78
- self.class.send :alias_method, "#{method}_without_#{_when}_cond".to_sym, method
79
- self.class.send :alias_method, method, "#{method}_with_#{_when}_cond".to_sym
80
- end
81
- }
82
- }
83
- end
84
-
85
- module ClassLevelConditionator
86
- private
87
-
88
- #Adds a pre or post condition to the list of conditions to be used
89
- def add_condition_for type, for_method, conditions, options = {}
90
- key = self.name.to_s
91
- if conditions.is_a? Array
92
- condition_list = conditions
93
- else
94
- condition_list = [conditions]
95
- end
96
-
97
- _data = data
98
- _data[key] = {} if _data[key].nil? #HookData.new if data[key].nil?
99
- _data[key][for_method] = Hash.new if _data[key][for_method].nil?
100
-
101
- _data[key][for_method][type] = Hash.new if _data[key][for_method][type].nil?
102
- _data[key][for_method][type][:methods] = condition_list
103
- _data[key][for_method][type][:failsafe] = options[:failsafe]
104
- _data[key][for_method][type][:mute] = options[:mute]
105
-
106
- end
107
-
108
-
109
- public
110
- def data
111
- @data = {} if @data.nil?
112
- @data
113
- end
114
- def data=(value)
115
- @data = value
116
- end
117
-
118
- #Adds a precondition for the method 'method_name'
119
- #method_name can be an array of methods, in which case, we add the same
120
- #preconditions for all methos inside it.
121
- def precondition_for method_name, preconditions, options = {}
122
- if method_name.is_a? Array
123
- method_name.each do |m|
124
- add_condition_for :pre, m, preconditions, options
125
- end
126
- else
127
- add_condition_for :pre, method_name, preconditions, options
128
- end
129
- end
130
-
131
- def postcondition_for method_name, conditions
132
- if method_name.is_a? Array
133
- method_name.each do |m|
134
- add_condition_for :post, m, conditions
135
- end
136
- else
137
- add_condition_for :post, method_name, conditions
138
- end
139
- end
140
- end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Conditionator
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: