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,258 +1,268 @@
1
- require File.join(File.dirname(__FILE__), '..', 'ruby_extensions_from_rails', 'object') unless Object.new.respond_to?(:blank?)
2
- require File.join(File.dirname(__FILE__), '..', 'ruby_extensions_from_rails', 'hash') unless Hash.new.respond_to?(:symbolize_keys!)
3
- require File.join(File.dirname(__FILE__), '..', 'ruby_extensions_from_rails', 'string') unless String.new.respond_to?(:starts_with?)
4
- require File.join(File.dirname(__FILE__), 'hash') unless Hash.new.respond_to?(:only)
1
+ module Useful; end
2
+ module Useful::RubyExtensions; end
5
3
 
6
- module Useful
7
- module RubyExtensions
8
- module Numeric
4
+ require 'useful/ruby_extensions/object' unless ::Object.new.respond_to?(:blank?)
5
+ require 'useful/ruby_extensions/hash' unless ::Hash.new.respond_to?(:symbolize_keys!) && ::Hash.new.respond_to?(:only)
6
+ require 'useful/ruby_extensions/string' unless ::String.new.respond_to?(:starts_with?)
9
7
 
10
- LOCALES = {
11
- :en => {
12
- :currency => {:format => "%u%n", :unit=> '$'},
13
- :storage => {:format => "%n %u", :delimiter => ''},
14
- :format => {:delimiter => ',', :separator => '.'},
15
- :defaults => {:precision => 2}
16
- }
17
- }.freeze
18
- STORAGE_UNITS = ['Bytes', 'KB', 'MB', 'GB', 'TB'].freeze
8
+ module Useful::RubyExtensions::Numeric
19
9
 
20
- module ClassMethods; end
21
- def self.included(klass)
22
- klass.extend(ClassMethods) if klass.kind_of?(Class)
10
+ LOCALES = {
11
+ :en => {
12
+ :currency => {:format => "%u%n", :unit=> '$'},
13
+ :storage => {:format => "%n %u", :delimiter => ''},
14
+ :format => {:delimiter => ',', :separator => '.'},
15
+ :defaults => {:precision => 2}
16
+ }
17
+ }.freeze
18
+ STORAGE_UNITS = ['Bytes', 'KB', 'MB', 'GB', 'TB'].freeze
19
+
20
+ module ClassMethods
21
+
22
+ def pad_precision(num, opts = {})
23
+ opts[:precision] ||= 2
24
+ opts[:separator] ||= '.'
25
+ opts[:pad_number] ||= 0
26
+ num_s = num.to_s
27
+ num_s << opts[:separator] unless num_s.include?(opts[:separator])
28
+ ljust_count = num_s.split(opts[:separator])[0].length
29
+ ljust_count += (num_s.count(opts[:separator]) + opts[:precision].to_i) if opts[:precision] > 0
30
+ num_count = num.to_s.length
31
+ if ljust_count >= num_count
32
+ num_s.ljust(ljust_count, opts[:pad_number].to_s)
33
+ else
34
+ num_s[0..(ljust_count-1)]
23
35
  end
36
+ end
24
37
 
25
- module ClassMethods
38
+ end
39
+
40
+ module InstanceMethods
26
41
 
