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,244 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'object') unless Object.new.respond_to?(:acts_like?)
2
- require File.join(File.dirname(__FILE__), 'duration')
3
-
4
- module Useful
5
- module RubyExtensionsFromRails
6
- module Date
7
-
8
- DATE_FORMATS = {
9
- :short => "%e %b",
10
- :long => "%B %e, %Y",
11
- :db => "%Y-%m-%d",
12
- :number => "%Y%m%d",
13
- :long_ordinal => lambda { |date| date.strftime("%B #{date.day.ordinalize}, %Y") }, # => "April 25th, 2007"
14
- :rfc822 => "%e %b %Y"
15
- }
16
-
17
- module ClassMethods; end
18
- def self.included(klass)
19
- klass.extend(ClassMethods) if klass.kind_of?(Class)
20
-
21
- klass.instance_eval do
22
- alias_method :to_default_s, :to_s
23
- alias_method :to_s, :to_formatted_s
24
- alias_method :default_inspect, :inspect
25
- alias_method :inspect, :readable_inspect
26
-
27
- # Ruby 1.9 has Date#to_time which converts to localtime only.
28
- remove_method :to_time if klass.instance_methods.include?(:to_time)
29
-
30
- # Ruby 1.9 has Date#xmlschema which converts to a string without the time component.
31
- remove_method :xmlschema if klass.instance_methods.include?(:xmlschema)
32
- end
33
- end
34
-
35
- module ClassMethods
36
-
37
- # Returns a new Date representing the date 1 day ago (i.e. yesterday's date).
38
- def yesterday
39
- ::Date.today.yesterday
40
- end
41
-
42
- # Returns a new Date representing the date 1 day after today (i.e. tomorrow's date).
43
- def tomorrow
44
- ::Date.today.tomorrow
45
- end
46
-
47
- # Returns Date.today.
48
- def current
49
- ::Date.today
50
- end
51
-
52
- end
53
-
54
- # Enable more predictable duck-typing on Date-like classes. See
55
- # Object#acts_like?.
56
- def acts_like_date?
57
- true
58
- end
59
-
60
- # Tells whether the Date object's date lies in the past
61
- def past?
62
- self < ::Date.current
63
- end
64
-
65
- # Tells whether the Date object's date is today
66
- def today?
67
- self.to_date == ::Date.current # we need the to_date because of DateTime
68
- end
69
-
70
- # Tells whether the Date object's date lies in the future
71
- def future?
72
- self > ::Date.current
73
- end
74
-
75
- # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
76
- # any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
77
- def advance(options)
78
- d = self
79
- d = d >> options.delete(:years) * 12 if options[:years]
80
- d = d >> options.delete(:months) if options[:months]
81
- d = d + options.delete(:weeks) * 7 if options[:weeks]
82
- d = d + options.delete(:days) if options[:days]
83
- d
84
- end
85
-
86
- # Returns a new Date where one or more of the elements have been changed according to the +options+ parameter.
87
- #
88
- # Examples:
89
- #
90
- # Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 1)
91
- # Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)
92
- def change(options)
93
- ::Date.new(
94
- options[:year] || self.year,
95
- options[:month] || self.month,
96
- options[:day] || self.day
97
- )
98
- end
99
-
100
- # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
101
- def beginning_of_day
102
- to_time
103
- end
104
- alias :midnight :beginning_of_day
105
- alias :at_midnight :beginning_of_day
106
- alias :at_beginning_of_day :beginning_of_day
107
-
108
- # Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
109
- def end_of_day
110
- to_time.end_of_day
111
- end
112
- alias :at_end_of_day :end_of_day
113
-
114
- # Returns a new Date representing the "start" of this week (i.e, Monday; DateTime objects will have time set to 0:00)
115
- def beginning_of_week
116
- days_to_monday = self.wday!=0 ? self.wday-1 : 6
117
- self - days_to_monday
118
- end
119
- alias :monday :beginning_of_week
120
- alias :at_beginning_of_week :beginning_of_week
121
-
122
- # Returns a new Date representing the end of this week (Sunday, DateTime objects will have time set to 23:59:59)
123
- def end_of_week
124
- days_to_sunday = self.wday!=0 ? 7-self.wday : 0
125
- self + days_to_sunday.days
126
- end
127
- alias :at_end_of_week :end_of_week
128
-
129
- # Returns a new Date representing the start of the given day in next week (default is Monday).
130
- def next_week(day = :monday)
131
- days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
132
- (self + 7).beginning_of_week + days_into_week[day]
133
- end
134
-
135
- # Returns a new Date representing the start of the month (1st of the month)
136
- def beginning_of_month
137
- change(:day => 1)
138
- end
139
- alias :at_beginning_of_month :beginning_of_month
140
-
141
- # Returns a new Date representing the end of the month (last day of the month)
142
- def end_of_month
143
- change(:day => ::Time.days_in_month( self.month, self.year ))
144
- end
145
- alias :at_end_of_month :end_of_month
146
-
147
- # Returns a new Date/DateTime representing the start of the quarter (1st of january, april, july, october; DateTime objects will have time set to 0:00)
148
- def beginning_of_quarter
149
- beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
150
- end
151
- alias :at_beginning_of_quarter :beginning_of_quarter
152
-
153
- # Returns a new Date/DateTime representing the end of the quarter (last day of march, june, september, december; DateTime objects will have time set to 23:59:59)
154
- def end_of_quarter
155
- beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month
156
- end
157
- alias :at_end_of_quarter :end_of_quarter
158
-
159
- # Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00)
160
- def beginning_of_year
161
- change(:month => 1, :day => 1)
162
- end
163
- alias :at_beginning_of_year :beginning_of_year
164
-
165
- # Returns a new Time representing the end of the year (31st of december; DateTime objects will have time set to 23:59:59)
166
- def end_of_year
167
- change(:month => 12, :day => 31)
168
- end
169
- alias :at_end_of_year :end_of_year
170
-
171
- # Convenience method which returns a new Date/DateTime representing the time 1 day ago
172
- def yesterday
173
- self - 1
174
- end
175
-
176
- # Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time
177
- def tomorrow
178
- self + 1
179
- end
180
-
181
- # Convert to a formatted string. See DATE_FORMATS for predefined formats.
182
- #
183
- # This method is aliased to <tt>to_s</tt>.
184
- #
185
- # ==== Examples
186
- # date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
187
- #
188
- # date.to_formatted_s(:db) # => "2007-11-10"
189
- # date.to_s(:db) # => "2007-11-10"
190
- #
191
- # date.to_formatted_s(:short) # => "10 Nov"
192
- # date.to_formatted_s(:long) # => "November 10, 2007"
193
- # date.to_formatted_s(:long_ordinal) # => "November 10th, 2007"
194
- # date.to_formatted_s(:rfc822) # => "10 Nov 2007"
195
- #
196
- # == Adding your own time formats to to_formatted_s
197
- # You can add your own formats to the Date::DATE_FORMATS hash.
198
- # Use the format name as the hash key and either a strftime string
199
- # or Proc instance that takes a date argument as the value.
200
- #
201
- # # config/initializers/time_formats.rb
202
- # Date::DATE_FORMATS[:month_and_year] = "%B %Y"
203
- # Date::DATE_FORMATS[:short_ordinal] = lambda { |date| date.strftime("%B #{date.day.ordinalize}") }
204
- def to_formatted_s(format = :default)
205
- if formatter = DATE_FORMATS[format]
206
- if formatter.respond_to?(:call)
207
- formatter.call(self).to_s
208
- else
209
- strftime(formatter)
210
- end
211
- else
212
- to_default_s
213
- end
214
- end
215
-
216
- # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
217
- def readable_inspect
218
- strftime("%a, %d %b %Y")
219
- end
220
-
221
- # Converts a Date instance to a Time, where the time is set to the beginning of the day.
222
- # The timezone can be either :local or :utc (default :local).
223
- #
224
- # ==== Examples
225
- # date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
226
- #
227
- # date.to_time # => Sat Nov 10 00:00:00 0800 2007
228
- # date.to_time(:local) # => Sat Nov 10 00:00:00 0800 2007
229
- #
230
- # date.to_time(:utc) # => Sat Nov 10 00:00:00 UTC 2007
231
- def to_time(form = :local)
232
- ::Time.send("#{form}_time", year, month, day)
233
- end
234
-
235
- def xmlschema
236
- to_time.xmlschema
237
- end
238
- end
239
- end
240
- end
241
-
242
- class Date
243
- include Useful::RubyExtensionsFromRails::Date
244
- end
@@ -1,99 +0,0 @@
1
- module Useful
2
- module RubyExtensionsFromRails
3
- # Provides accurate date and time measurements using Date#advance and
4
- # Time#advance, respectively. It mainly supports the methods on Numeric,
5
- # such as in this example:
6
- #
7
- # 1.month.ago # equivalent to Time.now.advance(:months => -1)
8
- class Duration
9
- attr_accessor :value, :parts
10
-
11
- def initialize(value, parts) #:nodoc:
12
- @value, @parts = value, parts
13
- end
14
-
15
- # Adds another Duration or a Numeric to this Duration. Numeric values
16
- # are treated as seconds.
17
- def +(other)
18
- if Duration === other
19
- Duration.new(value + other.value, @parts + other.parts)
20
- else
21
- Duration.new(value + other, @parts + [[:seconds, other]])
22
- end
23
- end
24
-
25
- # Subtracts another Duration or a Numeric from this Duration. Numeric
26
- # values are treated as seconds.
27
- def -(other)
28
- self + (-other)
29
- end
30
-
31
- def -@ #:nodoc:
32
- Duration.new(-value, parts.map { |type,number| [type, -number] })
33
- end
34
-
35
- def is_a?(klass) #:nodoc:
36
- klass == Duration || super
37
- end
38
-
39
- # Returns true if <tt>other</tt> is also a Duration instance with the
40
- # same <tt>value</tt>, or if <tt>other == value</tt>.
41
- def ==(other)
42
- if Duration === other
43
- other.value == value
44
- else
45
- other == value
46
- end
47
- end
48
-
49
- def self.===(other) #:nodoc:
50
- other.is_a?(Duration) rescue super
51
- end
52
-
53
- # Calculates a new Time or Date that is as far in the future
54
- # as this Duration represents.
55
- def since(time = ::Time.current)
56
- sum(1, time)
57
- end
58
- alias :from_now :since
59
-
60
- # Calculates a new Time or Date that is as far in the past
61
- # as this Duration represents.
62
- def ago(time = ::Time.current)
63
- sum(-1, time)
64
- end
65
- alias :until :ago
66
-
67
- def inspect #:nodoc:
68
- consolidated = parts.inject(::Hash.new(0)) { |h,part| h[part.first] += part.last; h }
69
- [:years, :months, :days, :minutes, :seconds].map do |length|
70
- n = consolidated[length]
71
- "#{n} #{n == 1 ? length.to_s.singularize : length.to_s}" if n.nonzero?
72
- end.compact.join(',')
73
- end
74
-
75
- protected
76
-
77
- def sum(sign, time = ::Time.current) #:nodoc:
78
- parts.inject(time) do |t,(type,number)|
79
- if t.acts_like?(:time) || t.acts_like?(:date)
80
- if type == :seconds
81
- t.since(sign * number)
82
- else
83
- t.advance(type => sign * number)
84
- end
85
- else
86
- raise ::ArgumentError, "expected a time or date, got #{time.inspect}"
87
- end
88
- end
89
- end
90
-
91
- private
92
-
93
- def method_missing(method, *args, &block) #:nodoc:
94
- value.send(method, *args)
95
- end
96
-
97
- end
98
- end
99
- end
@@ -1,32 +0,0 @@
1
- module Useful
2
- module RubyExtensionsFromRails
3
- module Fixnum
4
-
5
- # Turns a number into an ordinal string used to denote the position in an
6
- # ordered sequence such as 1st, 2nd, 3rd, 4th.
7
- #
8
- # Examples:
9
- # 1.ordinalize # => "1st"
10
- # 2.ordinalize # => "2nd"
11
- # 1003.ordinalize # => "1003rd"
12
- # 1004.ordinalize # => "1004th"
13
- def ordinalize
14
- if (11..13).include?(self.to_i % 100)
15
- "#{number}th"
16
- else
17
- case self.to_i % 10
18
- when 1; "#{number}st"
19
- when 2; "#{number}nd"
20
- when 3; "#{number}rd"
21
- else "#{number}th"
22
- end
23
- end
24
- end
25
-
26
- end
27
- end
28
- end
29
-
30
- class Fixnum
31
- include Useful::RubyExtensionsFromRails::Fixnum
32
- end
@@ -1,50 +0,0 @@
1
- module Useful
2
- module RubyExtensionsFromRails
3
- module Hash
4
-
5
- module ClassMethods; end
6
- def self.included(klass)
7
- klass.extend(ClassMethods) if klass.kind_of?(Class)
8
- end
9
-
10
- module ClassMethods
11
-
12
- # inspired by ActiveSupport::CoreExtensions::Hash::Keys (http://api.rubyonrails.org/)
13
- def stringify_keys(hash)
14
- hash.keys.each{ |key| hash[(key.to_s rescue key)] ||= hash.delete(key) }
15
- hash
16
- end
17
-
18
- # inspired by from ActiveSupport::CoreExtensions::Hash::Keys (http://api.rubyonrails.org/)
19
- def symbolize_keys(hash)
20
- hash.keys.each{ |key| hash[(key.to_sym rescue key)] ||= hash.delete(key) }
21
- hash
22
- end
23
-
24
- end
25
-
26
- # Return a new hash with all keys converted to strings.
27
- def stringify_keys
28
- self.class.stringify_keys(self.clone)
29
- end
30
- # Destructively convert all keys to strings.
31
- def stringify_keys!
32
- self.class.stringify_keys(self)
33
- end
34
-
35
- # Return a new hash with all keys converted to strings.
36
- def symbolize_keys
37
- self.class.symbolize_keys(self.clone)
38
- end
39
- # Destructively convert all keys to strings.
40
- def symbolize_keys!
41
- self.class.symbolize_keys(self)
42
- end
43
-
44
- end
45
- end
46
- end
47
-
48
- class Hash
49
- include Useful::RubyExtensionsFromRails::Hash
50
- end
@@ -1,60 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'duration')
2
-
3
- module Useful
4
- module RubyExtensionsFromRails
5
- module Numeric
6
-
7
- def seconds
8
- Useful::RubyExtensionsFromRails::Duration.new(self, [[:seconds, self]])
9
- end
10
- alias :second :seconds
11
-
12
- def minutes
13
- Useful::RubyExtensionsFromRails::Duration.new(self * 60, [[:seconds, self * 60]])
14
- end
15
- alias :minute :minutes
16
-
17
- def hours
18
- Useful::RubyExtensionsFromRails::Duration.new(self * 3600, [[:seconds, self * 3600]])
19
- end
20
- alias :hour :hours
21
-
22
- def days
23
- Useful::RubyExtensionsFromRails::Duration.new(self * 24.hours, [[:days, self]])
24
- end
25
- alias :day :days
26
-
27
- def weeks
28
- Useful::RubyExtensionsFromRails::Duration.new(self * 7.days, [[:days, self * 7]])
29
- end
30
- alias :week :weeks
31
-
32
- def fortnights
33
- Useful::RubyExtensionsFromRails::Duration.new(self * 2.weeks, [[:days, self * 14]])
34
- end
35
- alias :fortnight :fortnights
36
-
37
- # Reads best without arguments: 10.minutes.ago
38
- def ago(time = ::Time.now)
39
- time - self
40
- end
41
-
42
- # Reads best with argument: 10.minutes.until(time)
43
- alias :until :ago
44
-
45
- # Reads best with argument: 10.minutes.since(time)
46
- def since(time = ::Time.now)
47
- time + self
48
- end
49
-
50
- # Reads best without arguments: 10.minutes.from_now
51
- alias :from_now :since
52
-
53
- end
54
- end
55
- end
56
-
57
-
58
- class Numeric
59
- include Useful::RubyExtensionsFromRails::Numeric
60
- end
@@ -1,79 +0,0 @@
1
- module Useful
2
- module RubyExtensionsFromRails
3
- module Object
4
-
5
- def blank?
6
- self.nil? || (self.respond_to?(:empty?) ? self.empty? : false)
7
- end
8
-
9
- # Returns +value+ after yielding +value+ to the block. This simplifies the
10
- # process of constructing an object, performing work on the object, and then
11
- # returning the object from a method. It is a Ruby-ized realization of the K
12
- # combinator, courtesy of Mikael Brockman.
13
- #
14
- # ==== Examples
15
- #
16
- # # Without returning
17
- # def foo
18
- # values = []
19
- # values << "bar"
20
- # values << "baz"
21
- # return values
22
- # end
23
- #
24
- # foo # => ['bar', 'baz']
25
- #
26
- # # returning with a local variable
27
- # def foo
28
- # returning values = [] do
29
- # values << 'bar'
30
- # values << 'baz'
31
- # end
32
- # end
33
- #
34
- # foo # => ['bar', 'baz']
35
- #
36
- # # returning with a block argument
37
- # def foo
38
- # returning [] do |values|
39
- # values << 'bar'
40
- # values << 'baz'
41
- # end
42
- # end
43
- #
44
- # foo # => ['bar', 'baz']
45
- def returning(value)
46
- yield(value)
47
- value
48
- end
49
-
50
- # Yields <code>x</code> to the block, and then returns <code>x</code>.
51
- # The primary purpose of this method is to "tap into" a method chain,
52
- # in order to perform operations on intermediate results within the chain.
53
- #
54
- # (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
55
- # tap { |x| puts "array: #{x.inspect}" }.
56
- # select { |x| x%2 == 0 }.
57
- # tap { |x| puts "evens: #{x.inspect}" }.
58
- # map { |x| x*x }.
59
- # tap { |x| puts "squares: #{x.inspect}" }
60
- def tap
61
- yield self
62
- self
63
- end unless ::Object.respond_to?(:tap)
64
-
65
- # A duck-type assistant method. For example, Active Support extends Date
66
- # to define an acts_like_date? method, and extends Time to define
67
- # acts_like_time?. As a result, we can do "x.acts_like?(:time)" and
68
- # "x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that
69
- # we want to act like Time simply need to define an acts_like_time? method.
70
- def acts_like?(duck)
71
- respond_to? "acts_like_#{duck}?"
72
- end
73
- end
74
- end
75
- end
76
-
77
- class Object
78
- include Useful::RubyExtensionsFromRails::Object
79
- end