kelredd-useful 0.1.25 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. data/README.rdoc +3 -2
  2. data/Rakefile +6 -7
  3. data/lib/useful/active_record_helpers/mysql_migration_helpers.rb +64 -65
  4. data/lib/useful/active_record_helpers.rb +1 -1
  5. data/lib/useful/cap_tasks/app_role_migrations.rb +41 -0
  6. data/lib/useful/cap_tasks/disable_migrate.rb +15 -0
  7. data/lib/useful/cap_tasks/git_query_revision_remote.rb +31 -0
  8. data/lib/useful/cap_tasks/passenger_deploy.rb +44 -0
  9. data/lib/useful/cap_tasks/rack_cache.rb +17 -0
  10. data/lib/useful/cap_tasks.rb +1 -3
  11. data/lib/useful/erb_helpers/common.rb +86 -0
  12. data/lib/useful/erb_helpers/forms.rb +118 -0
  13. data/lib/useful/erb_helpers/links.rb +156 -0
  14. data/lib/useful/erb_helpers/proper.rb +109 -0
  15. data/lib/useful/erb_helpers/tags.rb +64 -0
  16. data/lib/useful/erb_helpers.rb +3 -0
  17. data/lib/useful/rails_helpers/environment_tests.rb +59 -0
  18. data/lib/useful/rails_helpers/erb.rb +3 -0
  19. data/lib/useful/rails_helpers.rb +3 -0
  20. data/lib/useful/ruby_extensions/array.rb +33 -34
  21. data/lib/useful/ruby_extensions/date.rb +11 -11
  22. data/lib/useful/ruby_extensions/false_class.rb +16 -13
  23. data/lib/useful/ruby_extensions/fixnum.rb +50 -19
  24. data/lib/useful/ruby_extensions/hash.rb +157 -91
  25. data/lib/useful/ruby_extensions/nil_class.rb +26 -0
  26. data/lib/useful/ruby_extensions/numeric.rb +239 -229
  27. data/lib/useful/ruby_extensions/object.rb +126 -11
  28. data/lib/useful/ruby_extensions/string.rb +259 -33
  29. data/lib/useful/ruby_extensions/true_class.rb +16 -13
  30. data/lib/useful/ruby_extensions.rb +1 -1
  31. data/lib/useful/ruby_extensions_with_activesupport.rb +2 -0
  32. data/lib/useful/shoulda_macros/test_unit.rb +58 -0
  33. data/lib/useful/version.rb +2 -2
  34. data/lib/useful.rb +1 -1
  35. metadata +19 -38
  36. data/lib/useful/cap_tasks/cache.rb +0 -5
  37. data/lib/useful/cap_tasks/gemsconfig.rb +0 -14
  38. data/lib/useful/rails_extensions/environment_tests.rb +0 -60
  39. data/lib/useful/rails_extensions.rb +0 -3
  40. data/lib/useful/ruby_extensions_from_rails/date.rb +0 -244
  41. data/lib/useful/ruby_extensions_from_rails/duration.rb +0 -99
  42. data/lib/useful/ruby_extensions_from_rails/fixnum.rb +0 -32
  43. data/lib/useful/ruby_extensions_from_rails/hash.rb +0 -50
  44. data/lib/useful/ruby_extensions_from_rails/numeric.rb +0 -60
  45. data/lib/useful/ruby_extensions_from_rails/object.rb +0 -79
  46. data/lib/useful/ruby_extensions_from_rails/string.rb +0 -174
  47. data/lib/useful/ruby_extensions_from_rails/time.rb +0 -320
  48. data/lib/useful/ruby_extensions_from_rails.rb +0 -3
  49. data/lib/useful/sinatra_helpers/environment_tests.rb +0 -19
  50. data/lib/useful/sinatra_helpers/erb/error_pages.rb +0 -25
  51. data/lib/useful/sinatra_helpers/erb/forms.rb +0 -131
  52. data/lib/useful/sinatra_helpers/erb/helpers.rb +0 -45
  53. data/lib/useful/sinatra_helpers/erb/links.rb +0 -79
  54. data/lib/useful/sinatra_helpers/erb/partials.rb +0 -41
  55. data/lib/useful/sinatra_helpers/erb/tags.rb +0 -56
  56. data/lib/useful/sinatra_helpers/erb.rb +0 -3
  57. data/lib/useful/sinatra_helpers/mailer/base.rb +0 -89
  58. data/lib/useful/sinatra_helpers/mailer/exceptions.rb +0 -17
  59. data/lib/useful/sinatra_helpers/mailer/tls.rb +0 -73
  60. data/lib/useful/sinatra_helpers/mailer.rb +0 -3
  61. data/lib/useful/sinatra_helpers.rb +0 -3