27
- def pad_precision(num, opts = {})
28
- opts[:precision] ||= 2
29
- opts[:separator] ||= '.'
30
- opts[:pad_number] ||= 0
31
- num.to_s.ljust(num.to_s.split(opts[:separator])[0].length + num.to_s.count(opts[:separator]) + opts[:precision].to_i, opts[:pad_number].to_s)
32
- end
42
+ # Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can
43
+ # customize the format in the +options+ hash.
44
+ # => taken and inspired from ActionView::Helpers::NumberHelper (http://api.rubyonrails.org/)
45
+ #
46
+ # ==== Options
47
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
48
+ # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
49
+ #
50
+ # ==== Examples
51
+ # 12345678.with_delimiter # => 12,345,678
52
+ # 12345678.05.with_delimiter # => 12,345,678.05
53
+ # 12345678.with_delimiter(:delimiter => ".") # => 12.345.678
54
+ # 12345678.with_delimiter(:seperator => ",") # => 12,345,678
55
+ # 98765432.98.with_delimiter(:delimiter => " ", :separator => ",") # => 98 765 432,98
56
+ def with_delimiter(opts = {})
57
+ number = self.to_s.strip
58
+ opts.symbolize_keys!
59
+ opts[:locale] ||= :en
60
+ locale = LOCALES[opts.delete(:locale)]
61
+ opts = locale[:format].merge(opts) unless locale.nil?
62
+ opts[:delimiter] ||= ','
63
+ opts[:separator] ||= '.'
33
64
 
65
+ begin
66
+ parts = number.to_s.split('.')
67
+ parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{opts[:delimiter]}")
68
+ parts.join(opts[:separator])
69
+ rescue
70
+ number
34
71
  end
72
+ end
35
73
 
36
- # Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can
37
- # customize the format in the +options+ hash.
38
- # => taken and inspired from ActionView::Helpers::NumberHelper (http://api.rubyonrails.org/)
39
- #
40
- # ==== Options
41
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
42
- # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
43
- #
44
- # ==== Examples
45
- # 12345678.with_delimiter # => 12,345,678
46
- # 12345678.05.with_delimiter # => 12,345,678.05
47
- # 12345678.with_delimiter(:delimiter => ".") # => 12.345.678
48
- # 12345678.with_delimiter(:seperator => ",") # => 12,345,678
49
- # 98765432.98.with_delimiter(:delimiter => " ", :separator => ",")
50
- # # => 98 765 432,98
51
- def with_delimiter(opts = {})
52
- number = self.to_s.strip
53
- opts.symbolize_keys!
54
- opts[:locale] ||= :en
55
- locale = LOCALES[opts.delete(:locale)]
56
- opts = locale[:format].merge(opts) unless locale.nil?
57
- opts[:delimiter] ||= ','
58
- opts[:separator] ||= '.'
74
+ # Converts a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
75
+ # => taken and inspired from ActionView::Helpers::NumberHelper (http://api.rubyonrails.org/)
76
+ def to_precision(precision = 2)
77
+ rounded_number = (Float(self) * (10 ** precision)).round
78
+ rounded_number = rounded_number.to_f if precision > 0
79
+ (rounded_number / 10 ** precision) rescue self
80
+ end
59
81
 
60
- begin
61
- parts = number.to_s.split('.')
62
- parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{opts[:delimiter]}")
63
- parts.join(opts[:separator])
64
- rescue
65
- number
66
- end
67
- end
82
+ # Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
83
+ # You can customize the format in the +options+ hash.
84
+ # => taken and inspired from ActionView::Helpers::NumberHelper (http://api.rubyonrails.org/)
85
+ #
86
+ # ==== Options
87
+ # * <tt>:precision</tt> - Sets the level of precision (defaults to 3).
88
+ # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
89
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
90
+ #
91
+ # ==== Examples (:locale => :en)
92
+ # 1111.2345.with_precision # => 1111.23
93
+ # 1111.2345.with_precision(:delimiter => ',') # => 1,111.23
94
+ # 111.2345.with_precision(:precision => 3) # => 111.235
95
+ # 13.with_precision(:precision => 5) # => 13.00000
96
+ # 389.32314.with_precision(:precision => 0) # => 389
97
+ # 1111.2345.with_precision(:precision => 3, :separator => ',', :delimiter => '.') # => 1.111,235
98
+ def with_precision(opts = {})
99
+ opts.symbolize_keys!
100
+ opts[:locale] ||= :en
101
+ opts[:delimiter] ||= '' # don't use a delimiter by default
102
+ locale = LOCALES[opts.delete(:locale)]
103
+ opts = locale[:defaults].merge(locale[:format]).merge(opts) unless locale.nil?
104
+ opts[:precision] ||= 2
68
105
 
