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.
- data/README.rdoc +3 -2
- data/Rakefile +6 -7
- data/lib/useful/active_record_helpers/mysql_migration_helpers.rb +64 -65
- data/lib/useful/active_record_helpers.rb +1 -1
- data/lib/useful/cap_tasks/app_role_migrations.rb +41 -0
- data/lib/useful/cap_tasks/disable_migrate.rb +15 -0
- data/lib/useful/cap_tasks/git_query_revision_remote.rb +31 -0
- data/lib/useful/cap_tasks/passenger_deploy.rb +44 -0
- data/lib/useful/cap_tasks/rack_cache.rb +17 -0
- data/lib/useful/cap_tasks.rb +1 -3
- data/lib/useful/erb_helpers/common.rb +86 -0
- data/lib/useful/erb_helpers/forms.rb +118 -0
- data/lib/useful/erb_helpers/links.rb +156 -0
- data/lib/useful/erb_helpers/proper.rb +109 -0
- data/lib/useful/erb_helpers/tags.rb +64 -0
- data/lib/useful/erb_helpers.rb +3 -0
- data/lib/useful/rails_helpers/environment_tests.rb +59 -0
- data/lib/useful/rails_helpers/erb.rb +3 -0
- data/lib/useful/rails_helpers.rb +3 -0
- data/lib/useful/ruby_extensions/array.rb +33 -34
- data/lib/useful/ruby_extensions/date.rb +11 -11
- data/lib/useful/ruby_extensions/false_class.rb +16 -13
- data/lib/useful/ruby_extensions/fixnum.rb +50 -19
- data/lib/useful/ruby_extensions/hash.rb +157 -91
- data/lib/useful/ruby_extensions/nil_class.rb +26 -0
- data/lib/useful/ruby_extensions/numeric.rb +239 -229
- data/lib/useful/ruby_extensions/object.rb +126 -11
- data/lib/useful/ruby_extensions/string.rb +259 -33
- data/lib/useful/ruby_extensions/true_class.rb +16 -13
- data/lib/useful/ruby_extensions.rb +1 -1
- data/lib/useful/ruby_extensions_with_activesupport.rb +2 -0
- data/lib/useful/shoulda_macros/test_unit.rb +58 -0
- data/lib/useful/version.rb +2 -2
- data/lib/useful.rb +1 -1
- metadata +19 -38
- data/lib/useful/cap_tasks/cache.rb +0 -5
- data/lib/useful/cap_tasks/gemsconfig.rb +0 -14
- data/lib/useful/rails_extensions/environment_tests.rb +0 -60
- data/lib/useful/rails_extensions.rb +0 -3
- data/lib/useful/ruby_extensions_from_rails/date.rb +0 -244
- data/lib/useful/ruby_extensions_from_rails/duration.rb +0 -99
- data/lib/useful/ruby_extensions_from_rails/fixnum.rb +0 -32
- data/lib/useful/ruby_extensions_from_rails/hash.rb +0 -50
- data/lib/useful/ruby_extensions_from_rails/numeric.rb +0 -60
- data/lib/useful/ruby_extensions_from_rails/object.rb +0 -79
- data/lib/useful/ruby_extensions_from_rails/string.rb +0 -174
- data/lib/useful/ruby_extensions_from_rails/time.rb +0 -320
- data/lib/useful/ruby_extensions_from_rails.rb +0 -3
- data/lib/useful/sinatra_helpers/environment_tests.rb +0 -19
- data/lib/useful/sinatra_helpers/erb/error_pages.rb +0 -25
- data/lib/useful/sinatra_helpers/erb/forms.rb +0 -131
- data/lib/useful/sinatra_helpers/erb/helpers.rb +0 -45
- data/lib/useful/sinatra_helpers/erb/links.rb +0 -79
- data/lib/useful/sinatra_helpers/erb/partials.rb +0 -41
- data/lib/useful/sinatra_helpers/erb/tags.rb +0 -56
- data/lib/useful/sinatra_helpers/erb.rb +0 -3
- data/lib/useful/sinatra_helpers/mailer/base.rb +0 -89
- data/lib/useful/sinatra_helpers/mailer/exceptions.rb +0 -17
- data/lib/useful/sinatra_helpers/mailer/tls.rb +0 -73
- data/lib/useful/sinatra_helpers/mailer.rb +0 -3
- data/lib/useful/sinatra_helpers.rb +0 -3
@@ -1,111 +1,177 @@
|
|
1
|
-
|
1
|
+
module Useful; end
|
2
|
+
module Useful::RubyExtensions; end
|
2
3
|
|
3
|
-
|
4
|
-
|
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
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
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
|
-
|
102
|
-
|
103
|
-
|
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
|