kelredd-useful 0.1.25 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- 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,258 +1,268 @@
|
|
1
|
-
|
2
|
-
|
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
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
38
|
+
end
|
39
|
+
|
40
|
+
module InstanceMethods
|
26
41
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
70
|
-
|
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
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
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
|
-
|
124
|
-
|
128
|
+
"#{self.with_precision(opts.only(:precision, :separator, :delimiter))}%" rescue self
|
129
|
+
end
|
125
130
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
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 => "£", :separator => ",", :delimiter => "")
|
150
|
+
# # => £1234567890,50
|
151
|
+
# 1234567890.50.to_currency(:unit => "£", :separator => ",", :delimiter => "", :format => "%n %u")
|
152
|
+
# # => 1234567890,50 £
|
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
|
-
|
156
|
-
|
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
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
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
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
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
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
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
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
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
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
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
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|