69
- # Converts a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
70
- # => taken and inspired from ActionView::Helpers::NumberHelper (http://api.rubyonrails.org/)
71
- def to_precision(precision = 2)
72
- rounded_number = (Float(self) * (10 ** precision)).round
73
- rounded_number = rounded_number.to_f if precision > 0
74
- (rounded_number / 10 ** precision) rescue self
75
- end
76
-
77
- # Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
78
- # You can customize the format in the +options+ hash.
79
- # => taken and inspired from ActionView::Helpers::NumberHelper (http://api.rubyonrails.org/)
80
- #
81
- # ==== Options
82
- # * <tt>:precision</tt> - Sets the level of precision (defaults to 3).
83
- # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
84
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
85
- #
86
- # ==== Examples (:locale => :en)
87
- # 111.2345.with_precision # => 111.235
88
- # 111.2345.with_precision(:precision => 2) # => 111.23
89
- # 13.with_precision(:precision => 5) # => 13.00000
90
- # 389.32314.with_precision(:precision => 0) # => 389
91
- # 1111.2345.with_precision(:precision => 2, :separator => ',', :delimiter => '.')
92
- # # => 1,111.23
93
- def with_precision(opts = {})
94
- opts.symbolize_keys!
95
- opts[:locale] ||= :en
96
- opts[:delimiter] ||= ''
97
- locale = LOCALES[opts.delete(:locale)]
98
- opts = locale[:defaults].merge(locale[:format]).merge(opts) unless locale.nil?
99
- opts[:precision] ||= 2
100
-
101
- self.class.pad_precision(self.to_precision(opts[:precision]).with_delimiter(opts.only(:separator, :delimiter)), opts) rescue self
102
- end
106
+ self.class.pad_precision(self.to_precision(opts[:precision]).with_delimiter(opts.only(:separator, :delimiter)), opts) rescue self
107
+ end
103
108
 
104
- # Formats a +number+ as a percentage string (e.g., 65%). You can customize the
105
- # format in the +options+ hash.
106
- #
107
- # ==== Options
108
- # * <tt>:precision</tt> - Sets the level of precision (defaults to 2).
109
- # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
110
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
111
- #
112
- # ==== Examples
113
- # 100.to_percentage # => 100.00%
114
- # 100.to_percentage(:precision => 0) # => 100%
115
- # 1000.to_percentage(:delimiter => '.', :separator => ',') # => 1.000,00%
116
- # 302.24398923423.to_percentage(:precision => 5) # => 302.24399%
117
- def to_percentage(opts = {})
118
- opts.symbolize_keys!
119
- opts[:locale] ||= :en
120
- locale = LOCALES[opts.delete(:locale)]
121
- opts = locale[:defaults].merge(locale[:format]).merge(opts) unless locale.nil?
109
+ # Formats a +number+ as a percentage string (e.g., 65%). You can customize the
110
+ # format in the +options+ hash.
111
+ #
112
+ # ==== Options
113
+ # * <tt>:precision</tt> - Sets the level of precision (defaults to 2).
114
+ # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
115
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
116
+ #
117
+ # ==== Examples
118
+ # 100.to_percentage # => 100.00%
119
+ # 100.to_percentage(:precision => 0) # => 100%
120
+ # 1000.to_percentage(:delimiter => '.', :separator => ',') # => 1.000,00%
121
+ # 302.24398923423.to_percentage(:precision => 5) # => 302.24399%
122
+ def to_percentage(opts = {})
123
+ opts.symbolize_keys!
124
+ opts[:locale] ||= :en
125
+ locale = LOCALES[opts.delete(:locale)]
126
+ opts = locale[:defaults].merge(locale[:format]).merge(opts) unless locale.nil?
122
127
 