@@ -1,111 +1,177 @@
1
- require 'json' unless defined?(JSON) && defined?(JSON::parse)
1
+ module Useful; end
2
+ module Useful::RubyExtensions; end
2
3
 
3
- module Useful
4
- module RubyExtensions
5
- module Hash
4
+ require 'json' unless defined?(JSON) && defined?(JSON::parse)
5
+ require 'useful/ruby_extensions/string' unless ::String.new.respond_to?(:cgi_escape)
6
6
 
7
- module ClassMethods; end
8
- def self.included(klass)
9
- klass.extend(ClassMethods) if klass.kind_of?(Class)
10
- end
7
+ module Useful::RubyExtensions::Hash
8
+
9
+ module ClassMethods
10
+
11
+ def from_json(string)
12
+ JSON.parse(string)
13
+ end
14
+
15
+ # Return the hash with only keys in *keys
16
+ def only(hash, *keys)
17
+ s_keys = keys.flatten.collect{|k| k.to_s}
18
+ hash.delete_if{ |k,v| !keys.flatten.include?(k) && !s_keys.include?(k) }
19
+ hash
20
+ end
11
21
 
12
- module ClassMethods
22
+ # Return the hash with only keys NOT in *keys
23
+ def except(hash, *keys)
24
+ s_keys = keys.flatten.collect{|k| k.to_s}
25
+ hash.delete_if{ |k,v| keys.flatten.include?(k) || s_keys.include?(k) }
26
+ hash
27
+ end
13
28
 
14
- def only(hash, *keys)
15
- hash.delete_if{ |k,v| !keys.flatten.include?(k) }
16
- hash
17
- end
18
-
19
- def except(hash, *keys)
20
- hash.delete_if{ |k,v| keys.flatten.include?(k) }
21
- hash
22
- end
23
-
24
- def from_json(string)
25
- JSON.parse(string)
29
+ # takes any empty values and makes them nil
30
+ def nillify(hash)
31
+ hash.each do |key,value|
32
+ if !value.nil? && ( (value.respond_to?('empty?') && value.empty?) || (value.respond_to?('to_s') && value.to_s.empty?) )
33
+ hash[key] = nil
26
34
  end
27
-
28
35
  end
36
+ hash
37
+ end
38
+
39
+ end
40
+
41
+ module InstanceMethods
42
+
43
+ def only(*keys)
44
+ self.class.only(self.clone, keys)
45
+ end
46
+ def only!(*keys)
47
+ self.class.only(self, keys)
48
+ end
29
49
 
30
- # Return a new hash with only keys in *keys
31
- def only(*keys)
32
- self.class.only(self.clone, keys)
33
- end
34
- # Destructively remove all keys not in *keys
35
- def only!(*keys)
36
- self.class.only(self, keys)
37
- end
50
+ def except(*keys)
51
+ self.class.except(self.clone, keys)
52
+ end
53
+
54
+ def except!(*keys)
55
+ self.class.except(self, keys)
56
+ end
38
57
 
39
- # Return a new hash with only keys not in *keys
40
- def except(*keys)
41
- self.class.except(self.clone, keys)
42
- end
43
- # Destructively remove all keys in *keys
44
- def except!(*keys)
45
- self.class.except(self, keys)
46
- end
47
-
48
- # Returns the value for the provided key(s). Allows searching in nested hashes
49
- def get_value(*keys)
50
- val = self[keys.first] || self[keys.first.to_s]
51
- val = self[keys.first.to_s.intern] unless val || keys.first.to_s.empty? || keys.first.kind_of?(Symbol)
52
- val.kind_of?(Hash) && keys.length > 1 ? val.get_value?(keys[1..-1]) : val
53
- end
54
- # Determines if a value exists for the provided key(s). Allows searching in nested hashes
55
- def check_value?(*keys)
56
- val = self.get_value(keys)
57
- val && !val.empty? ? true : false
58
- end
58
+ def nillify
59
+ self.class.nillify(self.clone)
60
+ end
61
+ def nillify!
62
+ self.class.nillify(self)
63
+ end
59
64
 
