activesupport 3.2.22.5 → 4.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +325 -136
- data/MIT-LICENSE +1 -1
- data/README.rdoc +4 -2
- data/lib/active_support.rb +8 -21
- data/lib/active_support/backtrace_cleaner.rb +33 -25
- data/lib/active_support/basic_object.rb +7 -17
- data/lib/active_support/benchmarkable.rb +19 -15
- data/lib/active_support/buffered_logger.rb +9 -113
- data/lib/active_support/cache.rb +203 -171
- data/lib/active_support/cache/file_store.rb +12 -12
- data/lib/active_support/cache/mem_cache_store.rb +24 -30
- data/lib/active_support/cache/memory_store.rb +2 -0
- data/lib/active_support/callbacks.rb +195 -247
- data/lib/active_support/concern.rb +16 -23
- data/lib/active_support/concurrency/latch.rb +27 -0
- data/lib/active_support/configurable.rb +69 -12
- data/lib/active_support/core_ext.rb +1 -0
- data/lib/active_support/core_ext/array.rb +0 -1
- data/lib/active_support/core_ext/array/access.rb +17 -9
- data/lib/active_support/core_ext/array/conversions.rb +113 -55
- data/lib/active_support/core_ext/array/extract_options.rb +2 -2
- data/lib/active_support/core_ext/array/grouping.rb +21 -22
- data/lib/active_support/core_ext/array/uniq_by.rb +12 -9
- data/lib/active_support/core_ext/array/wrap.rb +11 -14
- data/lib/active_support/core_ext/big_decimal/conversions.rb +7 -24
- data/lib/active_support/core_ext/class/attribute.rb +12 -8
- data/lib/active_support/core_ext/class/attribute_accessors.rb +14 -12
- data/lib/active_support/core_ext/class/delegating_attributes.rb +15 -19
- data/lib/active_support/core_ext/class/subclasses.rb +11 -5
- data/lib/active_support/core_ext/date.rb +6 -0
- data/lib/active_support/core_ext/date/calculations.rb +34 -188
- data/lib/active_support/core_ext/date/conversions.rb +16 -38
- data/lib/active_support/core_ext/date/infinite_comparable.rb +5 -0
- data/lib/active_support/core_ext/date/zones.rb +25 -2
- data/lib/active_support/core_ext/date_and_time/calculations.rb +232 -0
- data/lib/active_support/core_ext/date_time.rb +5 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +0 -1
- data/lib/active_support/core_ext/date_time/calculations.rb +73 -65
- data/lib/active_support/core_ext/date_time/conversions.rb +21 -33
- data/lib/active_support/core_ext/date_time/infinite_comparable.rb +5 -0
- data/lib/active_support/core_ext/date_time/zones.rb +11 -8
- data/lib/active_support/core_ext/enumerable.rb +26 -73
- data/lib/active_support/core_ext/file.rb +0 -1
- data/lib/active_support/core_ext/file/atomic.rb +27 -11
- data/lib/active_support/core_ext/hash.rb +0 -1
- data/lib/active_support/core_ext/hash/conversions.rb +145 -79
- data/lib/active_support/core_ext/hash/deep_merge.rb +14 -8
- data/lib/active_support/core_ext/hash/diff.rb +5 -4
- data/lib/active_support/core_ext/hash/except.rb +1 -9
- data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -5
- data/lib/active_support/core_ext/hash/keys.rb +108 -24
- data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
- data/lib/active_support/core_ext/hash/slice.rb +12 -12
- data/lib/active_support/core_ext/infinite_comparable.rb +35 -0
- data/lib/active_support/core_ext/integer/inflections.rb +13 -1
- data/lib/active_support/core_ext/integer/time.rb +17 -12
- data/lib/active_support/core_ext/kernel/debugger.rb +2 -2
- data/lib/active_support/core_ext/kernel/reporting.rb +36 -22
- data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
- data/lib/active_support/core_ext/load_error.rb +7 -5
- data/lib/active_support/core_ext/logger.rb +7 -23
- data/lib/active_support/core_ext/marshal.rb +19 -0
- data/lib/active_support/core_ext/module.rb +1 -3
- data/lib/active_support/core_ext/module/aliasing.rb +8 -9
- data/lib/active_support/core_ext/module/anonymous.rb +2 -7
- data/lib/active_support/core_ext/module/attr_internal.rb +0 -1
- data/lib/active_support/core_ext/module/attribute_accessors.rb +12 -10
- data/lib/active_support/core_ext/module/delegation.rb +57 -40
- data/lib/active_support/core_ext/module/deprecation.rb +19 -3
- data/lib/active_support/core_ext/module/introspection.rb +17 -27
- data/lib/active_support/core_ext/module/qualified_const.rb +8 -20
- data/lib/active_support/core_ext/module/remove_method.rb +1 -5
- data/lib/active_support/core_ext/numeric.rb +2 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +135 -0
- data/lib/active_support/core_ext/numeric/infinite_comparable.rb +9 -0
- data/lib/active_support/core_ext/numeric/time.rb +6 -6
- data/lib/active_support/core_ext/object.rb +1 -0
- data/lib/active_support/core_ext/object/acts_like.rb +4 -4
- data/lib/active_support/core_ext/object/blank.rb +7 -23
- data/lib/active_support/core_ext/object/deep_dup.rb +46 -0
- data/lib/active_support/core_ext/object/duplicable.rb +1 -30
- data/lib/active_support/core_ext/object/inclusion.rb +6 -6
- data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
- data/lib/active_support/core_ext/object/to_json.rb +8 -0
- data/lib/active_support/core_ext/object/to_param.rb +5 -2
- data/lib/active_support/core_ext/object/try.rb +46 -25
- data/lib/active_support/core_ext/object/with_options.rb +7 -8
- data/lib/active_support/core_ext/proc.rb +3 -0
- data/lib/active_support/core_ext/range.rb +0 -2
- data/lib/active_support/core_ext/range/conversions.rb +0 -2
- data/lib/active_support/core_ext/range/include_range.rb +1 -1
- data/lib/active_support/core_ext/range/overlaps.rb +1 -1
- data/lib/active_support/core_ext/string.rb +2 -2
- data/lib/active_support/core_ext/string/access.rb +95 -90
- data/lib/active_support/core_ext/string/conversions.rb +29 -38
- data/lib/active_support/core_ext/string/encoding.rb +6 -9
- data/lib/active_support/core_ext/string/filters.rb +24 -18
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +70 -60
- data/lib/active_support/core_ext/string/inquiry.rb +2 -2
- data/lib/active_support/core_ext/string/multibyte.rb +41 -64
- data/lib/active_support/core_ext/string/output_safety.rb +59 -51
- data/lib/active_support/core_ext/string/zones.rb +13 -0
- data/lib/active_support/core_ext/struct.rb +6 -0
- data/lib/active_support/core_ext/thread.rb +74 -0
- data/lib/active_support/core_ext/time.rb +6 -0
- data/lib/active_support/core_ext/time/calculations.rb +105 -193
- data/lib/active_support/core_ext/time/conversions.rb +27 -51
- data/lib/active_support/core_ext/time/infinite_comparable.rb +5 -0
- data/lib/active_support/core_ext/time/marshal.rb +0 -27
- data/lib/active_support/core_ext/time/zones.rb +27 -17
- data/lib/active_support/core_ext/uri.rb +13 -17
- data/lib/active_support/dependencies.rb +160 -141
- data/lib/active_support/dependencies/autoload.rb +47 -20
- data/lib/active_support/deprecation.rb +39 -14
- data/lib/active_support/deprecation/behaviors.rb +44 -30
- data/lib/active_support/deprecation/instance_delegator.rb +24 -0
- data/lib/active_support/deprecation/method_wrappers.rb +33 -18
- data/lib/active_support/deprecation/proxy_wrappers.rb +58 -13
- data/lib/active_support/deprecation/reporting.rb +40 -11
- data/lib/active_support/descendants_tracker.rb +34 -19
- data/lib/active_support/duration.rb +6 -8
- data/lib/active_support/file_update_checker.rb +63 -47
- data/lib/active_support/gzip.rb +11 -5
- data/lib/active_support/hash_with_indifferent_access.rb +112 -37
- data/lib/active_support/i18n.rb +4 -0
- data/lib/active_support/i18n_railtie.rb +5 -22
- data/lib/active_support/inflections.rb +14 -12
- data/lib/active_support/inflector/inflections.rb +108 -71
- data/lib/active_support/inflector/methods.rb +181 -160
- data/lib/active_support/inflector/transliterate.rb +16 -17
- data/lib/active_support/json/decoding.rb +18 -17
- data/lib/active_support/json/encoding.rb +93 -39
- data/lib/active_support/json/variable.rb +10 -1
- data/lib/active_support/key_generator.rb +75 -0
- data/lib/active_support/lazy_load_hooks.rb +21 -19
- data/lib/active_support/locale/en.yml +100 -3
- data/lib/active_support/log_subscriber.rb +56 -36
- data/lib/active_support/log_subscriber/test_helper.rb +18 -15
- data/lib/active_support/logger.rb +57 -0
- data/lib/active_support/logger_silence.rb +24 -0
- data/lib/active_support/message_encryptor.rb +32 -29
- data/lib/active_support/message_verifier.rb +8 -14
- data/lib/active_support/multibyte.rb +5 -28
- data/lib/active_support/multibyte/chars.rb +80 -333
- data/lib/active_support/multibyte/unicode.rb +74 -64
- data/lib/active_support/notifications.rb +57 -25
- data/lib/active_support/notifications/fanout.rb +105 -18
- data/lib/active_support/notifications/instrumenter.rb +32 -13
- data/lib/active_support/number_helper.rb +636 -0
- data/lib/active_support/ordered_hash.rb +8 -190
- data/lib/active_support/ordered_options.rb +21 -23
- data/lib/active_support/proxy_object.rb +13 -0
- data/lib/active_support/rails.rb +27 -0
- data/lib/active_support/railtie.rb +12 -32
- data/lib/active_support/rescuable.rb +9 -4
- data/lib/active_support/string_inquirer.rb +13 -8
- data/lib/active_support/tagged_logging.rb +51 -73
- data/lib/active_support/test_case.rb +46 -17
- data/lib/active_support/testing/assertions.rb +56 -26
- data/lib/active_support/testing/autorun.rb +5 -0
- data/lib/active_support/testing/constant_lookup.rb +52 -0
- data/lib/active_support/testing/declarative.rb +1 -1
- data/lib/active_support/testing/deprecation.rb +0 -19
- data/lib/active_support/testing/isolation.rb +25 -58
- data/lib/active_support/testing/pending.rb +5 -43
- data/lib/active_support/testing/setup_and_teardown.rb +6 -92
- data/lib/active_support/testing/tagged_logging.rb +25 -0
- data/lib/active_support/time.rb +6 -21
- data/lib/active_support/time_with_zone.rb +78 -43
- data/lib/active_support/values/time_zone.rb +77 -58
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +4 -4
- data/lib/active_support/xml_mini.rb +35 -17
- data/lib/active_support/xml_mini/jdom.rb +9 -17
- data/lib/active_support/xml_mini/libxml.rb +1 -2
- data/lib/active_support/xml_mini/libxmlsax.rb +1 -2
- data/lib/active_support/xml_mini/nokogiri.rb +1 -2
- data/lib/active_support/xml_mini/nokogirisax.rb +1 -2
- data/lib/active_support/xml_mini/rexml.rb +6 -8
- metadata +107 -77
- data/lib/active_support/base64.rb +0 -54
- data/lib/active_support/core_ext/array/random_access.rb +0 -30
- data/lib/active_support/core_ext/date/freeze.rb +0 -33
- data/lib/active_support/core_ext/exception.rb +0 -3
- data/lib/active_support/core_ext/file/path.rb +0 -5
- data/lib/active_support/core_ext/float.rb +0 -1
- data/lib/active_support/core_ext/float/rounding.rb +0 -19
- data/lib/active_support/core_ext/hash/deep_dup.rb +0 -18
- data/lib/active_support/core_ext/io.rb +0 -15
- data/lib/active_support/core_ext/module/method_names.rb +0 -14
- data/lib/active_support/core_ext/module/synchronization.rb +0 -45
- data/lib/active_support/core_ext/process.rb +0 -1
- data/lib/active_support/core_ext/process/daemon.rb +0 -23
- data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
- data/lib/active_support/core_ext/range/cover.rb +0 -3
- data/lib/active_support/core_ext/rexml.rb +0 -46
- data/lib/active_support/core_ext/string/interpolation.rb +0 -2
- data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
- data/lib/active_support/memoizable.rb +0 -116
- data/lib/active_support/multibyte/exceptions.rb +0 -8
- data/lib/active_support/multibyte/utils.rb +0 -60
- data/lib/active_support/ruby/shim.rb +0 -22
- data/lib/active_support/security_utils.rb +0 -27
- data/lib/active_support/testing/mochaing.rb +0 -7
- data/lib/active_support/testing/performance.rb +0 -317
- data/lib/active_support/testing/performance/jruby.rb +0 -115
- data/lib/active_support/testing/performance/rubinius.rb +0 -113
- data/lib/active_support/testing/performance/ruby.rb +0 -152
- data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
- data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
- data/lib/active_support/time/autoload.rb +0 -5
- data/lib/active_support/whiny_nil.rb +0 -24
@@ -1,43 +1,38 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
require 'date'
|
3
|
-
require 'active_support/core_ext/time/publicize_conversion_methods'
|
4
2
|
require 'active_support/core_ext/time/calculations'
|
5
3
|
|
6
4
|
class String
|
7
|
-
#
|
8
|
-
#
|
5
|
+
# Converts a string to a Time value.
|
6
|
+
# The +form+ can be either :utc or :local (default :local).
|
9
7
|
#
|
10
|
-
#
|
11
|
-
#
|
8
|
+
# The time is parsed using Time.parse method.
|
9
|
+
# If +form+ is :local, then the time is in the system timezone.
|
10
|
+
# If the date part is missing then the current date is used and if
|
11
|
+
# the time part is missing then it is assumed to be 00:00:00.
|
12
12
|
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
#
|
22
|
-
# Note that the 224 is different in both examples. In ISO-8859-1 "à" is
|
23
|
-
# represented as a single byte, 224. In UTF8 it is represented with two
|
24
|
-
# bytes, namely 195 and 160, but its Unicode codepoint is 224. If we
|
25
|
-
# call +ord+ on the UTF8 string "à" the return value will be 195. That is
|
26
|
-
# not an error, because UTF8 is unsupported, the call itself would be
|
27
|
-
# bogus.
|
28
|
-
def ord
|
29
|
-
self[0]
|
30
|
-
end unless method_defined?(:ord)
|
13
|
+
# "13-12-2012".to_time # => 2012-12-13 00:00:00 +0100
|
14
|
+
# "06:12".to_time # => 2012-12-13 06:12:00 +0100
|
15
|
+
# "2012-12-13 06:12".to_time # => 2012-12-13 06:12:00 +0100
|
16
|
+
# "2012-12-13T06:12".to_time # => 2012-12-13 06:12:00 +0100
|
17
|
+
# "2012-12-13T06:12".to_time(:utc) # => 2012-12-13 05:12:00 UTC
|
18
|
+
def to_time(form = :local)
|
19
|
+
parts = Date._parse(self, false)
|
20
|
+
return if parts.empty?
|
31
21
|
|
32
|
-
|
33
|
-
|
22
|
+
now = Time.now
|
23
|
+
offset = parts[:offset]
|
24
|
+
utc_offset = form == :utc ? 0 : now.utc_offset
|
25
|
+
adjustment = offset ? offset - utc_offset : 0
|
34
26
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
27
|
+
Time.send(
|
28
|
+
form,
|
29
|
+
parts.fetch(:year, now.year),
|
30
|
+
parts.fetch(:mon, now.month),
|
31
|
+
parts.fetch(:mday, now.day),
|
32
|
+
parts.fetch(:hour, 0),
|
33
|
+
parts.fetch(:min, 0),
|
34
|
+
parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0)
|
35
|
+
) - adjustment
|
41
36
|
end
|
42
37
|
|
43
38
|
# Converts a string to a Date value.
|
@@ -47,8 +42,7 @@ class String
|
|
47
42
|
# "2012-12-13".to_date #=> Thu, 13 Dec 2012
|
48
43
|
# "12/13/2012".to_date #=> ArgumentError: invalid date
|
49
44
|
def to_date
|
50
|
-
|
51
|
-
::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
|
45
|
+
::Date.parse(self, false) unless blank?
|
52
46
|
end
|
53
47
|
|
54
48
|
# Converts a string to a DateTime value.
|
@@ -58,9 +52,6 @@ class String
|
|
58
52
|
# "2012-12-13 12:50".to_datetime #=> Thu, 13 Dec 2012 12:50:00 +0000
|
59
53
|
# "12/13/2012".to_datetime #=> ArgumentError: invalid date
|
60
54
|
def to_datetime
|
61
|
-
|
62
|
-
d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :sec_fraction).map { |arg| arg || 0 }
|
63
|
-
d[5] += d.pop
|
64
|
-
::DateTime.civil(*d)
|
55
|
+
::DateTime.parse(self, false) unless blank?
|
65
56
|
end
|
66
57
|
end
|
@@ -1,11 +1,8 @@
|
|
1
|
+
require 'active_support/deprecation'
|
2
|
+
|
1
3
|
class String
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
end
|
6
|
-
else
|
7
|
-
def encoding_aware?
|
8
|
-
false
|
9
|
-
end
|
4
|
+
def encoding_aware?
|
5
|
+
ActiveSupport::Deprecation.warn 'String#encoding_aware? is deprecated'
|
6
|
+
true
|
10
7
|
end
|
11
|
-
end
|
8
|
+
end
|
@@ -1,11 +1,10 @@
|
|
1
|
-
require 'active_support/core_ext/string/multibyte'
|
2
|
-
|
3
1
|
class String
|
4
2
|
# Returns the string, first removing all whitespace on both ends of
|
5
3
|
# the string, and then changing remaining consecutive whitespace
|
6
4
|
# groups into one space each.
|
7
5
|
#
|
8
|
-
#
|
6
|
+
# Note that it handles both ASCII and Unicode whitespace like mongolian vowel separator (U+180E).
|
7
|
+
#
|
9
8
|
# %{ Multi-line
|
10
9
|
# string }.squish # => "Multi-line string"
|
11
10
|
# " foo bar \n \t boo".squish # => "foo bar boo"
|
@@ -15,35 +14,42 @@ class String
|
|
15
14
|
|
16
15
|
# Performs a destructive squish. See String#squish.
|
17
16
|
def squish!
|
18
|
-
|
19
|
-
gsub!(
|
17
|
+
gsub!(/\A[[:space:]]+/, '')
|
18
|
+
gsub!(/[[:space:]]+\z/, '')
|
19
|
+
gsub!(/[[:space:]]+/, ' ')
|
20
20
|
self
|
21
21
|
end
|
22
22
|
|
23
23
|
# Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
|
24
24
|
#
|
25
|
-
#
|
25
|
+
# 'Once upon a time in a world far far away'.truncate(27)
|
26
26
|
# # => "Once upon a time in a wo..."
|
27
27
|
#
|
28
|
-
# Pass a <tt>:separator</tt> to truncate +text+ at a natural break:
|
28
|
+
# Pass a string or regexp <tt>:separator</tt> to truncate +text+ at a natural break:
|
29
|
+
#
|
30
|
+
# 'Once upon a time in a world far far away'.truncate(27, separator: ' ')
|
31
|
+
# # => "Once upon a time in a..."
|
29
32
|
#
|
30
|
-
#
|
33
|
+
# 'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
|
31
34
|
# # => "Once upon a time in a..."
|
32
35
|
#
|
33
36
|
# The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
|
34
|
-
# for a total length not exceeding <tt
|
37
|
+
# for a total length not exceeding <tt>length</tt>:
|
35
38
|
#
|
36
|
-
#
|
39
|
+
# 'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
|
37
40
|
# # => "And they f... (continued)"
|
38
|
-
def truncate(
|
39
|
-
|
40
|
-
options[:omission] ||= "..."
|
41
|
+
def truncate(truncate_at, options = {})
|
42
|
+
return dup unless length > truncate_at
|
41
43
|
|
42
|
-
|
43
|
-
|
44
|
-
stop =
|
45
|
-
|
44
|
+
options[:omission] ||= '...'
|
45
|
+
length_with_room_for_omission = truncate_at - options[:omission].length
|
46
|
+
stop = \
|
47
|
+
if options[:separator]
|
48
|
+
rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
|
49
|
+
else
|
50
|
+
length_with_room_for_omission
|
51
|
+
end
|
46
52
|
|
47
|
-
|
53
|
+
self[0...stop] + options[:omission]
|
48
54
|
end
|
49
55
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class String
|
2
|
+
# Same as +indent+, except it indents the receiver in-place.
|
3
|
+
#
|
4
|
+
# Returns the indented string, or +nil+ if there was nothing to indent.
|
5
|
+
def indent!(amount, indent_string=nil, indent_empty_lines=false)
|
6
|
+
indent_string = indent_string || self[/^[ \t]/] || ' '
|
7
|
+
re = indent_empty_lines ? /^/ : /^(?!$)/
|
8
|
+
gsub!(re, indent_string * amount)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Indents the lines in the receiver:
|
12
|
+
#
|
13
|
+
# <<EOS.indent(2)
|
14
|
+
# def some_method
|
15
|
+
# some_code
|
16
|
+
# end
|
17
|
+
# EOS
|
18
|
+
# # =>
|
19
|
+
# def some_method
|
20
|
+
# some_code
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# The second argument, +indent_string+, specifies which indent string to
|
24
|
+
# use. The default is +nil+, which tells the method to make a guess by
|
25
|
+
# peeking at the first indented line, and fallback to a space if there is
|
26
|
+
# none.
|
27
|
+
#
|
28
|
+
# " foo".indent(2) # => " foo"
|
29
|
+
# "foo\n\t\tbar".indent(2) # => "\t\tfoo\n\t\t\t\tbar"
|
30
|
+
# "foo".indent(2, "\t") # => "\t\tfoo"
|
31
|
+
#
|
32
|
+
# While +indent_string+ is tipically one space or tab, it may be any string.
|
33
|
+
#
|
34
|
+
# The third argument, +indent_empty_lines+, is a flag that says whether
|
35
|
+
# empty lines should be indented. Default is false.
|
36
|
+
#
|
37
|
+
# "foo\n\nbar".indent(2) # => " foo\n\n bar"
|
38
|
+
# "foo\n\nbar".indent(2, nil, true) # => " foo\n \n bar"
|
39
|
+
#
|
40
|
+
def indent(amount, indent_string=nil, indent_empty_lines=false)
|
41
|
+
dup.tap {|_| _.indent!(amount, indent_string, indent_empty_lines)}
|
42
|
+
end
|
43
|
+
end
|
@@ -4,7 +4,7 @@ require 'active_support/inflector/transliterate'
|
|
4
4
|
# String inflections define new methods on the String class to transform names for different purposes.
|
5
5
|
# For instance, you can figure out the name of a table from the name of a class.
|
6
6
|
#
|
7
|
-
#
|
7
|
+
# 'ScaleScore'.tableize # => "scale_scores"
|
8
8
|
#
|
9
9
|
class String
|
10
10
|
# Returns the plural form of the word in the string.
|
@@ -13,43 +13,55 @@ class String
|
|
13
13
|
# the singular form will be returned if <tt>count == 1</tt>.
|
14
14
|
# For any other value of +count+ the plural will be returned.
|
15
15
|
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
|
16
|
+
# If the optional parameter +locale+ is specified,
|
17
|
+
# the word will be pluralized as a word of that language.
|
18
|
+
# By default, this parameter is set to <tt>:en</tt>.
|
19
|
+
# You must define your own inflection rules for languages other than English.
|
20
|
+
#
|
21
|
+
# 'post'.pluralize # => "posts"
|
22
|
+
# 'octopus'.pluralize # => "octopi"
|
23
|
+
# 'sheep'.pluralize # => "sheep"
|
24
|
+
# 'words'.pluralize # => "words"
|
25
|
+
# 'the blue mailman'.pluralize # => "the blue mailmen"
|
26
|
+
# 'CamelOctopus'.pluralize # => "CamelOctopi"
|
27
|
+
# 'apple'.pluralize(1) # => "apple"
|
28
|
+
# 'apple'.pluralize(2) # => "apples"
|
29
|
+
# 'ley'.pluralize(:es) # => "leyes"
|
30
|
+
# 'ley'.pluralize(1, :es) # => "ley"
|
31
|
+
def pluralize(count = nil, locale = :en)
|
32
|
+
locale = count if count.is_a?(Symbol)
|
26
33
|
if count == 1
|
27
34
|
self
|
28
35
|
else
|
29
|
-
ActiveSupport::Inflector.pluralize(self)
|
36
|
+
ActiveSupport::Inflector.pluralize(self, locale)
|
30
37
|
end
|
31
38
|
end
|
32
39
|
|
33
40
|
# The reverse of +pluralize+, returns the singular form of a word in a string.
|
34
41
|
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
|
42
|
-
|
42
|
+
# If the optional parameter +locale+ is specified,
|
43
|
+
# the word will be singularized as a word of that language.
|
44
|
+
# By default, this paramter is set to <tt>:en</tt>.
|
45
|
+
# You must define your own inflection rules for languages other than English.
|
46
|
+
#
|
47
|
+
# 'posts'.singularize # => "post"
|
48
|
+
# 'octopi'.singularize # => "octopus"
|
49
|
+
# 'sheep'.singularize # => "sheep"
|
50
|
+
# 'word'.singularize # => "word"
|
51
|
+
# 'the blue mailmen'.singularize # => "the blue mailman"
|
52
|
+
# 'CamelOctopi'.singularize # => "CamelOctopus"
|
53
|
+
# 'leyes'.singularize(:es) # => "ley"
|
54
|
+
def singularize(locale = :en)
|
55
|
+
ActiveSupport::Inflector.singularize(self, locale)
|
43
56
|
end
|
44
57
|
|
45
58
|
# +constantize+ tries to find a declared constant with the name specified
|
46
59
|
# in the string. It raises a NameError when the name is not in CamelCase
|
47
60
|
# or is not initialized. See ActiveSupport::Inflector.constantize
|
48
61
|
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
# "blargle".constantize # => NameError: wrong constant name blargle
|
62
|
+
# 'Module'.constantize # => Module
|
63
|
+
# 'Class'.constantize # => Class
|
64
|
+
# 'blargle'.constantize # => NameError: wrong constant name blargle
|
53
65
|
def constantize
|
54
66
|
ActiveSupport::Inflector.constantize(self)
|
55
67
|
end
|
@@ -58,10 +70,9 @@ class String
|
|
58
70
|
# in the string. It returns nil when the name is not in CamelCase
|
59
71
|
# or is not initialized. See ActiveSupport::Inflector.safe_constantize
|
60
72
|
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
# "blargle".safe_constantize # => nil
|
73
|
+
# 'Module'.safe_constantize # => Module
|
74
|
+
# 'Class'.safe_constantize # => Class
|
75
|
+
# 'blargle'.safe_constantize # => nil
|
65
76
|
def safe_constantize
|
66
77
|
ActiveSupport::Inflector.safe_constantize(self)
|
67
78
|
end
|
@@ -71,14 +82,16 @@ class String
|
|
71
82
|
#
|
72
83
|
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
|
73
84
|
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
85
|
+
# 'active_record'.camelize # => "ActiveRecord"
|
86
|
+
# 'active_record'.camelize(:lower) # => "activeRecord"
|
87
|
+
# 'active_record/errors'.camelize # => "ActiveRecord::Errors"
|
88
|
+
# 'active_record/errors'.camelize(:lower) # => "activeRecord::Errors"
|
78
89
|
def camelize(first_letter = :upper)
|
79
90
|
case first_letter
|
80
|
-
|
81
|
-
|
91
|
+
when :upper
|
92
|
+
ActiveSupport::Inflector.camelize(self, true)
|
93
|
+
when :lower
|
94
|
+
ActiveSupport::Inflector.camelize(self, false)
|
82
95
|
end
|
83
96
|
end
|
84
97
|
alias_method :camelcase, :camelize
|
@@ -89,8 +102,8 @@ class String
|
|
89
102
|
#
|
90
103
|
# +titleize+ is also aliased as +titlecase+.
|
91
104
|
#
|
92
|
-
#
|
93
|
-
#
|
105
|
+
# 'man from the boondocks'.titleize # => "Man From The Boondocks"
|
106
|
+
# 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
|
94
107
|
def titleize
|
95
108
|
ActiveSupport::Inflector.titleize(self)
|
96
109
|
end
|
@@ -100,23 +113,23 @@ class String
|
|
100
113
|
#
|
101
114
|
# +underscore+ will also change '::' to '/' to convert namespaces to paths.
|
102
115
|
#
|
103
|
-
#
|
104
|
-
#
|
116
|
+
# 'ActiveModel'.underscore # => "active_model"
|
117
|
+
# 'ActiveModel::Errors'.underscore # => "active_model/errors"
|
105
118
|
def underscore
|
106
119
|
ActiveSupport::Inflector.underscore(self)
|
107
120
|
end
|
108
121
|
|
109
122
|
# Replaces underscores with dashes in the string.
|
110
123
|
#
|
111
|
-
#
|
124
|
+
# 'puni_puni'.dasherize # => "puni-puni"
|
112
125
|
def dasherize
|
113
126
|
ActiveSupport::Inflector.dasherize(self)
|
114
127
|
end
|
115
128
|
|
116
129
|
# Removes the module part from the constant expression in the string.
|
117
130
|
#
|
118
|
-
#
|
119
|
-
#
|
131
|
+
# 'ActiveRecord::CoreExtensions::String::Inflections'.demodulize # => "Inflections"
|
132
|
+
# 'Inflections'.demodulize # => "Inflections"
|
120
133
|
#
|
121
134
|
# See also +deconstantize+.
|
122
135
|
def demodulize
|
@@ -125,11 +138,11 @@ class String
|
|
125
138
|
|
126
139
|
# Removes the rightmost segment from the constant expression in the string.
|
127
140
|
#
|
128
|
-
#
|
129
|
-
#
|
130
|
-
#
|
131
|
-
#
|
132
|
-
#
|
141
|
+
# 'Net::HTTP'.deconstantize # => "Net"
|
142
|
+
# '::Net::HTTP'.deconstantize # => "::Net"
|
143
|
+
# 'String'.deconstantize # => ""
|
144
|
+
# '::String'.deconstantize # => ""
|
145
|
+
# ''.deconstantize # => ""
|
133
146
|
#
|
134
147
|
# See also +demodulize+.
|
135
148
|
def deconstantize
|
@@ -138,8 +151,6 @@ class String
|
|
138
151
|
|
139
152
|
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
|
140
153
|
#
|
141
|
-
# ==== Examples
|
142
|
-
#
|
143
154
|
# class Person
|
144
155
|
# def to_param
|
145
156
|
# "#{id}-#{name.parameterize}"
|
@@ -158,9 +169,9 @@ class String
|
|
158
169
|
# Creates the name of a table like Rails does for models to table names. This method
|
159
170
|
# uses the +pluralize+ method on the last word in the string.
|
160
171
|
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
172
|
+
# 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
|
173
|
+
# 'egg_and_ham'.tableize # => "egg_and_hams"
|
174
|
+
# 'fancyCategory'.tableize # => "fancy_categories"
|
164
175
|
def tableize
|
165
176
|
ActiveSupport::Inflector.tableize(self)
|
166
177
|
end
|
@@ -169,12 +180,12 @@ class String
|
|
169
180
|
# Note that this returns a string and not a class. (To convert to an actual class
|
170
181
|
# follow +classify+ with +constantize+.)
|
171
182
|
#
|
172
|
-
#
|
173
|
-
#
|
183
|
+
# 'egg_and_hams'.classify # => "EggAndHam"
|
184
|
+
# 'posts'.classify # => "Post"
|
174
185
|
#
|
175
186
|
# Singular names are not handled correctly.
|
176
187
|
#
|
177
|
-
#
|
188
|
+
# 'business'.classify # => "Busines"
|
178
189
|
def classify
|
179
190
|
ActiveSupport::Inflector.classify(self)
|
180
191
|
end
|
@@ -182,8 +193,8 @@ class String
|
|
182
193
|
# Capitalizes the first word, turns underscores into spaces, and strips '_id'.
|
183
194
|
# Like +titleize+, this is meant for creating pretty output.
|
184
195
|
#
|
185
|
-
#
|
186
|
-
#
|
196
|
+
# 'employee_salary'.humanize # => "Employee salary"
|
197
|
+
# 'author_id'.humanize # => "Author"
|
187
198
|
def humanize
|
188
199
|
ActiveSupport::Inflector.humanize(self)
|
189
200
|
end
|
@@ -192,10 +203,9 @@ class String
|
|
192
203
|
# +separate_class_name_and_id_with_underscore+ sets whether
|
193
204
|
# the method should put '_' between the name and 'id'.
|
194
205
|
#
|
195
|
-
#
|
196
|
-
#
|
197
|
-
#
|
198
|
-
# "Admin::Post".foreign_key # => "post_id"
|
206
|
+
# 'Message'.foreign_key # => "message_id"
|
207
|
+
# 'Message'.foreign_key(false) # => "messageid"
|
208
|
+
# 'Admin::Post'.foreign_key # => "post_id"
|
199
209
|
def foreign_key(separate_class_name_and_id_with_underscore = true)
|
200
210
|
ActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
|
201
211
|
end
|