123
- "#{self.with_precision(opts.only(:precision, :separator, :delimiter))}%" rescue self
124
- end
128
+ "#{self.with_precision(opts.only(:precision, :separator, :delimiter))}%" rescue self
129
+ end
125
130
 
126
- # Formats a +number+ into a currency string (e.g., $13.65). You can customize the format
127
- # in the +options+ hash.
128
- # => taken and inspired from ActionView::Helpers::NumberHelper (http://api.rubyonrails.org/)
129
- #
130
- # ==== Options
131
- # * <tt>:precision</tt> - Sets the level of precision (defaults to 2).
132
- # * <tt>:unit</tt> - Sets the denomination of the currency (defaults to "$").
133
- # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
134
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
135
- # * <tt>:format</tt> - Sets the format of the output string (defaults to "%u%n"). The field types are:
136
- # %u The currency unit
137
- # %n The number
138
- #
139
- # ==== Examples (:locale => :en)
140
- # 1234567890.50.to_currency # => $1,234,567,890.50
141
- # 1234567890.506.to_currency # => $1,234,567,890.51
142
- # 1234567890.506.to_currency(:precision => 3) # => $1,234,567,890.506
143
- #
144
- # 1234567890.50.to_currency(:unit => "&pound;", :separator => ",", :delimiter => "")
145
- # # => &pound;1234567890,50
146
- # 1234567890.50.to_currency(:unit => "&pound;", :separator => ",", :delimiter => "", :format => "%n %u")
147
- # # => 1234567890,50 &pound;
148
- def to_currency(opts = {})
149
- return opts[:zero_display] if opts[:zero_display] && self.to_f == 0.to_f
150
- opts.symbolize_keys!
151
- opts[:locale] ||= :en
152
- locale = LOCALES[opts.delete(:locale)]
153
- opts = locale[:defaults].merge(locale[:format]).merge(locale[:currency]).merge(opts) unless locale.nil?
131
+ # Formats a +number+ into a currency string (e.g., $13.65). You can customize the format
132
+ # in the +options+ hash.
133
+ # => taken and inspired from ActionView::Helpers::NumberHelper (http://api.rubyonrails.org/)
134
+ #
135
+ # ==== Options
136
+ # * <tt>:precision</tt> - Sets the level of precision (defaults to 2).
137
+ # * <tt>:unit</tt> - Sets the denomination of the currency (defaults to "$").
138
+ # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
139
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to ",").
140
+ # * <tt>:format</tt> - Sets the format of the output string (defaults to "%u%n"). The field types are:
141
+ # %u The currency unit
142
+ # %n The number
143
+ #
144
+ # ==== Examples (:locale => :en)
145
+ # 1234567890.50.to_currency # => $1,234,567,890.50
146
+ # 1234567890.506.to_currency # => $1,234,567,890.51
147
+ # 1234567890.506.to_currency(:precision => 3) # => $1,234,567,890.506
148
+ #
149
+ # 1234567890.50.to_currency(:unit => "&pound;", :separator => ",", :delimiter => "")
150
+ # # => &pound;1234567890,50
151
+ # 1234567890.50.to_currency(:unit => "&pound;", :separator => ",", :delimiter => "", :format => "%n %u")
152
+ # # => 1234567890,50 &pound;
153
+ def to_currency(opts = {})
154
+ return opts[:zero_display] if opts[:zero_display] && self.to_f == 0.to_f
155
+ opts.symbolize_keys!
156
+ opts[:locale] ||= :en
157
+ locale = LOCALES[opts.delete(:locale)]
158
+ opts = locale[:defaults].merge(locale[:format]).merge(locale[:currency]).merge(opts) unless locale.nil?
154
159
 
