activesupport 3.1.0 → 5.0.0
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 +7 -0
- data/CHANGELOG.md +798 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +13 -7
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +38 -34
- data/lib/active_support/benchmarkable.rb +17 -28
- data/lib/active_support/cache/file_store.rb +85 -70
- data/lib/active_support/cache/mem_cache_store.rb +75 -66
- data/lib/active_support/cache/memory_store.rb +31 -23
- data/lib/active_support/cache/null_store.rb +41 -0
- data/lib/active_support/cache/strategy/local_cache.rb +73 -70
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
- data/lib/active_support/cache.rb +360 -294
- data/lib/active_support/callbacks.rb +563 -393
- data/lib/active_support/concern.rb +42 -34
- data/lib/active_support/concurrency/latch.rb +19 -0
- data/lib/active_support/concurrency/share_lock.rb +186 -0
- data/lib/active_support/configurable.rb +70 -12
- data/lib/active_support/core_ext/array/access.rb +53 -9
- data/lib/active_support/core_ext/array/conversions.rb +109 -62
- data/lib/active_support/core_ext/array/extract_options.rb +2 -2
- data/lib/active_support/core_ext/array/grouping.rb +39 -32
- data/lib/active_support/core_ext/array/inquiry.rb +17 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
- data/lib/active_support/core_ext/array/wrap.rb +16 -18
- data/lib/active_support/core_ext/array.rb +2 -2
- data/lib/active_support/core_ext/benchmark.rb +7 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -36
- data/lib/active_support/core_ext/class/attribute.rb +47 -34
- data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -79
- data/lib/active_support/core_ext/class/subclasses.rb +12 -7
- data/lib/active_support/core_ext/class.rb +0 -3
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +57 -167
- data/lib/active_support/core_ext/date/conversions.rb +31 -42
- data/lib/active_support/core_ext/date/zones.rb +2 -10
- data/lib/active_support/core_ext/date.rb +5 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +1 -0
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +132 -65
- data/lib/active_support/core_ext/date_time/compatibility.rb +5 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +36 -34
- data/lib/active_support/core_ext/date_time.rb +5 -0
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +81 -74
- data/lib/active_support/core_ext/file/atomic.rb +53 -26
- data/lib/active_support/core_ext/file.rb +0 -1
- data/lib/active_support/core_ext/hash/compact.rb +20 -0
- data/lib/active_support/core_ext/hash/conversions.rb +175 -70
- data/lib/active_support/core_ext/hash/deep_merge.rb +30 -8
- data/lib/active_support/core_ext/hash/except.rb +11 -12
- data/lib/active_support/core_ext/hash/indifferent_access.rb +7 -8
- data/lib/active_support/core_ext/hash/keys.rb +147 -24
- data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
- data/lib/active_support/core_ext/hash/slice.rb +22 -14
- data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
- data/lib/active_support/core_ext/hash.rb +2 -2
- data/lib/active_support/core_ext/integer/inflections.rb +13 -1
- data/lib/active_support/core_ext/integer/multiple.rb +4 -0
- data/lib/active_support/core_ext/integer/time.rb +12 -22
- data/lib/active_support/core_ext/kernel/agnostics.rb +2 -2
- data/lib/active_support/core_ext/kernel/concern.rb +12 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +2 -15
- data/lib/active_support/core_ext/kernel/reporting.rb +12 -62
- data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
- data/lib/active_support/core_ext/kernel.rb +2 -3
- data/lib/active_support/core_ext/load_error.rb +14 -7
- data/lib/active_support/core_ext/marshal.rb +22 -0
- data/lib/active_support/core_ext/module/aliasing.rb +16 -12
- data/lib/active_support/core_ext/module/anonymous.rb +12 -8
- data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
- data/lib/active_support/core_ext/module/attribute_accessors.rb +165 -13
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
- data/lib/active_support/core_ext/module/concerning.rb +135 -0
- data/lib/active_support/core_ext/module/delegation.rb +141 -68
- data/lib/active_support/core_ext/module/deprecation.rb +17 -3
- data/lib/active_support/core_ext/module/introspection.rb +9 -31
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
- data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
- data/lib/active_support/core_ext/module/reachable.rb +1 -3
- data/lib/active_support/core_ext/module/remove_method.rb +24 -5
- data/lib/active_support/core_ext/module.rb +3 -3
- data/lib/active_support/core_ext/name_error.rb +15 -2
- data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +31 -36
- data/lib/active_support/core_ext/numeric.rb +2 -0
- data/lib/active_support/core_ext/object/acts_like.rb +4 -4
- data/lib/active_support/core_ext/object/blank.rb +52 -18
- data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
- data/lib/active_support/core_ext/object/duplicable.rb +12 -20
- data/lib/active_support/core_ext/object/inclusion.rb +13 -1
- data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
- data/lib/active_support/core_ext/object/json.rb +205 -0
- data/lib/active_support/core_ext/object/to_param.rb +1 -55
- data/lib/active_support/core_ext/object/to_query.rb +66 -9
- data/lib/active_support/core_ext/object/try.rb +124 -33
- data/lib/active_support/core_ext/object/with_options.rb +37 -11
- data/lib/active_support/core_ext/object.rb +2 -1
- data/lib/active_support/core_ext/range/conversions.rb +17 -7
- data/lib/active_support/core_ext/range/each.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +20 -18
- data/lib/active_support/core_ext/range/overlaps.rb +1 -1
- data/lib/active_support/core_ext/range.rb +1 -2
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string/access.rb +95 -90
- data/lib/active_support/core_ext/string/behavior.rb +1 -1
- data/lib/active_support/core_ext/string/conversions.rb +41 -38
- data/lib/active_support/core_ext/string/exclude.rb +6 -1
- data/lib/active_support/core_ext/string/filters.rb +70 -17
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +139 -59
- data/lib/active_support/core_ext/string/inquiry.rb +2 -2
- data/lib/active_support/core_ext/string/multibyte.rb +46 -65
- data/lib/active_support/core_ext/string/output_safety.rb +153 -56
- data/lib/active_support/core_ext/string/strip.rb +3 -6
- data/lib/active_support/core_ext/string/zones.rb +14 -0
- data/lib/active_support/core_ext/string.rb +2 -3
- data/lib/active_support/core_ext/struct.rb +3 -0
- data/lib/active_support/core_ext/time/calculations.rb +173 -173
- data/lib/active_support/core_ext/time/compatibility.rb +5 -0
- data/lib/active_support/core_ext/time/conversions.rb +33 -29
- data/lib/active_support/core_ext/time/marshal.rb +2 -56
- data/lib/active_support/core_ext/time/zones.rb +57 -32
- data/lib/active_support/core_ext/time.rb +5 -0
- data/lib/active_support/core_ext/uri.rb +13 -19
- data/lib/active_support/core_ext.rb +3 -2
- data/lib/active_support/dependencies/autoload.rb +47 -20
- data/lib/active_support/dependencies/interlock.rb +51 -0
- data/lib/active_support/dependencies.rb +315 -265
- data/lib/active_support/deprecation/behaviors.rb +71 -30
- data/lib/active_support/deprecation/instance_delegator.rb +24 -0
- data/lib/active_support/deprecation/method_wrappers.rb +59 -18
- data/lib/active_support/deprecation/proxy_wrappers.rb +82 -14
- data/lib/active_support/deprecation/reporting.rb +61 -14
- data/lib/active_support/deprecation.rb +38 -13
- data/lib/active_support/descendants_tracker.rb +34 -19
- data/lib/active_support/duration/iso8601_parser.rb +122 -0
- data/lib/active_support/duration/iso8601_serializer.rb +51 -0
- data/lib/active_support/duration.rb +85 -14
- data/lib/active_support/evented_file_update_checker.rb +194 -0
- data/lib/active_support/execution_wrapper.rb +117 -0
- data/lib/active_support/executor.rb +6 -0
- data/lib/active_support/file_update_checker.rb +138 -17
- data/lib/active_support/gem_version.rb +15 -0
- data/lib/active_support/gzip.rb +11 -5
- data/lib/active_support/hash_with_indifferent_access.rb +199 -49
- data/lib/active_support/i18n.rb +6 -2
- data/lib/active_support/i18n_railtie.rb +40 -21
- data/lib/active_support/inflections.rb +22 -13
- data/lib/active_support/inflector/inflections.rb +175 -144
- data/lib/active_support/inflector/methods.rb +328 -91
- data/lib/active_support/inflector/transliterate.rb +51 -37
- data/lib/active_support/json/decoding.rb +31 -22
- data/lib/active_support/json/encoding.rb +88 -248
- data/lib/active_support/key_generator.rb +71 -0
- data/lib/active_support/lazy_load_hooks.rb +27 -25
- data/lib/active_support/locale/en.yml +102 -3
- data/lib/active_support/log_subscriber/test_helper.rb +24 -21
- data/lib/active_support/log_subscriber.rb +36 -49
- data/lib/active_support/logger.rb +106 -0
- data/lib/active_support/logger_silence.rb +28 -0
- data/lib/active_support/logger_thread_safe_level.rb +31 -0
- data/lib/active_support/message_encryptor.rb +72 -36
- data/lib/active_support/message_verifier.rb +96 -24
- data/lib/active_support/multibyte/chars.rb +88 -333
- data/lib/active_support/multibyte/unicode.rb +156 -136
- data/lib/active_support/multibyte.rb +5 -28
- data/lib/active_support/notifications/fanout.rb +115 -19
- data/lib/active_support/notifications/instrumenter.rb +52 -15
- data/lib/active_support/notifications.rb +168 -33
- data/lib/active_support/number_helper/number_converter.rb +182 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
- data/lib/active_support/number_helper.rb +368 -0
- data/lib/active_support/option_merger.rb +1 -1
- data/lib/active_support/ordered_hash.rb +18 -183
- data/lib/active_support/ordered_options.rb +44 -24
- data/lib/active_support/per_thread_registry.rb +58 -0
- data/lib/active_support/proxy_object.rb +13 -0
- data/lib/active_support/rails.rb +27 -0
- data/lib/active_support/railtie.rb +25 -34
- data/lib/active_support/reloader.rb +129 -0
- data/lib/active_support/rescuable.rb +98 -48
- data/lib/active_support/security_utils.rb +27 -0
- data/lib/active_support/string_inquirer.rb +14 -9
- data/lib/active_support/subscriber.rb +120 -0
- data/lib/active_support/tagged_logging.rb +78 -0
- data/lib/active_support/test_case.rb +69 -17
- data/lib/active_support/testing/assertions.rb +43 -41
- data/lib/active_support/testing/autorun.rb +12 -0
- data/lib/active_support/testing/constant_lookup.rb +50 -0
- data/lib/active_support/testing/declarative.rb +7 -21
- data/lib/active_support/testing/deprecation.rb +14 -33
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +53 -95
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/setup_and_teardown.rb +21 -82
- data/lib/active_support/testing/stream.rb +42 -0
- data/lib/active_support/testing/tagged_logging.rb +25 -0
- data/lib/active_support/testing/time_helpers.rb +134 -0
- data/lib/active_support/time.rb +6 -23
- data/lib/active_support/time_with_zone.rb +239 -92
- data/lib/active_support/values/time_zone.rb +236 -160
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +5 -7
- data/lib/active_support/xml_mini/jdom.rb +19 -13
- data/lib/active_support/xml_mini/libxml.rb +3 -4
- data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
- data/lib/active_support/xml_mini/nokogiri.rb +3 -4
- data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
- data/lib/active_support/xml_mini/rexml.rb +8 -10
- data/lib/active_support/xml_mini.rb +66 -34
- data/lib/active_support.rb +40 -23
- metadata +185 -134
- data/CHANGELOG +0 -1534
- data/lib/active_support/base64.rb +0 -42
- data/lib/active_support/basic_object.rb +0 -21
- data/lib/active_support/buffered_logger.rb +0 -137
- data/lib/active_support/cache/compressed_mem_cache_store.rb +0 -13
- data/lib/active_support/cache/synchronized_memory_store.rb +0 -11
- data/lib/active_support/core_ext/array/random_access.rb +0 -30
- data/lib/active_support/core_ext/array/uniq_by.rb +0 -16
- data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -44
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +0 -178
- data/lib/active_support/core_ext/date/freeze.rb +0 -31
- data/lib/active_support/core_ext/date_time/zones.rb +0 -21
- 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/rounding.rb +0 -19
- data/lib/active_support/core_ext/float.rb +0 -1
- data/lib/active_support/core_ext/hash/deep_dup.rb +0 -11
- data/lib/active_support/core_ext/hash/diff.rb +0 -13
- data/lib/active_support/core_ext/kernel/requires.rb +0 -28
- data/lib/active_support/core_ext/logger.rb +0 -81
- data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +0 -31
- data/lib/active_support/core_ext/module/method_names.rb +0 -14
- data/lib/active_support/core_ext/module/synchronization.rb +0 -43
- data/lib/active_support/core_ext/object/to_json.rb +0 -19
- data/lib/active_support/core_ext/proc.rb +0 -14
- data/lib/active_support/core_ext/process/daemon.rb +0 -23
- data/lib/active_support/core_ext/process.rb +0 -1
- 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/encoding.rb +0 -11
- data/lib/active_support/core_ext/string/interpolation.rb +0 -2
- data/lib/active_support/core_ext/string/xchar.rb +0 -18
- data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
- data/lib/active_support/file_watcher.rb +0 -36
- data/lib/active_support/json/variable.rb +0 -9
- data/lib/active_support/memoizable.rb +0 -105
- 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/secure_random.rb +0 -6
- data/lib/active_support/testing/mochaing.rb +0 -7
- data/lib/active_support/testing/pending.rb +0 -52
- 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/mri.rb +0 -57
- data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
- data/lib/active_support/testing/performance/ruby.rb +0 -152
- data/lib/active_support/testing/performance.rb +0 -317
- data/lib/active_support/time/autoload.rb +0 -5
- data/lib/active_support/whiny_nil.rb +0 -60
@@ -1,61 +1,97 @@
|
|
1
1
|
require 'active_support/inflector/methods'
|
2
|
-
require 'active_support/inflector/inflections'
|
3
2
|
require 'active_support/inflector/transliterate'
|
4
3
|
|
5
4
|
# String inflections define new methods on the String class to transform names for different purposes.
|
6
5
|
# For instance, you can figure out the name of a table from the name of a class.
|
7
6
|
#
|
8
|
-
#
|
7
|
+
# 'ScaleScore'.tableize # => "scale_scores"
|
9
8
|
#
|
10
9
|
class String
|
11
10
|
# Returns the plural form of the word in the string.
|
12
11
|
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
|
20
|
-
|
12
|
+
# If the optional parameter +count+ is specified,
|
13
|
+
# the singular form will be returned if <tt>count == 1</tt>.
|
14
|
+
# For any other value of +count+ the plural will be returned.
|
15
|
+
#
|
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)
|
33
|
+
if count == 1
|
34
|
+
self.dup
|
35
|
+
else
|
36
|
+
ActiveSupport::Inflector.pluralize(self, locale)
|
37
|
+
end
|
21
38
|
end
|
22
39
|
|
23
40
|
# The reverse of +pluralize+, returns the singular form of a word in a string.
|
24
41
|
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
|
32
|
-
|
42
|
+
# If the optional parameter +locale+ is specified,
|
43
|
+
# the word will be singularized as a word of that language.
|
44
|
+
# By default, this parameter 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)
|
33
56
|
end
|
34
57
|
|
35
58
|
# +constantize+ tries to find a declared constant with the name specified
|
36
59
|
# in the string. It raises a NameError when the name is not in CamelCase
|
37
|
-
# or is not initialized.
|
60
|
+
# or is not initialized. See ActiveSupport::Inflector.constantize
|
38
61
|
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
62
|
+
# 'Module'.constantize # => Module
|
63
|
+
# 'Class'.constantize # => Class
|
64
|
+
# 'blargle'.constantize # => NameError: wrong constant name blargle
|
42
65
|
def constantize
|
43
66
|
ActiveSupport::Inflector.constantize(self)
|
44
67
|
end
|
45
68
|
|
69
|
+
# +safe_constantize+ tries to find a declared constant with the name specified
|
70
|
+
# in the string. It returns nil when the name is not in CamelCase
|
71
|
+
# or is not initialized. See ActiveSupport::Inflector.safe_constantize
|
72
|
+
#
|
73
|
+
# 'Module'.safe_constantize # => Module
|
74
|
+
# 'Class'.safe_constantize # => Class
|
75
|
+
# 'blargle'.safe_constantize # => nil
|
76
|
+
def safe_constantize
|
77
|
+
ActiveSupport::Inflector.safe_constantize(self)
|
78
|
+
end
|
79
|
+
|
46
80
|
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
|
47
81
|
# is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
|
48
82
|
#
|
49
83
|
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
|
50
84
|
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
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"
|
55
89
|
def camelize(first_letter = :upper)
|
56
90
|
case first_letter
|
57
|
-
|
58
|
-
|
91
|
+
when :upper
|
92
|
+
ActiveSupport::Inflector.camelize(self, true)
|
93
|
+
when :lower
|
94
|
+
ActiveSupport::Inflector.camelize(self, false)
|
59
95
|
end
|
60
96
|
end
|
61
97
|
alias_method :camelcase, :camelize
|
@@ -66,8 +102,8 @@ class String
|
|
66
102
|
#
|
67
103
|
# +titleize+ is also aliased as +titlecase+.
|
68
104
|
#
|
69
|
-
#
|
70
|
-
#
|
105
|
+
# 'man from the boondocks'.titleize # => "Man From The Boondocks"
|
106
|
+
# 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
|
71
107
|
def titleize
|
72
108
|
ActiveSupport::Inflector.titleize(self)
|
73
109
|
end
|
@@ -77,30 +113,45 @@ class String
|
|
77
113
|
#
|
78
114
|
# +underscore+ will also change '::' to '/' to convert namespaces to paths.
|
79
115
|
#
|
80
|
-
#
|
81
|
-
#
|
116
|
+
# 'ActiveModel'.underscore # => "active_model"
|
117
|
+
# 'ActiveModel::Errors'.underscore # => "active_model/errors"
|
82
118
|
def underscore
|
83
119
|
ActiveSupport::Inflector.underscore(self)
|
84
120
|
end
|
85
121
|
|
86
122
|
# Replaces underscores with dashes in the string.
|
87
123
|
#
|
88
|
-
#
|
124
|
+
# 'puni_puni'.dasherize # => "puni-puni"
|
89
125
|
def dasherize
|
90
126
|
ActiveSupport::Inflector.dasherize(self)
|
91
127
|
end
|
92
128
|
|
93
129
|
# Removes the module part from the constant expression in the string.
|
94
130
|
#
|
95
|
-
#
|
96
|
-
#
|
131
|
+
# 'ActiveRecord::CoreExtensions::String::Inflections'.demodulize # => "Inflections"
|
132
|
+
# 'Inflections'.demodulize # => "Inflections"
|
133
|
+
# '::Inflections'.demodulize # => "Inflections"
|
134
|
+
# ''.demodulize # => ''
|
135
|
+
#
|
136
|
+
# See also +deconstantize+.
|
97
137
|
def demodulize
|
98
138
|
ActiveSupport::Inflector.demodulize(self)
|
99
139
|
end
|
100
140
|
|
101
|
-
#
|
141
|
+
# Removes the rightmost segment from the constant expression in the string.
|
102
142
|
#
|
103
|
-
#
|
143
|
+
# 'Net::HTTP'.deconstantize # => "Net"
|
144
|
+
# '::Net::HTTP'.deconstantize # => "::Net"
|
145
|
+
# 'String'.deconstantize # => ""
|
146
|
+
# '::String'.deconstantize # => ""
|
147
|
+
# ''.deconstantize # => ""
|
148
|
+
#
|
149
|
+
# See also +demodulize+.
|
150
|
+
def deconstantize
|
151
|
+
ActiveSupport::Inflector.deconstantize(self)
|
152
|
+
end
|
153
|
+
|
154
|
+
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
|
104
155
|
#
|
105
156
|
# class Person
|
106
157
|
# def to_param
|
@@ -111,53 +162,82 @@ class String
|
|
111
162
|
# @person = Person.find(1)
|
112
163
|
# # => #<Person id: 1, name: "Donald E. Knuth">
|
113
164
|
#
|
114
|
-
# <%= link_to(@person.name, person_path %>
|
165
|
+
# <%= link_to(@person.name, person_path) %>
|
115
166
|
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
|
116
|
-
|
117
|
-
|
167
|
+
#
|
168
|
+
# To preserve the case of the characters in a string, use the `preserve_case` argument.
|
169
|
+
#
|
170
|
+
# class Person
|
171
|
+
# def to_param
|
172
|
+
# "#{id}-#{name.parameterize(preserve_case: true)}"
|
173
|
+
# end
|
174
|
+
# end
|
175
|
+
#
|
176
|
+
# @person = Person.find(1)
|
177
|
+
# # => #<Person id: 1, name: "Donald E. Knuth">
|
178
|
+
#
|
179
|
+
# <%= link_to(@person.name, person_path) %>
|
180
|
+
# # => <a href="/person/1-Donald-E-Knuth">Donald E. Knuth</a>
|
181
|
+
def parameterize(sep = :unused, separator: '-', preserve_case: false)
|
182
|
+
unless sep == :unused
|
183
|
+
ActiveSupport::Deprecation.warn("Passing the separator argument as a positional parameter is deprecated and will soon be removed. Use `separator: '#{sep}'` instead.")
|
184
|
+
separator = sep
|
185
|
+
end
|
186
|
+
ActiveSupport::Inflector.parameterize(self, separator: separator, preserve_case: preserve_case)
|
118
187
|
end
|
119
188
|
|
120
189
|
# Creates the name of a table like Rails does for models to table names. This method
|
121
190
|
# uses the +pluralize+ method on the last word in the string.
|
122
191
|
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
192
|
+
# 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
|
193
|
+
# 'ham_and_egg'.tableize # => "ham_and_eggs"
|
194
|
+
# 'fancyCategory'.tableize # => "fancy_categories"
|
126
195
|
def tableize
|
127
196
|
ActiveSupport::Inflector.tableize(self)
|
128
197
|
end
|
129
198
|
|
130
|
-
#
|
199
|
+
# Creates a class name from a plural table name like Rails does for table names to models.
|
131
200
|
# Note that this returns a string and not a class. (To convert to an actual class
|
132
201
|
# follow +classify+ with +constantize+.)
|
133
202
|
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
# Singular names are not handled correctly.
|
138
|
-
#
|
139
|
-
# "business".classify # => "Busines"
|
203
|
+
# 'ham_and_eggs'.classify # => "HamAndEgg"
|
204
|
+
# 'posts'.classify # => "Post"
|
140
205
|
def classify
|
141
206
|
ActiveSupport::Inflector.classify(self)
|
142
207
|
end
|
143
208
|
|
144
|
-
# Capitalizes the first word, turns underscores into spaces, and strips
|
209
|
+
# Capitalizes the first word, turns underscores into spaces, and strips a
|
210
|
+
# trailing '_id' if present.
|
145
211
|
# Like +titleize+, this is meant for creating pretty output.
|
146
212
|
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
|
150
|
-
|
213
|
+
# The capitalization of the first word can be turned off by setting the
|
214
|
+
# optional parameter +capitalize+ to false.
|
215
|
+
# By default, this parameter is true.
|
216
|
+
#
|
217
|
+
# 'employee_salary'.humanize # => "Employee salary"
|
218
|
+
# 'author_id'.humanize # => "Author"
|
219
|
+
# 'author_id'.humanize(capitalize: false) # => "author"
|
220
|
+
# '_id'.humanize # => "Id"
|
221
|
+
def humanize(options = {})
|
222
|
+
ActiveSupport::Inflector.humanize(self, options)
|
223
|
+
end
|
224
|
+
|
225
|
+
# Converts just the first character to uppercase.
|
226
|
+
#
|
227
|
+
# 'what a Lovely Day'.upcase_first # => "What a Lovely Day"
|
228
|
+
# 'w'.upcase_first # => "W"
|
229
|
+
# ''.upcase_first # => ""
|
230
|
+
def upcase_first
|
231
|
+
ActiveSupport::Inflector.upcase_first(self)
|
151
232
|
end
|
152
233
|
|
153
234
|
# Creates a foreign key name from a class name.
|
154
235
|
# +separate_class_name_and_id_with_underscore+ sets whether
|
155
236
|
# the method should put '_' between the name and 'id'.
|
156
237
|
#
|
157
|
-
#
|
158
|
-
#
|
159
|
-
#
|
160
|
-
# "Admin::Post".foreign_key # => "post_id"
|
238
|
+
# 'Message'.foreign_key # => "message_id"
|
239
|
+
# 'Message'.foreign_key(false) # => "messageid"
|
240
|
+
# 'Admin::Post'.foreign_key # => "post_id"
|
161
241
|
def foreign_key(separate_class_name_and_id_with_underscore = true)
|
162
242
|
ActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
|
163
243
|
end
|
@@ -2,9 +2,9 @@ require 'active_support/string_inquirer'
|
|
2
2
|
|
3
3
|
class String
|
4
4
|
# Wraps the current string in the <tt>ActiveSupport::StringInquirer</tt> class,
|
5
|
-
# which gives you a prettier way to test for equality.
|
5
|
+
# which gives you a prettier way to test for equality.
|
6
6
|
#
|
7
|
-
# env =
|
7
|
+
# env = 'production'.inquiry
|
8
8
|
# env.production? # => true
|
9
9
|
# env.development? # => false
|
10
10
|
def inquiry
|
@@ -1,72 +1,53 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
require 'active_support/multibyte'
|
3
2
|
|
4
3
|
class String
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
#
|
37
|
-
# For more information about the methods defined on the Chars proxy see ActiveSupport::Multibyte::Chars. For
|
38
|
-
# information about how to change the default Multibyte behavior see ActiveSupport::Multibyte.
|
39
|
-
def mb_chars
|
40
|
-
if ActiveSupport::Multibyte.proxy_class.consumes?(self)
|
41
|
-
ActiveSupport::Multibyte.proxy_class.new(self)
|
42
|
-
else
|
43
|
-
self
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def is_utf8? #:nodoc
|
48
|
-
case encoding
|
49
|
-
when Encoding::UTF_8
|
50
|
-
valid_encoding?
|
51
|
-
when Encoding::ASCII_8BIT, Encoding::US_ASCII
|
52
|
-
dup.force_encoding(Encoding::UTF_8).valid_encoding?
|
53
|
-
else
|
54
|
-
false
|
55
|
-
end
|
56
|
-
end
|
57
|
-
else
|
58
|
-
def mb_chars
|
59
|
-
if ActiveSupport::Multibyte.proxy_class.wants?(self)
|
60
|
-
ActiveSupport::Multibyte.proxy_class.new(self)
|
61
|
-
else
|
62
|
-
self
|
63
|
-
end
|
64
|
-
end
|
4
|
+
# == Multibyte proxy
|
5
|
+
#
|
6
|
+
# +mb_chars+ is a multibyte safe proxy for string methods.
|
7
|
+
#
|
8
|
+
# It creates and returns an instance of the ActiveSupport::Multibyte::Chars class which
|
9
|
+
# encapsulates the original string. A Unicode safe version of all the String methods are defined on this proxy
|
10
|
+
# class. If the proxy class doesn't respond to a certain method, it's forwarded to the encapsulated string.
|
11
|
+
#
|
12
|
+
# >> "lj".upcase
|
13
|
+
# => "lj"
|
14
|
+
# >> "lj".mb_chars.upcase.to_s
|
15
|
+
# => "LJ"
|
16
|
+
#
|
17
|
+
# == Method chaining
|
18
|
+
#
|
19
|
+
# All the methods on the Chars proxy which normally return a string will return a Chars object. This allows
|
20
|
+
# method chaining on the result of any of these methods.
|
21
|
+
#
|
22
|
+
# name.mb_chars.reverse.length # => 12
|
23
|
+
#
|
24
|
+
# == Interoperability and configuration
|
25
|
+
#
|
26
|
+
# The Chars object tries to be as interchangeable with String objects as possible: sorting and comparing between
|
27
|
+
# String and Char work like expected. The bang! methods change the internal string representation in the Chars
|
28
|
+
# object. Interoperability problems can be resolved easily with a +to_s+ call.
|
29
|
+
#
|
30
|
+
# For more information about the methods defined on the Chars proxy see ActiveSupport::Multibyte::Chars. For
|
31
|
+
# information about how to change the default Multibyte behavior see ActiveSupport::Multibyte.
|
32
|
+
def mb_chars
|
33
|
+
ActiveSupport::Multibyte.proxy_class.new(self)
|
34
|
+
end
|
65
35
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
36
|
+
# Returns +true+ if string has utf_8 encoding.
|
37
|
+
#
|
38
|
+
# utf_8_str = "some string".encode "UTF-8"
|
39
|
+
# iso_str = "some string".encode "ISO-8859-1"
|
40
|
+
#
|
41
|
+
# utf_8_str.is_utf8? # => true
|
42
|
+
# iso_str.is_utf8? # => false
|
43
|
+
def is_utf8?
|
44
|
+
case encoding
|
45
|
+
when Encoding::UTF_8
|
46
|
+
valid_encoding?
|
47
|
+
when Encoding::ASCII_8BIT, Encoding::US_ASCII
|
48
|
+
dup.force_encoding(Encoding::UTF_8).valid_encoding?
|
49
|
+
else
|
50
|
+
false
|
70
51
|
end
|
71
52
|
end
|
72
53
|
end
|