60
- # takes any empty values and makes them nil inline
61
- def nillify!
62
- self.each { |key,value| self[key] = nil if !value.nil? && value.to_s.empty? }
65
+ # Returns the value for the provided key(s). Allows searching in nested hashes
66
+ def get_value(*keys_array)
67
+ keys = keys_array.flatten
68
+ if !keys.respond_to?('empty?') || keys.empty?
69
+ nil
70
+ else
71
+ val = self[keys.shift]
72
+ val.respond_to?('get_value') && !keys.empty? ? val.get_value(keys) : val
63
73
  end
74
+ end
75
+ alias search get_value
76
+
77
+ # Determines if a value exists for the provided key(s). Allows searching in nested hashes
78
+ def check_value?(*keys_array)
79
+ val = self.get_value(keys_array)
80
+ val && !val.empty? ? true : false
81
+ end
64
82
 
65
- # Returns string formatted for HTTP URL encoded name-value pairs.
66
- # For example,
67
- # {:id => 'thomas_hardy'}.to_http_query_str
68
- # # => "?id=thomas_hardy"
69
- # {:id => 23423, :since => Time.now}.to_http_query_str
70
- # # => "?since=Thu,%2021%20Jun%202007%2012:10:05%20-0500&id=23423"
71
- # {:id => [1,2]}.to_http_query_str
72
- # # => "?id[]=1&id[]=2"
73
- # {:poo => {:foo => 1, :bar => 2}}.to_http_query_str
74
- # # => "?poo[bar]=2&poo[foo]=1"
75
- # {:poo => {:foo => 1, :bar => {:bar1 => 1, :bar2 => "nasty"}}}.to_http_query_str
76
- # "?poo[bar][bar1]=1&poo[bar][bar2]=nasty&poo[foo]=1"
77
- unless {}.respond_to?(:to_http_query_str)
78
- def to_http_query_str(opts = {})
79
- require 'cgi' unless defined?(::CGI) && defined?(::CGI::escape)
80
- opts[:prepend] ||= '?'
81
- opts[:append] ||= ''
82
- opts[:key_ns] ||= nil
83
- opt_strings = self.collect do |key, val|
84
- key_s = opts[:key_ns] ? "#{opts[:key_ns]}[#{key.to_s}]" : key.to_s
85
- if val.kind_of?(::Array)
86
- val.collect{|i| "#{key_s}[]=#{::CGI.escape(i.to_s)}"}.join('&')
87
- elsif val.kind_of?(::Hash)
88
- val.to_http_query_str({
89
- :prepend => '',
90
- :key_ns => key_s,
91
- :append => ''
92
- })
93
- else
94
- "#{key_s}=#{::CGI.escape(val.to_s)}"
95
- end
96
- end
97
- self.empty? ? '' : "#{opts[:prepend]}#{opt_strings.join('&')}#{opts[:append]}"
83
+ # Returns string formatted for HTTP URL encoded name-value pairs.
84
+ # For example,
85
+ # {:id => 'thomas_hardy'}.to_http_query_str
86
+ # # => "?id=thomas_hardy"
87
+ # {:id => 23423, :since => Time.now}.to_http_query_str
88
+ # # => "?since=Thu,%2021%20Jun%202007%2012:10:05%20-0500&id=23423"
89
+ # {:id => [1,2]}.to_http_query_str
90
+ # # => "?id[]=1&id[]=2"
91
+ # {:poo => {:foo => 1, :bar => 2}}.to_http_query_str
92
+ # # => "?poo[bar]=2&poo[foo]=1"
93
+ # {:poo => {:foo => 1, :bar => {:bar1 => 1, :bar2 => "nasty"}}}.to_http_query_str
94
+ # "?poo[bar][bar1]=1&poo[bar][bar2]=nasty&poo[foo]=1"
95
+ def to_http_query_str(opts = {})
96
+ opts[:prepend] ||= '?'
97
+ opts[:append] ||= ''
98
+ opts[:key_ns] ||= nil
99
+ opt_strings = self.sort{|a,b| a[0].to_s <=> b[0].to_s}.collect do |key_val|
100
+ key = key_val[0]
101
+ val = key_val[1]
102
+ key_s = opts[:key_ns] ? "#{opts[:key_ns]}[#{key.to_s}]" : key.to_s
103
+ if val.kind_of?(::Array)
104
+ val.sort.collect{|i| "#{key_s}[]=#{i.to_s.cgi_escape}"}.join('&')
105
+ elsif val.respond_to?('to_http_query_str')
106
+ val.to_http_query_str({
107
+ :prepend => '',
108
+ :key_ns => key_s,
109
+ :append => ''
110
+ })
111
+ else
112
+ "#{key_s}=#{val.to_s.cgi_escape}"
98
113
  end