155
- opts[:format].gsub(/%n/, self.with_precision(opts.only(:precision, :delimiter, :separator)).to_s).gsub(/%u/, opts[:unit].to_s) #rescue self
156
- end
160
+ opts[:format].gsub(/%n/, self.with_precision(opts.only(:precision, :delimiter, :separator)).to_s).gsub(/%u/, opts[:unit].to_s) #rescue self
161
+ end
157
162
 
158
- # Formats the bytes in +size+ into a more understandable representation
159
- # (e.g., giving it 1500 yields 1.5 KB). This method is useful for
160
- # reporting file sizes to users. This method returns nil if
161
- # +size+ cannot be converted into a number. You can customize the
162
- # format in the +options+ hash.
163
- #
164
- # ==== Options
165
- # * <tt>:precision</tt> - Sets the level of precision (defaults to 2).
166
- # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
167
- # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
168
- #
169
- # ==== Examples
170
- # 123.to_storage_size # => 123 Bytes
171
- # 1234.to_storage_size # => 1.2 KB
172
- # 12345.to_storage_size # => 12.1 KB
173
- # 1234567.to_storage_size # => 1.2 MB
174
- # 1234567890.to_storage_size # => 1.1 GB
175
- # 1234567890123.to_storage_size # => 1.1 TB
176
- # 1234567.to_storage_size(:precision => 2) # => 1.18 MB
177
- # 483989.to_storage_size(:precision => 0) # => 473 KB
178
- # 1234567.to_storage_size(:precision => 2, :separator => ',') # => 1,18 MB
179
- def to_storage_size(opts = {})
180
- return nil if self.nil?
181
- opts.symbolize_keys!
182
- opt_precision = opts[:precision]
183
- opts[:locale] ||= :en
184
- locale = LOCALES[opts.delete(:locale)]
185
- opts = locale[:defaults].merge(locale[:format]).merge(locale[:storage]).merge(opts) unless locale.nil?
186
- opts[:format] ||= "%n %u"
187
- opts[:precision] = 0 unless opt_precision
163
+ # Formats the bytes in +size+ into a more understandable representation
164
+ # (e.g., giving it 1500 yields 1.5 KB). This method is useful for
165
+ # reporting file sizes to users. This method returns nil if
166
+ # +size+ cannot be converted into a number. You can customize the
167
+ # format in the +options+ hash.
168
+ #
169
+ # ==== Options
170
+ # * <tt>:precision</tt> - Sets the level of precision (defaults to 2).
171
+ # * <tt>:separator</tt> - Sets the separator between the units (defaults to ".").
172
+ # * <tt>:delimiter</tt> - Sets the thousands delimiter (defaults to "").
173
+ #
174
+ # ==== Examples
175
+ # 123.to_storage_size # => 123 Bytes
176
+ # 1234.to_storage_size # => 1.2 KB
177
+ # 12345.to_storage_size # => 12.1 KB
178
+ # 1234567.to_storage_size # => 1.2 MB
179
+ # 1234567890.to_storage_size # => 1.1 GB
180
+ # 1234567890123.to_storage_size # => 1.1 TB
181
+ # 1234567.to_storage_size(:precision => 2) # => 1.18 MB
182
+ # 483989.to_storage_size(:precision => 0) # => 473 KB
183
+ # 1234567.to_storage_size(:precision => 2, :separator => ',') # => 1,18 MB
184
+ def to_storage_size(opts = {})
185
+ return nil if self.nil?
186
+ opts.symbolize_keys!
187
+ opt_precision = opts[:precision]
188
+ opts[:locale] ||= :en
189
+ locale = LOCALES[opts.delete(:locale)]
190
+ opts = locale[:defaults].merge(locale[:format]).merge(locale[:storage]).merge(opts) unless locale.nil?
191
+ opts[:format] ||= "%n %u"
192
+ opts[:precision] = 0 unless opt_precision
188
193
 
