sequel_proc_error_handling.rb 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/sequel_proc_error_handling.rb +163 -0
  2. metadata +75 -0
@@ -0,0 +1,163 @@
1
+ module Sequel
2
+ module Plugins
3
+ # Proc Error Handling is a plugin to help handle validation errors
4
+ # by providing context specific error handling as proc objects.
5
+ #
6
+ # example:
7
+ #
8
+ # filter_restricted = proc do |klass, values|
9
+ # if $!.message =~ /doesn't exist or access is restricted to it/
10
+ # values.delete_if { |k,v| klass.restricted_columns.include? k }
11
+ # :retry
12
+ # end
13
+ # end
14
+ #
15
+ # Foo.new(:restricted_column => 'value') # raises error
16
+ # Foo.new(:restricted_column => 'value', filter_restricted) # No Error
17
+ #
18
+ # This plugin is helpful when you don't want to be changing a model's
19
+ # error settings to acomodate unverified data in one area of the code,
20
+ # but still want the strict errors in a more secure area of the code.
21
+ module ProcErrorHandling
22
+ def self.apply(model)
23
+ end
24
+
25
+ def self.configure(model,&block)
26
+ end
27
+
28
+ module InstanceMethods
29
+ def update(hash,*error_proc)
30
+ super(hash)
31
+ rescue
32
+ result = PEH.send(:process_error_proc,error_proc,self,hash)
33
+ retry if result == :retry
34
+ result
35
+ end
36
+
37
+ def update_all(hash, *error_proc)
38
+ super(hash)
39
+ rescue
40
+ result = PEH.send(:process_error_proc,error_proc,self,hash)
41
+ retry if result == :retry
42
+ result
43
+ end
44
+
45
+ def update_except(hash, *except)
46
+ error_procs = []
47
+ error_procs.unshift except.pop while except.last.is_a? Proc
48
+
49
+ # Only want to retry the update, don't want to clear error_procs
50
+ begin
51
+ super(hash,*except)
52
+ rescue
53
+ result = PEH.send(:process_error_proc,error_procs,self,hash)
54
+ retry if result == :retry
55
+ result
56
+ end
57
+ end
58
+
59
+ def update_only(hash, *only)
60
+ error_procs = []
61
+ error_procs.unshift only.pop while only.last.is_a? Proc
62
+
63
+ begin
64
+ super(hash,*only)
65
+ rescue
66
+ result = PEH.send(:process_error_proc,error_procs,self,hash)
67
+ retry if result == :retry
68
+ result
69
+ end
70
+ end
71
+
72
+ def initialize(values = {}, *args,&block)
73
+ orig = args.dup
74
+ error_procs = []
75
+ error_procs.unshift args.pop while args.last.is_a? Proc
76
+ from_db = args.pop || false # First value will be nil or boolean
77
+ raise ArgumentError,
78
+ "Invalid Arguments passed to #new #{orig.inpsect}" unless args.empty?
79
+
80
+ begin
81
+ super(values,from_db,&block)
82
+ rescue
83
+ result = PEH.send(:process_error_proc,error_procs,self,values)
84
+ retry if result == :retry
85
+ # Special for new since we can't return anything else
86
+ if result.is_a? self.class
87
+ values = result.values
88
+ from_db = true unless result.new?
89
+ retry
90
+ end
91
+ # Should not get here... means result was something other
92
+ # then :raise, :retry, nil, or an instance of self.class
93
+ raise "#new can not return any other object, there is" <<
94
+ " an error in your PEH proc"
95
+ end
96
+ end
97
+
98
+ def save(*columns)
99
+ error_procs = []
100
+ error_procs.unshift columns.pop while columns.last.is_a? Proc
101
+
102
+ begin
103
+ super(*columns)
104
+ rescue
105
+ result = PEH.send(:process_error_proc,error_procs,self,values)
106
+ retry if result == :retry
107
+ result
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ module ClassMethods
114
+ def create(values = {}, *error_proc, &block)
115
+ new(values,*error_proc, &block).save *error_proc
116
+ end
117
+
118
+ def on_error(&block)
119
+ @peh_error_block = block
120
+ end
121
+
122
+ def peh_error_occured(model)
123
+ if @peh_error_block
124
+ @peh_error_block.call(model)
125
+ elsif superclass.respond_to? :peh_error_occured
126
+ superclass.peh_error_occured(model)
127
+ end
128
+ end
129
+ end
130
+
131
+ module DatasetMethods
132
+ end
133
+
134
+ def self.process_error_proc(procs,obj,hash)
135
+ klass = obj.class
136
+ # if procs is nil then compact array and result will be nil
137
+ # if procs is single proc wrap in array so code runs normaly
138
+ # if procs is array execute each one till value returned
139
+ procs = [procs].compact unless procs.is_a? Array
140
+ result = procs.each do |ep|
141
+ val = ep.call(klass,hash)
142
+ break val unless val.nil?
143
+ end
144
+ # if result is the original array then error handling failed
145
+ result = (result == procs) ? nil : result
146
+
147
+ if result == :raise or result.nil?
148
+ klass.peh_error_occured(obj)
149
+ raise $!
150
+ end
151
+ if result != nil and result != :retry and result.class != obj.class
152
+ raise Sequel::Error, "An error handling proc must return either " <<
153
+ "nil, :raise, :retry, or an instance of the klass it is rescuing."
154
+ end
155
+ result
156
+ end
157
+
158
+ private_class_method :process_error_proc
159
+ end
160
+
161
+ PEH = ProcErrorHandling
162
+ end
163
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel_proc_error_handling.rb
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 5
8
+ - 0
9
+ version: 0.5.0
10
+ platform: ruby
11
+ authors:
12
+ - daicoden
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-11 00:00:00 -06:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: sequel
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 11
30
+ - 0
31
+ version: 3.11.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: " Proc Error Handling allows you to specify a proc for\n most of the database oriented operations. If an error\n occurs, the klass and values are passed to the proc.\n The proc may attempt to fix the error and retry, or\n pass the error on up the chain.\n"
35
+ email: daicoden@copypastel.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - lib/sequel_proc_error_handling.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/daicoden/sequel_proc_error_handling
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.6
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Proc based error recovery.
74
+ test_files: []
75
+