Conditionator 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,6 +31,10 @@ module ConditionatorHooks
31
31
  conditions :post
32
32
  end
33
33
 
34
+ def failsafe_for method
35
+ self.class.data[self.class.name][method][:pre][:failsafe]
36
+ end
37
+
34
38
  #Creates all methods needed for the hooks to take place.
35
39
  def load_conditions
36
40
 
@@ -44,27 +48,30 @@ module ConditionatorHooks
44
48
 
45
49
  if !self.respond_to? "#{method}_with_#{_when}_cond".to_sym
46
50
  self.class.send :define_method, "#{method}_with_#{_when}_cond".to_sym do |*p|
51
+ execute_method = true
47
52
  if(_when == :pre)
48
53
  returns = arr_methods.collect do |m|
49
54
  self.send(m, *p) ? true : false
50
55
  end
51
56
  returns.uniq!
52
57
 
58
+ #if one of the preconditions returned false, we act accordingly
53
59
  if returns.include? false
54
- raise PreconditionsNotMet
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
55
66
  end
56
- #if returns.length == 1 and returns.include? true
57
- #data[:block].call(self) if !data[:block].nil?
58
- #else
59
- #raise PreconditionsNotMet
60
- #end
61
67
  end
62
- ret_value = self.send "#{method}_without_#{_when}_cond".to_sym, *p
68
+ if execute_method
69
+ ret_value = self.send "#{method}_without_#{_when}_cond".to_sym, *p
70
+ end
63
71
  if(_when == :post)
64
72
  arr_methods.each do |m|
65
73
  self.send m, *p, ret_value
66
74
  end
67
- #data[:block].call(self) if !data[:block].nil?
68
75
  end
69
76
  return ret_value
70
77
  end
@@ -79,7 +86,7 @@ module ConditionatorHooks
79
86
  private
80
87
 
81
88
  #Adds a pre or post condition to the list of conditions to be used
82
- def add_condition_for type, for_method, conditions
89
+ def add_condition_for type, for_method, conditions, options = {}
83
90
  key = self.name.to_s
84
91
  if conditions.is_a? Array
85
92
  condition_list = conditions
@@ -93,6 +100,9 @@ module ConditionatorHooks
93
100
 
94
101
  _data[key][for_method][type] = Hash.new if _data[key][for_method][type].nil?
95
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
+
96
106
  end
97
107
 
98
108
 
@@ -108,13 +118,13 @@ module ConditionatorHooks
108
118
  #Adds a precondition for the method 'method_name'
109
119
  #method_name can be an array of methods, in which case, we add the same
110
120
  #preconditions for all methos inside it.
111
- def precondition_for method_name, preconditions
121
+ def precondition_for method_name, preconditions, options = {}
112
122
  if method_name.is_a? Array
113
123
  method_name.each do |m|
114
- add_condition_for :pre, m, preconditions
124
+ add_condition_for :pre, m, preconditions, options
115
125
  end
116
126
  else
117
- add_condition_for :pre, method_name, preconditions
127
+ add_condition_for :pre, method_name, preconditions, options
118
128
  end
119
129
  end
120
130
 
data/lib/conditionator.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  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.2
5
+ VERSION = 0.3
6
6
 
7
7
  def self.included base
8
8
  base.send :include, ConditionatorHooks
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.2'
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-17 00:00:00.000000000Z
12
+ date: 2012-05-18 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: This gem allows you to set pre and post conditions to your methods, in
15
15
  order to assure their successfull excecution
@@ -20,7 +20,7 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - lib/conditionator.rb
22
22
  - lib/conditionator/hooks.rb
23
- homepage: https://github.com/deleteman/conditionatorr
23
+ homepage: https://github.com/deleteman/conditionator
24
24
  licenses: []
25
25
  post_install_message:
26
26
  rdoc_options: []