189
- value = self.to_f
190
- unit = ''
191
- STORAGE_UNITS.each do |storage_unit|
192
- unit = storage_unit.to_s
193
- return opts[:format].gsub(/%n/, value.with_precision(opts.only(:precision, :delimiter, :separator)).to_s).gsub(/%u/, unit.to_s) if value < 1024 || storage_unit == STORAGE_UNITS.last
194
- opts[:precision] = 2 unless opt_precision
195
- value /= 1024.0
196
- end
194
+ value = self.to_f
195
+ unit = ''
196
+ STORAGE_UNITS.each do |storage_unit|
197
+ unit = storage_unit.to_s
198
+ return opts[:format].gsub(/%n/, value.with_precision(opts.only(:precision, :delimiter, :separator)).to_s).gsub(/%u/, unit.to_s) if value < 1024 || storage_unit == STORAGE_UNITS.last
199
+ opts[:precision] = 1 unless opt_precision
200
+ value /= 1024.0
197
201
  end
202
+ end
198
203
 
199
- # Returns the string representation of the number's +parity+.
200
- #
201
- # ==== Examples
202
- # 1.to_parity # => "odd"
203
- # 2.to_parity # => "even"
204
- def to_parity
205
- self.to_i.even? ? 'even' : 'odd'
206
- end
204
+ # Returns the string representation of the number's +parity+.
205
+ #
206
+ # ==== Examples
207
+ # 1.to_parity # => "odd"
208
+ # 2.to_parity # => "even"
209
+ def to_parity
210
+ self.to_i.modulo(2).zero? ? 'even' : 'odd'
211
+ end
207
212
 
208
- # Provides methods for converting numbers into formatted strings.
209
- # Methods are provided for phone numbers, currency, percentage,
210
- # precision, positional notation, and file size.
211
- # => taken and inspired from ActionView::Helpers::NumberHelper (http://api.rubyonrails.org/)
213
+ # Provides methods for converting numbers into formatted strings.
214
+ # Methods are provided for phone numbers, currency, percentage,
215
+ # precision, positional notation, and file size.
216
+ # => taken and inspired from ActionView::Helpers::NumberHelper (http://api.rubyonrails.org/)
212
217
 
213
- # Formats a +number+ into a US phone number (e.g., (555) 123-9876). You can customize the format
214
- # in the +options+ hash.
215
- #
216
- # ==== Options
217
- # * <tt>:area_code</tt> - Adds parentheses around the area code.
218
- # * <tt>:delimiter</tt> - Specifies the delimiter to use (defaults to "-").
219
- # * <tt>:extension</tt> - Specifies an extension to add to the end of the
220
- # generated number.
221
- # * <tt>:country_code</tt> - Sets the country code for the phone number.
222
- #
223
- # ==== Examples
224
- # 5551234.to_phone # => 555-1234
225
- # 1235551234.to_phone # => 123-555-1234
226
- # 1235551234.to_phone(:area_code => true) # => (123) 555-1234
227
- # 1235551234.to_phone(:delimiter => " ") # => 123 555 1234
228
- # 1235551234.to_phone(:area_code => true, :extension => 555) # => (123) 555-1234 x 555
229
- # 1235551234.to_phone(:country_code => 1) # => +1-123-555-1234
230
- # 1235551234.to_phone({ :country_code => 1,
231
- # :extension => 1343, :delimiter => "." }) # => +1.123.555.1234 x 1343
232
- def to_phone(opts={})
233
- number = self.to_s.strip
234
- opts.symbolize_keys!
235
- opts[:delimiter] ||= '-'
236
- opts[:extension] = opts[:extension].to_s.strip unless opts[:extension].nil?
218
+ # Formats a +number+ into a US phone number (e.g., (555) 123-9876). You can customize the format
219
+ # in the +options+ hash.
220
+ #
221
+ # ==== Options
222
+ # * <tt>:area_code</tt> - Adds parentheses around the area code.
223
+ # * <tt>:delimiter</tt> - Specifies the delimiter to use (defaults to "-").
224
+ # * <tt>:extension</tt> - Specifies an extension to add to the end of the
225
+ # generated number.
226
+ # * <tt>:country_code</tt> - Sets the country code for the phone number.
227
+ #
228
+ # ==== Examples
229
+ # 5551234.to_phone # => 555-1234
230
+ # 1235551234.to_phone # => 123-555-1234
231
+ # 1235551234.to_phone(:area_code => true) # => (123) 555-1234
232
+ # 1235551234.to_phone(:delimiter => " ") # => 123 555 1234
233
+ # 1235551234.to_phone(:area_code => true, :extension => 555) # => (123) 555-1234 x 555
234
+ # 1235551234.to_phone(:country_code => 1) # => +1-123-555-1234
235
+ # 1235551234.to_phone({ :country_code => 1,
236
+ # :extension => 1343, :delimiter => "." }) # => +1.123.555.1234 x 1343
237
+ def to_phone(opts={})
238
+ number = self.to_s.strip
239
+ opts.symbolize_keys!
240
+ opts[:delimiter] ||= '-'
241
+ opts[:extension] = opts[:extension].to_s.strip unless opts[:extension].nil?
237
242
 
