resque_safe 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Robert R. Meyer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = resque_safe
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to resque_safe
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Robert R. Meyer. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,66 @@
1
+ module ResqueSafe
2
+ module ConvertFromHash
3
+ def to_objects(inbound_hash)
4
+ objects = {}
5
+ inbound_hash.each do |klass_name,value|
6
+ klass_name, value = klass_name.to_s, value.to_s
7
+ klassified_klass = klass_name.classify
8
+ symbolized_klass = klass_name.to_sym
9
+
10
+ if Object.const_defined?(klassified_klass) &&
11
+ known_models.include?(klassified_klass.constantize)
12
+
13
+ objects[symbolized_klass] = return_active_record(klassified_klass, value)
14
+ else
15
+ objects[symbolized_klass] = return_other_object(klass_name, value)
16
+ end
17
+ end
18
+
19
+ objects
20
+ end
21
+
22
+ def args_to_instance_vars(args)
23
+ #take each key, if a model, make it an @#{model} = model.find(value)
24
+ #else make it a @#{name}=#{value}
25
+
26
+ args.each do |klass_name,value|
27
+ klass_name, value = klass_name.to_s, value.to_s
28
+ klassified_klass = klass_name.classify
29
+
30
+ if Object.const_defined?(klassified_klass) &&
31
+ known_models.include?(klassified_klass.constantize)
32
+
33
+ set_for_active_record(klassified_klass, value)
34
+ else
35
+ set_for_other(klass_name, value)
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+ def return_active_record(klass_name, value)
42
+ return self.instance_eval("#{klass_name.constantize}.find(#{value})")
43
+ end
44
+
45
+ def return_other_object(klass_name, value)
46
+ if value.is_a?(String)
47
+ return self.instance_eval("%|#{value}|")
48
+ else
49
+ return self.instance_eval(":#{instance_name} => #{value}")
50
+ end
51
+
52
+ end
53
+
54
+ def set_instance_for_active_record(klass_name, value)
55
+ self.instance_eval("@#{klass_name}= #{klass_name.constantize}.find(#{value})")
56
+ end
57
+
58
+ def set_instance_for_other(instance_name, value)
59
+ if value.is_a?(String)
60
+ self.instance_eval("@#{instance_name} = %|#{value}|")
61
+ else
62
+ self.instance_eval("@#{instance_name}=#{value}")
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,28 @@
1
+ module ResqueSafe
2
+ module ConvertToHash
3
+ def translate_to_hash(*args)
4
+ th = {}
5
+
6
+ args = args.flatten if args.respond_to?(:flatten)
7
+
8
+ args.each do |arg|
9
+ next if arg.blank?
10
+ arg_class = arg.class.name
11
+ if arg.is_a?(Hash)
12
+ arg.each do|k,v|
13
+ if v.respond_to?(:gsub)
14
+ th[k.to_sym] = CGI.escapeHTML(v)
15
+ else
16
+ th[k.to_sym] = v
17
+ end
18
+ end
19
+ elsif known_models.include?(arg_class) || arg.class.respond_to?(:column_names)
20
+ th[arg_class.underscore.to_sym] = arg.id
21
+ else
22
+ raise ArgumentError.new("Unknown #{arg_class} passed to resque safe. \n Object #{arg.inspect} \n\n Args #{args} \n\n Known Models #{known_models}")
23
+ end
24
+ end
25
+ th
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ module ResqueSafe
2
+ module Util
3
+ def known_models
4
+ @known_models if @known_models
5
+ @known_models = ActiveRecord::Base.send( :descendants ).flatten.uniq
6
+ @known_models += @known_models.collect{|sub_class| sub_class.send(:descendants) }.flatten.uniq
7
+ @known_models.uniq!
8
+ @known_models
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ require 'resque_safe/convert_to_hash'
2
+ require 'resque_safe/convert_from_hash'
3
+ require 'resque_safe/util'
4
+
5
+ module ResqueSafe
6
+ extend Util
7
+ extend ConvertFromHash
8
+ extend ConvertToHash
9
+ end
metadata ADDED
@@ -0,0 +1,232 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque_safe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Robert R. Meyer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: atomic
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: i18n
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: tzinfo
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: minitest
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '4.2'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '4.2'
94
+ - !ruby/object:Gem::Dependency
95
+ name: thread_safe
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rdoc
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '3.12'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: '3.12'
126
+ - !ruby/object:Gem::Dependency
127
+ name: bundler
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: '1.0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: '1.0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: jeweler
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: '1.6'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: '1.6'
158
+ - !ruby/object:Gem::Dependency
159
+ name: simplecov
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ - !ruby/object:Gem::Dependency
175
+ name: reek
176
+ requirement: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ~>
180
+ - !ruby/object:Gem::Version
181
+ version: 1.2.8
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ~>
188
+ - !ruby/object:Gem::Version
189
+ version: 1.2.8
190
+ description: ! ' longer description of your gem'
191
+ email: Blue.Dog.Archolite@gmail.com
192
+ executables: []
193
+ extensions: []
194
+ extra_rdoc_files:
195
+ - LICENSE.txt
196
+ - README.rdoc
197
+ files:
198
+ - lib/resque_safe.rb
199
+ - lib/resque_safe/convert_from_hash.rb
200
+ - lib/resque_safe/convert_to_hash.rb
201
+ - lib/resque_safe/util.rb
202
+ - LICENSE.txt
203
+ - README.rdoc
204
+ homepage: http://github.com/Blue-Dog-Archolite/resque_safe
205
+ licenses:
206
+ - MIT
207
+ post_install_message:
208
+ rdoc_options: []
209
+ require_paths:
210
+ - lib
211
+ required_ruby_version: !ruby/object:Gem::Requirement
212
+ none: false
213
+ requirements:
214
+ - - ! '>='
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ segments:
218
+ - 0
219
+ hash: -1972537241754522031
220
+ required_rubygems_version: !ruby/object:Gem::Requirement
221
+ none: false
222
+ requirements:
223
+ - - ! '>='
224
+ - !ruby/object:Gem::Version
225
+ version: '0'
226
+ requirements: []
227
+ rubyforge_project:
228
+ rubygems_version: 1.8.25
229
+ signing_key:
230
+ specification_version: 3
231
+ summary: one-line summary of your gem
232
+ test_files: []