99
- end
114
+ end
115
+ self.empty? ? '' : "#{opts[:prepend]}#{opt_strings.join('&')}#{opts[:append]}"
116
+ end
117
+
118
+ def to_html_attrs
119
+ self.empty? ? '' : self.sort{|a,b| a[0].to_s <=> b[0].to_s}.collect{|k_v| "#{k_v[0]}=\"#{k_v[1]}\""}.join(' ')
120
+ end
121
+
122
+ end
123
+
124
+ module FromActivesupport
125
+
126
+ module ClassMethods
100
127
 
101
- def to_html_attrs
102
- self.empty? ? '' : self.collect{|key, val| "#{key}=\"#{val}\""}.join(' ')
103
- end
128
+ # inspired by ActiveSupport::CoreExtensions::Hash::Keys (http://api.rubyonrails.org/)
129
+ def stringify_keys(hash)
130
+ hash.keys.each{ |key| hash[(key.to_s rescue key)] ||= hash.delete(key) }
131
+ hash
132
+ end unless ::Hash.respond_to?('stringify_keys')
104
133
 
134
+ # inspired by ActiveSupport::CoreExtensions::Hash::Keys (http://api.rubyonrails.org/)
135
+ def symbolize_keys(hash)
136
+ hash.keys.each{ |key| hash[(key.to_sym rescue key)] ||= hash.delete(key) }
137
+ hash
138
+ end unless ::Hash.respond_to?('symbolize_keys')
139
+
105
140
  end
141
+
142
+ module InstanceMethods
143
+
144
+ # Return a new hash with all keys converted to strings.
145
+ def stringify_keys
146
+ self.class.stringify_keys(self.clone)
147
+ end unless {}.respond_to?('stringify_keys')
148
+ # Destructively convert all keys to strings.
149
+ def stringify_keys!
150
+ self.class.stringify_keys(self)
151
+ end unless {}.respond_to?('stringify_keys!')
152
+
153
+ # Return a new hash with all keys converted to strings.
154
+ def symbolize_keys
155
+ self.class.symbolize_keys(self.clone)
156
+ end unless {}.respond_to?('symbolize_keys')
157
+ # Destructively convert all keys to strings.
158
+ def symbolize_keys!
159
+ self.class.symbolize_keys(self)
160
+ end unless {}.respond_to?('symbolize_keys!')
161
+
162
+ end
163
+
164
+ end
165
+
166
+ def self.included(receiver)
167
+ receiver.extend ClassMethods
168
+ receiver.extend FromActivesupport::ClassMethods
169
+ receiver.send :include, InstanceMethods
170
+ receiver.send :include, FromActivesupport::InstanceMethods
106
171
  end
172
+
107
173
  end
108
174
 
109
175
  class Hash
110
- include Useful::RubyExtensions::Hash
176
+ include Useful::RubyExtensions::Hash
111
177
  end
@@ -0,0 +1,26 @@
1
+ module Useful; end
2
+ module Useful::RubyExtensions; end
3
+
4
+ module Useful::RubyExtensions::NilClass
5
+
6
+ module FromActivesupport
7
+
8
+ def try(*args, &block)
9
+ nil
10
+ end unless nil.respond_to?('try')
11
+
12
+ def to_boolean
13
+ false
14
+ end unless nil.respond_to?('to_boolean')
15
+
16
+ end
17
+
18
+ def self.included(receiver)
19
+ receiver.send :include, FromActivesupport
20
+ end
21
+
22
+ end
23
+
24
+ class NilClass
25
+ include Useful::RubyExtensions::NilClass
26
+ end