238
- begin
239
- str = ""
240
- str << "+#{opts[:country_code]}#{opts[:delimiter]}" unless opts[:country_code].blank?
241
- str << if opts[:area_code]
242
- number.gsub!(/([0-9]{1,3})([0-9]{3})([0-9]{4}$)/,"(\\1) \\2#{opts[:delimiter]}\\3")
243
- else
244
- number.gsub!(/([0-9]{0,3})([0-9]{3})([0-9]{4})$/,"\\1#{opts[:delimiter]}\\2#{opts[:delimiter]}\\3")
245
- number.starts_with?('-') ? number.slice!(1..-1) : number
246
- end
247
- str << " x #{opts[:extension]}" unless opts[:extension].blank?
248
- str
249
- rescue
250
- number
243
+ begin
244
+ str = ""
245
+ str << "+#{opts[:country_code]}#{opts[:delimiter]}" unless opts[:country_code].blank?
246
+ str << if opts[:area_code]
247
+ number.gsub!(/([0-9]{1,3})([0-9]{3})([0-9]{4}$)/,"(\\1) \\2#{opts[:delimiter]}\\3")
248
+ else
249
+ number.gsub!(/([0-9]{0,3})([0-9]{3})([0-9]{4})$/,"\\1#{opts[:delimiter]}\\2#{opts[:delimiter]}\\3")
250
+ number.starts_with?('-') ? number.slice!(1..-1) : number
251
251
  end
252
+ str << " x #{opts[:extension]}" unless opts[:extension].blank?
253
+ str
254
+ rescue
255
+ number
252
256
  end
253
-
254
257
  end
258
+
255
259
  end
260
+
261
+ def self.included(receiver)
262
+ receiver.extend ClassMethods
263
+ receiver.send :include, InstanceMethods
264
+ end
265
+
256
266
  end
257
267
 
258
268
  class Numeric
@@ -1,17 +1,132 @@
1
- module Useful
2
- module RubyExtensions
3
- module Object
4
-
5
- def false?
6
- self == false
7
- end
8
-
9
- def true?
10
- self == true
1
+ require 'useful/ruby_extensions/nil_class'
2
+
3
+ module Useful; end
4
+ module Useful::RubyExtensions; end
5
+
6
+ module Useful::RubyExtensions::Object
7
+
8
+ def false?
9
+ self == false
10
+ end
11
+ alias_method :is_false?, :false?
12
+
13
+ def true?
14
+ self == true
15
+ end
16
+ alias_method :is_true?, :true?
17
+
18
+ def capture_std_output
19
+ out = ::StringIO.new
20
+ err = ::StringIO.new
21
+ $stdout = out
22
+ $stderr = err
23
+ yield
24
+ return out, err
25
+ ensure
26
+ $stdout = STDOUT
27
+ $stderr = STDERR
28
+ end
29
+
30
+ module FromActivesupport
31
+
32
+ def blank?
33
+ self.nil? || self.false? || (self.respond_to?(:empty?) ? self.empty? : false)
34
+ end unless ::Object.new.respond_to?('blank?')
35
+
36
+ # Returns +value+ after yielding +value+ to the block. This simplifies the
37
+ # process of constructing an object, performing work on the object, and then
38
+ # returning the object from a method. It is a Ruby-ized realization of the K
39
+ # combinator, courtesy of Mikael Brockman.
40
+ #
41
+ # ==== Examples
42
+ #
43
+ # # Without returning
44
+ # def foo
45
+ # values = []
46
+ # values << "bar"
47
+ # values << "baz"
48
+ # return values
49
+ # end
50
+ #
51
+ # foo # => ['bar', 'baz']
52
+ #
53
+ # # returning with a local variable
54
+ # def foo
55
+ # returning values = [] do
56
+ # values << 'bar'
57
+ # values << 'baz'
58
+ # end
59
+ # end
60
+ #
61
+ # foo # => ['bar', 'baz']
62
+ #
63
+ # # returning with a block argument
64
+ # def foo
65
+ # returning [] do |values|
66
+ # values << 'bar'
67
+ # values << 'baz'
68
+ # end
69
+ # end
70
+ #
71
+ # foo # => ['bar', 'baz']
72
+ def returning(value)
73
+ yield(value)
74
+ value
75
+ end unless ::Object.new.respond_to?('returning')
76
+
77
+ # Yields <code>x</code> to the block, and then returns <code>x</code>.
78
+ # The primary purpose of this method is to "tap into" a method chain,
79
+ # in order to perform operations on intermediate results within the chain.
80
+ #
81
+ # (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
82
+ # tap { |x| puts "array: #{x.inspect}" }.
83
+ # select { |x| x%2 == 0 }.
84
+ # tap { |x| puts "evens: #{x.inspect}" }.
85
+ # map { |x| x*x }.
86
+ # tap { |x| puts "squares: #{x.inspect}" }
87
+ def tap
88
+ yield self
89
+ self
90
+ end unless ::Object.new.respond_to?('tap')
91
+
92
+ # Invokes the method identified by the symbol +method+, passing it any arguments
93
+ # and/or the block specified, just like the regular Ruby <tt>Object#send</tt> does.
94
+ #
95
+ # *Unlike* that method however, a +NoMethodError+ exception will *not* be raised
96
+ # and +nil+ will be returned instead, if the receiving object is a +nil+ object or NilClass.
97
+ #
98
+ # ==== Examples
99
+ #
100
+ # Without try
101
+ # @person && @person.name
102
+ # or
103
+ # @person ? @person.name : nil
104
+ #
105
+ # With try
106
+ # @person.try(:name)
107
+ #
108
+ # +try+ also accepts arguments and/or a block, for the method it is trying
109
+ # Person.try(:find, 1)
110
+ # @people.try(:collect) {|p| p.name}
111
+ #--
112
+ # This method definition below is for rdoc purposes only. The alias_method call
113
+ # below overrides it as an optimization since +try+ behaves like +Object#send+,
114
+ # unless called on +NilClass+.
115
+ # => see try method definition on the NilClass extensions for details.
116
+ unless ::Object.new.respond_to?('try')
117
+ def try(method, *args, &block)
118
+ send(method, *args, &block)
11
119
  end
12
-
120
+ remove_method :try
121
+ alias_method :try, :__send__
13
122
  end
123
+
124
+ end
125
+
126
+ def self.included(receiver)
127
+ receiver.send :include, FromActivesupport
14
128
  end
129
+
15
130
  end
16
131
 
17
132
  class Object