activesupport 5.0.7.2
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 +1018 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +39 -0
- data/lib/active_support.rb +99 -0
- data/lib/active_support/all.rb +3 -0
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +103 -0
- data/lib/active_support/benchmarkable.rb +49 -0
- data/lib/active_support/builder.rb +6 -0
- data/lib/active_support/cache.rb +701 -0
- data/lib/active_support/cache/file_store.rb +204 -0
- data/lib/active_support/cache/mem_cache_store.rb +207 -0
- data/lib/active_support/cache/memory_store.rb +167 -0
- data/lib/active_support/cache/null_store.rb +41 -0
- data/lib/active_support/cache/strategy/local_cache.rb +172 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
- data/lib/active_support/callbacks.rb +791 -0
- data/lib/active_support/concern.rb +142 -0
- data/lib/active_support/concurrency/latch.rb +26 -0
- data/lib/active_support/concurrency/share_lock.rb +226 -0
- data/lib/active_support/configurable.rb +148 -0
- data/lib/active_support/core_ext.rb +4 -0
- data/lib/active_support/core_ext/array.rb +7 -0
- data/lib/active_support/core_ext/array/access.rb +90 -0
- data/lib/active_support/core_ext/array/conversions.rb +211 -0
- data/lib/active_support/core_ext/array/extract_options.rb +29 -0
- data/lib/active_support/core_ext/array/grouping.rb +107 -0
- 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 +46 -0
- data/lib/active_support/core_ext/benchmark.rb +14 -0
- data/lib/active_support/core_ext/big_decimal.rb +1 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/class.rb +2 -0
- data/lib/active_support/core_ext/class/attribute.rb +128 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -0
- data/lib/active_support/core_ext/class/subclasses.rb +41 -0
- data/lib/active_support/core_ext/date.rb +5 -0
- data/lib/active_support/core_ext/date/acts_like.rb +8 -0
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +143 -0
- data/lib/active_support/core_ext/date/conversions.rb +95 -0
- data/lib/active_support/core_ext/date/zones.rb +6 -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 +14 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time.rb +5 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +14 -0
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +199 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +105 -0
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +146 -0
- data/lib/active_support/core_ext/file.rb +1 -0
- data/lib/active_support/core_ext/file/atomic.rb +68 -0
- data/lib/active_support/core_ext/hash.rb +9 -0
- data/lib/active_support/core_ext/hash/compact.rb +24 -0
- data/lib/active_support/core_ext/hash/conversions.rb +262 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +38 -0
- data/lib/active_support/core_ext/hash/except.rb +22 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +23 -0
- data/lib/active_support/core_ext/hash/keys.rb +170 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -0
- data/lib/active_support/core_ext/hash/slice.rb +48 -0
- data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
- data/lib/active_support/core_ext/integer.rb +3 -0
- data/lib/active_support/core_ext/integer/inflections.rb +29 -0
- data/lib/active_support/core_ext/integer/multiple.rb +10 -0
- data/lib/active_support/core_ext/integer/time.rb +29 -0
- data/lib/active_support/core_ext/kernel.rb +4 -0
- data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/active_support/core_ext/kernel/concern.rb +12 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +3 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +43 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +6 -0
- data/lib/active_support/core_ext/load_error.rb +31 -0
- data/lib/active_support/core_ext/marshal.rb +22 -0
- data/lib/active_support/core_ext/module.rb +12 -0
- data/lib/active_support/core_ext/module/aliasing.rb +74 -0
- data/lib/active_support/core_ext/module/anonymous.rb +28 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +36 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
- 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 +216 -0
- data/lib/active_support/core_ext/module/deprecation.rb +23 -0
- data/lib/active_support/core_ext/module/introspection.rb +68 -0
- 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 +8 -0
- data/lib/active_support/core_ext/module/remove_method.rb +35 -0
- data/lib/active_support/core_ext/name_error.rb +31 -0
- data/lib/active_support/core_ext/numeric.rb +4 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +64 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +144 -0
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +74 -0
- data/lib/active_support/core_ext/object.rb +14 -0
- data/lib/active_support/core_ext/object/acts_like.rb +10 -0
- data/lib/active_support/core_ext/object/blank.rb +143 -0
- data/lib/active_support/core_ext/object/conversions.rb +4 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
- data/lib/active_support/core_ext/object/duplicable.rb +124 -0
- data/lib/active_support/core_ext/object/inclusion.rb +27 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +28 -0
- data/lib/active_support/core_ext/object/json.rb +205 -0
- data/lib/active_support/core_ext/object/to_param.rb +1 -0
- data/lib/active_support/core_ext/object/to_query.rb +84 -0
- data/lib/active_support/core_ext/object/try.rb +146 -0
- data/lib/active_support/core_ext/object/with_options.rb +69 -0
- data/lib/active_support/core_ext/range.rb +4 -0
- data/lib/active_support/core_ext/range/conversions.rb +31 -0
- data/lib/active_support/core_ext/range/each.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +8 -0
- data/lib/active_support/core_ext/regexp.rb +5 -0
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string.rb +13 -0
- data/lib/active_support/core_ext/string/access.rb +104 -0
- data/lib/active_support/core_ext/string/behavior.rb +6 -0
- data/lib/active_support/core_ext/string/conversions.rb +57 -0
- data/lib/active_support/core_ext/string/exclude.rb +11 -0
- data/lib/active_support/core_ext/string/filters.rb +102 -0
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +244 -0
- data/lib/active_support/core_ext/string/inquiry.rb +13 -0
- data/lib/active_support/core_ext/string/multibyte.rb +53 -0
- data/lib/active_support/core_ext/string/output_safety.rb +260 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/active_support/core_ext/string/strip.rb +23 -0
- data/lib/active_support/core_ext/string/zones.rb +14 -0
- data/lib/active_support/core_ext/struct.rb +3 -0
- data/lib/active_support/core_ext/time.rb +5 -0
- data/lib/active_support/core_ext/time/acts_like.rb +8 -0
- data/lib/active_support/core_ext/time/calculations.rb +290 -0
- data/lib/active_support/core_ext/time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/time/conversions.rb +67 -0
- data/lib/active_support/core_ext/time/marshal.rb +3 -0
- data/lib/active_support/core_ext/time/zones.rb +111 -0
- data/lib/active_support/core_ext/uri.rb +24 -0
- data/lib/active_support/dependencies.rb +755 -0
- data/lib/active_support/dependencies/autoload.rb +77 -0
- data/lib/active_support/dependencies/interlock.rb +55 -0
- data/lib/active_support/deprecation.rb +43 -0
- data/lib/active_support/deprecation/behaviors.rb +90 -0
- data/lib/active_support/deprecation/instance_delegator.rb +37 -0
- data/lib/active_support/deprecation/method_wrappers.rb +70 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +149 -0
- data/lib/active_support/deprecation/reporting.rb +112 -0
- data/lib/active_support/descendants_tracker.rb +60 -0
- data/lib/active_support/duration.rb +235 -0
- data/lib/active_support/duration/iso8601_parser.rb +122 -0
- data/lib/active_support/duration/iso8601_serializer.rb +51 -0
- data/lib/active_support/evented_file_update_checker.rb +199 -0
- data/lib/active_support/execution_wrapper.rb +126 -0
- data/lib/active_support/executor.rb +6 -0
- data/lib/active_support/file_update_checker.rb +157 -0
- data/lib/active_support/gem_version.rb +15 -0
- data/lib/active_support/gzip.rb +36 -0
- data/lib/active_support/hash_with_indifferent_access.rb +329 -0
- data/lib/active_support/i18n.rb +13 -0
- data/lib/active_support/i18n_railtie.rb +115 -0
- data/lib/active_support/inflections.rb +70 -0
- data/lib/active_support/inflector.rb +7 -0
- data/lib/active_support/inflector/inflections.rb +242 -0
- data/lib/active_support/inflector/methods.rb +390 -0
- data/lib/active_support/inflector/transliterate.rb +112 -0
- data/lib/active_support/json.rb +2 -0
- data/lib/active_support/json/decoding.rb +74 -0
- data/lib/active_support/json/encoding.rb +127 -0
- data/lib/active_support/key_generator.rb +71 -0
- data/lib/active_support/lazy_load_hooks.rb +76 -0
- data/lib/active_support/locale/en.yml +135 -0
- data/lib/active_support/log_subscriber.rb +109 -0
- data/lib/active_support/log_subscriber/test_helper.rb +104 -0
- 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 +114 -0
- data/lib/active_support/message_verifier.rb +134 -0
- data/lib/active_support/multibyte.rb +21 -0
- data/lib/active_support/multibyte/chars.rb +231 -0
- data/lib/active_support/multibyte/unicode.rb +413 -0
- data/lib/active_support/notifications.rb +212 -0
- data/lib/active_support/notifications/fanout.rb +157 -0
- data/lib/active_support/notifications/instrumenter.rb +91 -0
- data/lib/active_support/number_helper.rb +368 -0
- 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/option_merger.rb +25 -0
- data/lib/active_support/ordered_hash.rb +48 -0
- data/lib/active_support/ordered_options.rb +81 -0
- 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 +51 -0
- data/lib/active_support/reloader.rb +129 -0
- data/lib/active_support/rescuable.rb +173 -0
- data/lib/active_support/security_utils.rb +27 -0
- data/lib/active_support/string_inquirer.rb +26 -0
- data/lib/active_support/subscriber.rb +120 -0
- data/lib/active_support/tagged_logging.rb +77 -0
- data/lib/active_support/test_case.rb +88 -0
- data/lib/active_support/testing/assertions.rb +99 -0
- data/lib/active_support/testing/autorun.rb +5 -0
- data/lib/active_support/testing/constant_lookup.rb +50 -0
- data/lib/active_support/testing/declarative.rb +26 -0
- data/lib/active_support/testing/deprecation.rb +36 -0
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +115 -0
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/setup_and_teardown.rb +50 -0
- 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 +136 -0
- data/lib/active_support/time.rb +18 -0
- data/lib/active_support/time_with_zone.rb +511 -0
- data/lib/active_support/values/time_zone.rb +484 -0
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +8 -0
- data/lib/active_support/xml_mini.rb +209 -0
- data/lib/active_support/xml_mini/jdom.rb +181 -0
- data/lib/active_support/xml_mini/libxml.rb +77 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +82 -0
- data/lib/active_support/xml_mini/nokogiri.rb +81 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +85 -0
- data/lib/active_support/xml_mini/rexml.rb +128 -0
- metadata +349 -0
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'active_support/inflector/inflections'
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Defines the standard inflection rules. These are the starting point for
|
5
|
+
# new projects and are not considered complete. The current set of inflection
|
6
|
+
# rules is frozen. This means, we do not change them to become more complete.
|
7
|
+
# This is a safety measure to keep existing applications from breaking.
|
8
|
+
#++
|
9
|
+
module ActiveSupport
|
10
|
+
Inflector.inflections(:en) do |inflect|
|
11
|
+
inflect.plural(/$/, 's')
|
12
|
+
inflect.plural(/s$/i, 's')
|
13
|
+
inflect.plural(/^(ax|test)is$/i, '\1es')
|
14
|
+
inflect.plural(/(octop|vir)us$/i, '\1i')
|
15
|
+
inflect.plural(/(octop|vir)i$/i, '\1i')
|
16
|
+
inflect.plural(/(alias|status)$/i, '\1es')
|
17
|
+
inflect.plural(/(bu)s$/i, '\1ses')
|
18
|
+
inflect.plural(/(buffal|tomat)o$/i, '\1oes')
|
19
|
+
inflect.plural(/([ti])um$/i, '\1a')
|
20
|
+
inflect.plural(/([ti])a$/i, '\1a')
|
21
|
+
inflect.plural(/sis$/i, 'ses')
|
22
|
+
inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
|
23
|
+
inflect.plural(/(hive)$/i, '\1s')
|
24
|
+
inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies')
|
25
|
+
inflect.plural(/(x|ch|ss|sh)$/i, '\1es')
|
26
|
+
inflect.plural(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices')
|
27
|
+
inflect.plural(/^(m|l)ouse$/i, '\1ice')
|
28
|
+
inflect.plural(/^(m|l)ice$/i, '\1ice')
|
29
|
+
inflect.plural(/^(ox)$/i, '\1en')
|
30
|
+
inflect.plural(/^(oxen)$/i, '\1')
|
31
|
+
inflect.plural(/(quiz)$/i, '\1zes')
|
32
|
+
|
33
|
+
inflect.singular(/s$/i, '')
|
34
|
+
inflect.singular(/(ss)$/i, '\1')
|
35
|
+
inflect.singular(/(n)ews$/i, '\1ews')
|
36
|
+
inflect.singular(/([ti])a$/i, '\1um')
|
37
|
+
inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, '\1sis')
|
38
|
+
inflect.singular(/(^analy)(sis|ses)$/i, '\1sis')
|
39
|
+
inflect.singular(/([^f])ves$/i, '\1fe')
|
40
|
+
inflect.singular(/(hive)s$/i, '\1')
|
41
|
+
inflect.singular(/(tive)s$/i, '\1')
|
42
|
+
inflect.singular(/([lr])ves$/i, '\1f')
|
43
|
+
inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y')
|
44
|
+
inflect.singular(/(s)eries$/i, '\1eries')
|
45
|
+
inflect.singular(/(m)ovies$/i, '\1ovie')
|
46
|
+
inflect.singular(/(x|ch|ss|sh)es$/i, '\1')
|
47
|
+
inflect.singular(/^(m|l)ice$/i, '\1ouse')
|
48
|
+
inflect.singular(/(bus)(es)?$/i, '\1')
|
49
|
+
inflect.singular(/(o)es$/i, '\1')
|
50
|
+
inflect.singular(/(shoe)s$/i, '\1')
|
51
|
+
inflect.singular(/(cris|test)(is|es)$/i, '\1is')
|
52
|
+
inflect.singular(/^(a)x[ie]s$/i, '\1xis')
|
53
|
+
inflect.singular(/(octop|vir)(us|i)$/i, '\1us')
|
54
|
+
inflect.singular(/(alias|status)(es)?$/i, '\1')
|
55
|
+
inflect.singular(/^(ox)en/i, '\1')
|
56
|
+
inflect.singular(/(vert|ind)ices$/i, '\1ex')
|
57
|
+
inflect.singular(/(matr)ices$/i, '\1ix')
|
58
|
+
inflect.singular(/(quiz)zes$/i, '\1')
|
59
|
+
inflect.singular(/(database)s$/i, '\1')
|
60
|
+
|
61
|
+
inflect.irregular('person', 'people')
|
62
|
+
inflect.irregular('man', 'men')
|
63
|
+
inflect.irregular('child', 'children')
|
64
|
+
inflect.irregular('sex', 'sexes')
|
65
|
+
inflect.irregular('move', 'moves')
|
66
|
+
inflect.irregular('zombie', 'zombies')
|
67
|
+
|
68
|
+
inflect.uncountable(%w(equipment information rice money species series fish sheep jeans police))
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# in case active_support/inflector is required without the rest of active_support
|
2
|
+
require 'active_support/inflector/inflections'
|
3
|
+
require 'active_support/inflector/transliterate'
|
4
|
+
require 'active_support/inflector/methods'
|
5
|
+
|
6
|
+
require 'active_support/inflections'
|
7
|
+
require 'active_support/core_ext/string/inflections'
|
@@ -0,0 +1,242 @@
|
|
1
|
+
require 'concurrent/map'
|
2
|
+
require 'active_support/core_ext/array/prepend_and_append'
|
3
|
+
require 'active_support/i18n'
|
4
|
+
|
5
|
+
module ActiveSupport
|
6
|
+
module Inflector
|
7
|
+
extend self
|
8
|
+
|
9
|
+
# A singleton instance of this class is yielded by Inflector.inflections,
|
10
|
+
# which can then be used to specify additional inflection rules. If passed
|
11
|
+
# an optional locale, rules for other languages can be specified. The
|
12
|
+
# default locale is <tt>:en</tt>. Only rules for English are provided.
|
13
|
+
#
|
14
|
+
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
15
|
+
# inflect.plural /^(ox)$/i, '\1\2en'
|
16
|
+
# inflect.singular /^(ox)en/i, '\1'
|
17
|
+
#
|
18
|
+
# inflect.irregular 'octopus', 'octopi'
|
19
|
+
#
|
20
|
+
# inflect.uncountable 'equipment'
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# New rules are added at the top. So in the example above, the irregular
|
24
|
+
# rule for octopus will now be the first of the pluralization and
|
25
|
+
# singularization rules that is runs. This guarantees that your rules run
|
26
|
+
# before any of the rules that may already have been loaded.
|
27
|
+
class Inflections
|
28
|
+
@__instance__ = Concurrent::Map.new
|
29
|
+
|
30
|
+
class Uncountables < Array
|
31
|
+
def initialize
|
32
|
+
@regex_array = []
|
33
|
+
super
|
34
|
+
end
|
35
|
+
|
36
|
+
def delete(entry)
|
37
|
+
super entry
|
38
|
+
@regex_array.delete(to_regex(entry))
|
39
|
+
end
|
40
|
+
|
41
|
+
def <<(*word)
|
42
|
+
add(word)
|
43
|
+
end
|
44
|
+
|
45
|
+
def add(words)
|
46
|
+
self.concat(words.flatten.map(&:downcase))
|
47
|
+
@regex_array += self.map {|word| to_regex(word) }
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
def uncountable?(str)
|
52
|
+
@regex_array.any? { |regex| regex === str }
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
def to_regex(string)
|
57
|
+
/\b#{::Regexp.escape(string)}\Z/i
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.instance(locale = :en)
|
62
|
+
@__instance__[locale] ||= new
|
63
|
+
end
|
64
|
+
|
65
|
+
attr_reader :plurals, :singulars, :uncountables, :humans, :acronyms, :acronym_regex
|
66
|
+
|
67
|
+
def initialize
|
68
|
+
@plurals, @singulars, @uncountables, @humans, @acronyms, @acronym_regex = [], [], Uncountables.new, [], {}, /(?=a)b/
|
69
|
+
end
|
70
|
+
|
71
|
+
# Private, for the test suite.
|
72
|
+
def initialize_dup(orig) # :nodoc:
|
73
|
+
%w(plurals singulars uncountables humans acronyms acronym_regex).each do |scope|
|
74
|
+
instance_variable_set("@#{scope}", orig.send(scope).dup)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Specifies a new acronym. An acronym must be specified as it will appear
|
79
|
+
# in a camelized string. An underscore string that contains the acronym
|
80
|
+
# will retain the acronym when passed to +camelize+, +humanize+, or
|
81
|
+
# +titleize+. A camelized string that contains the acronym will maintain
|
82
|
+
# the acronym when titleized or humanized, and will convert the acronym
|
83
|
+
# into a non-delimited single lowercase word when passed to +underscore+.
|
84
|
+
#
|
85
|
+
# acronym 'HTML'
|
86
|
+
# titleize 'html' # => 'HTML'
|
87
|
+
# camelize 'html' # => 'HTML'
|
88
|
+
# underscore 'MyHTML' # => 'my_html'
|
89
|
+
#
|
90
|
+
# The acronym, however, must occur as a delimited unit and not be part of
|
91
|
+
# another word for conversions to recognize it:
|
92
|
+
#
|
93
|
+
# acronym 'HTTP'
|
94
|
+
# camelize 'my_http_delimited' # => 'MyHTTPDelimited'
|
95
|
+
# camelize 'https' # => 'Https', not 'HTTPs'
|
96
|
+
# underscore 'HTTPS' # => 'http_s', not 'https'
|
97
|
+
#
|
98
|
+
# acronym 'HTTPS'
|
99
|
+
# camelize 'https' # => 'HTTPS'
|
100
|
+
# underscore 'HTTPS' # => 'https'
|
101
|
+
#
|
102
|
+
# Note: Acronyms that are passed to +pluralize+ will no longer be
|
103
|
+
# recognized, since the acronym will not occur as a delimited unit in the
|
104
|
+
# pluralized result. To work around this, you must specify the pluralized
|
105
|
+
# form as an acronym as well:
|
106
|
+
#
|
107
|
+
# acronym 'API'
|
108
|
+
# camelize(pluralize('api')) # => 'Apis'
|
109
|
+
#
|
110
|
+
# acronym 'APIs'
|
111
|
+
# camelize(pluralize('api')) # => 'APIs'
|
112
|
+
#
|
113
|
+
# +acronym+ may be used to specify any word that contains an acronym or
|
114
|
+
# otherwise needs to maintain a non-standard capitalization. The only
|
115
|
+
# restriction is that the word must begin with a capital letter.
|
116
|
+
#
|
117
|
+
# acronym 'RESTful'
|
118
|
+
# underscore 'RESTful' # => 'restful'
|
119
|
+
# underscore 'RESTfulController' # => 'restful_controller'
|
120
|
+
# titleize 'RESTfulController' # => 'RESTful Controller'
|
121
|
+
# camelize 'restful' # => 'RESTful'
|
122
|
+
# camelize 'restful_controller' # => 'RESTfulController'
|
123
|
+
#
|
124
|
+
# acronym 'McDonald'
|
125
|
+
# underscore 'McDonald' # => 'mcdonald'
|
126
|
+
# camelize 'mcdonald' # => 'McDonald'
|
127
|
+
def acronym(word)
|
128
|
+
@acronyms[word.downcase] = word
|
129
|
+
@acronym_regex = /#{@acronyms.values.join("|")}/
|
130
|
+
end
|
131
|
+
|
132
|
+
# Specifies a new pluralization rule and its replacement. The rule can
|
133
|
+
# either be a string or a regular expression. The replacement should
|
134
|
+
# always be a string that may include references to the matched data from
|
135
|
+
# the rule.
|
136
|
+
def plural(rule, replacement)
|
137
|
+
@uncountables.delete(rule) if rule.is_a?(String)
|
138
|
+
@uncountables.delete(replacement)
|
139
|
+
@plurals.prepend([rule, replacement])
|
140
|
+
end
|
141
|
+
|
142
|
+
# Specifies a new singularization rule and its replacement. The rule can
|
143
|
+
# either be a string or a regular expression. The replacement should
|
144
|
+
# always be a string that may include references to the matched data from
|
145
|
+
# the rule.
|
146
|
+
def singular(rule, replacement)
|
147
|
+
@uncountables.delete(rule) if rule.is_a?(String)
|
148
|
+
@uncountables.delete(replacement)
|
149
|
+
@singulars.prepend([rule, replacement])
|
150
|
+
end
|
151
|
+
|
152
|
+
# Specifies a new irregular that applies to both pluralization and
|
153
|
+
# singularization at the same time. This can only be used for strings, not
|
154
|
+
# regular expressions. You simply pass the irregular in singular and
|
155
|
+
# plural form.
|
156
|
+
#
|
157
|
+
# irregular 'octopus', 'octopi'
|
158
|
+
# irregular 'person', 'people'
|
159
|
+
def irregular(singular, plural)
|
160
|
+
@uncountables.delete(singular)
|
161
|
+
@uncountables.delete(plural)
|
162
|
+
|
163
|
+
s0 = singular[0]
|
164
|
+
srest = singular[1..-1]
|
165
|
+
|
166
|
+
p0 = plural[0]
|
167
|
+
prest = plural[1..-1]
|
168
|
+
|
169
|
+
if s0.upcase == p0.upcase
|
170
|
+
plural(/(#{s0})#{srest}$/i, '\1' + prest)
|
171
|
+
plural(/(#{p0})#{prest}$/i, '\1' + prest)
|
172
|
+
|
173
|
+
singular(/(#{s0})#{srest}$/i, '\1' + srest)
|
174
|
+
singular(/(#{p0})#{prest}$/i, '\1' + srest)
|
175
|
+
else
|
176
|
+
plural(/#{s0.upcase}(?i)#{srest}$/, p0.upcase + prest)
|
177
|
+
plural(/#{s0.downcase}(?i)#{srest}$/, p0.downcase + prest)
|
178
|
+
plural(/#{p0.upcase}(?i)#{prest}$/, p0.upcase + prest)
|
179
|
+
plural(/#{p0.downcase}(?i)#{prest}$/, p0.downcase + prest)
|
180
|
+
|
181
|
+
singular(/#{s0.upcase}(?i)#{srest}$/, s0.upcase + srest)
|
182
|
+
singular(/#{s0.downcase}(?i)#{srest}$/, s0.downcase + srest)
|
183
|
+
singular(/#{p0.upcase}(?i)#{prest}$/, s0.upcase + srest)
|
184
|
+
singular(/#{p0.downcase}(?i)#{prest}$/, s0.downcase + srest)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
# Specifies words that are uncountable and should not be inflected.
|
189
|
+
#
|
190
|
+
# uncountable 'money'
|
191
|
+
# uncountable 'money', 'information'
|
192
|
+
# uncountable %w( money information rice )
|
193
|
+
def uncountable(*words)
|
194
|
+
@uncountables.add(words)
|
195
|
+
end
|
196
|
+
|
197
|
+
# Specifies a humanized form of a string by a regular expression rule or
|
198
|
+
# by a string mapping. When using a regular expression based replacement,
|
199
|
+
# the normal humanize formatting is called after the replacement. When a
|
200
|
+
# string is used, the human form should be specified as desired (example:
|
201
|
+
# 'The name', not 'the_name').
|
202
|
+
#
|
203
|
+
# human /_cnt$/i, '\1_count'
|
204
|
+
# human 'legacy_col_person_name', 'Name'
|
205
|
+
def human(rule, replacement)
|
206
|
+
@humans.prepend([rule, replacement])
|
207
|
+
end
|
208
|
+
|
209
|
+
# Clears the loaded inflections within a given scope (default is
|
210
|
+
# <tt>:all</tt>). Give the scope as a symbol of the inflection type, the
|
211
|
+
# options are: <tt>:plurals</tt>, <tt>:singulars</tt>, <tt>:uncountables</tt>,
|
212
|
+
# <tt>:humans</tt>.
|
213
|
+
#
|
214
|
+
# clear :all
|
215
|
+
# clear :plurals
|
216
|
+
def clear(scope = :all)
|
217
|
+
case scope
|
218
|
+
when :all
|
219
|
+
@plurals, @singulars, @uncountables, @humans = [], [], Uncountables.new, []
|
220
|
+
else
|
221
|
+
instance_variable_set "@#{scope}", []
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
# Yields a singleton instance of Inflector::Inflections so you can specify
|
227
|
+
# additional inflector rules. If passed an optional locale, rules for other
|
228
|
+
# languages can be specified. If not specified, defaults to <tt>:en</tt>.
|
229
|
+
# Only rules for English are provided.
|
230
|
+
#
|
231
|
+
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
232
|
+
# inflect.uncountable 'rails'
|
233
|
+
# end
|
234
|
+
def inflections(locale = :en)
|
235
|
+
if block_given?
|
236
|
+
yield Inflections.instance(locale)
|
237
|
+
else
|
238
|
+
Inflections.instance(locale)
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
@@ -0,0 +1,390 @@
|
|
1
|
+
require 'active_support/inflections'
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
# The Inflector transforms words from singular to plural, class names to table
|
5
|
+
# names, modularized class names to ones without, and class names to foreign
|
6
|
+
# keys. The default inflections for pluralization, singularization, and
|
7
|
+
# uncountable words are kept in inflections.rb.
|
8
|
+
#
|
9
|
+
# The Rails core team has stated patches for the inflections library will not
|
10
|
+
# be accepted in order to avoid breaking legacy applications which may be
|
11
|
+
# relying on errant inflections. If you discover an incorrect inflection and
|
12
|
+
# require it for your application or wish to define rules for languages other
|
13
|
+
# than English, please correct or add them yourself (explained below).
|
14
|
+
module Inflector
|
15
|
+
extend self
|
16
|
+
|
17
|
+
# Returns the plural form of the word in the string.
|
18
|
+
#
|
19
|
+
# If passed an optional +locale+ parameter, the word will be
|
20
|
+
# pluralized using rules defined for that language. By default,
|
21
|
+
# this parameter is set to <tt>:en</tt>.
|
22
|
+
#
|
23
|
+
# pluralize('post') # => "posts"
|
24
|
+
# pluralize('octopus') # => "octopi"
|
25
|
+
# pluralize('sheep') # => "sheep"
|
26
|
+
# pluralize('words') # => "words"
|
27
|
+
# pluralize('CamelOctopus') # => "CamelOctopi"
|
28
|
+
# pluralize('ley', :es) # => "leyes"
|
29
|
+
def pluralize(word, locale = :en)
|
30
|
+
apply_inflections(word, inflections(locale).plurals)
|
31
|
+
end
|
32
|
+
|
33
|
+
# The reverse of #pluralize, returns the singular form of a word in a
|
34
|
+
# string.
|
35
|
+
#
|
36
|
+
# If passed an optional +locale+ parameter, the word will be
|
37
|
+
# singularized using rules defined for that language. By default,
|
38
|
+
# this parameter is set to <tt>:en</tt>.
|
39
|
+
#
|
40
|
+
# singularize('posts') # => "post"
|
41
|
+
# singularize('octopi') # => "octopus"
|
42
|
+
# singularize('sheep') # => "sheep"
|
43
|
+
# singularize('word') # => "word"
|
44
|
+
# singularize('CamelOctopi') # => "CamelOctopus"
|
45
|
+
# singularize('leyes', :es) # => "ley"
|
46
|
+
def singularize(word, locale = :en)
|
47
|
+
apply_inflections(word, inflections(locale).singulars)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Converts strings to UpperCamelCase.
|
51
|
+
# If the +uppercase_first_letter+ parameter is set to false, then produces
|
52
|
+
# lowerCamelCase.
|
53
|
+
#
|
54
|
+
# Also converts '/' to '::' which is useful for converting
|
55
|
+
# paths to namespaces.
|
56
|
+
#
|
57
|
+
# camelize('active_model') # => "ActiveModel"
|
58
|
+
# camelize('active_model', false) # => "activeModel"
|
59
|
+
# camelize('active_model/errors') # => "ActiveModel::Errors"
|
60
|
+
# camelize('active_model/errors', false) # => "activeModel::Errors"
|
61
|
+
#
|
62
|
+
# As a rule of thumb you can think of +camelize+ as the inverse of
|
63
|
+
# #underscore, though there are cases where that does not hold:
|
64
|
+
#
|
65
|
+
# camelize(underscore('SSLError')) # => "SslError"
|
66
|
+
def camelize(term, uppercase_first_letter = true)
|
67
|
+
string = term.to_s
|
68
|
+
if uppercase_first_letter
|
69
|
+
string = string.sub(/^[a-z\d]*/) { |match| inflections.acronyms[match] || match.capitalize }
|
70
|
+
else
|
71
|
+
string = string.sub(/^(?:#{inflections.acronym_regex}(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
|
72
|
+
end
|
73
|
+
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{inflections.acronyms[$2] || $2.capitalize}" }
|
74
|
+
string.gsub!('/'.freeze, '::'.freeze)
|
75
|
+
string
|
76
|
+
end
|
77
|
+
|
78
|
+
# Makes an underscored, lowercase form from the expression in the string.
|
79
|
+
#
|
80
|
+
# Changes '::' to '/' to convert namespaces to paths.
|
81
|
+
#
|
82
|
+
# underscore('ActiveModel') # => "active_model"
|
83
|
+
# underscore('ActiveModel::Errors') # => "active_model/errors"
|
84
|
+
#
|
85
|
+
# As a rule of thumb you can think of +underscore+ as the inverse of
|
86
|
+
# #camelize, though there are cases where that does not hold:
|
87
|
+
#
|
88
|
+
# camelize(underscore('SSLError')) # => "SslError"
|
89
|
+
def underscore(camel_cased_word)
|
90
|
+
return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
|
91
|
+
word = camel_cased_word.to_s.gsub('::'.freeze, '/'.freeze)
|
92
|
+
word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1 && '_'.freeze }#{$2.downcase}" }
|
93
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
|
94
|
+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
|
95
|
+
word.tr!("-".freeze, "_".freeze)
|
96
|
+
word.downcase!
|
97
|
+
word
|
98
|
+
end
|
99
|
+
|
100
|
+
# Tweaks an attribute name for display to end users.
|
101
|
+
#
|
102
|
+
# Specifically, performs these transformations:
|
103
|
+
#
|
104
|
+
# * Applies human inflection rules to the argument.
|
105
|
+
# * Deletes leading underscores, if any.
|
106
|
+
# * Removes a "_id" suffix if present.
|
107
|
+
# * Replaces underscores with spaces, if any.
|
108
|
+
# * Downcases all words except acronyms.
|
109
|
+
# * Capitalizes the first word.
|
110
|
+
#
|
111
|
+
# The capitalization of the first word can be turned off by setting the
|
112
|
+
# +:capitalize+ option to false (default is true).
|
113
|
+
#
|
114
|
+
# humanize('employee_salary') # => "Employee salary"
|
115
|
+
# humanize('author_id') # => "Author"
|
116
|
+
# humanize('author_id', capitalize: false) # => "author"
|
117
|
+
# humanize('_id') # => "Id"
|
118
|
+
#
|
119
|
+
# If "SSL" was defined to be an acronym:
|
120
|
+
#
|
121
|
+
# humanize('ssl_error') # => "SSL error"
|
122
|
+
#
|
123
|
+
def humanize(lower_case_and_underscored_word, options = {})
|
124
|
+
result = lower_case_and_underscored_word.to_s.dup
|
125
|
+
|
126
|
+
inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
127
|
+
|
128
|
+
result.sub!(/\A_+/, ''.freeze)
|
129
|
+
result.sub!(/_id\z/, ''.freeze)
|
130
|
+
result.tr!('_'.freeze, ' '.freeze)
|
131
|
+
|
132
|
+
result.gsub!(/([a-z\d]*)/i) do |match|
|
133
|
+
"#{inflections.acronyms[match] || match.downcase}"
|
134
|
+
end
|
135
|
+
|
136
|
+
if options.fetch(:capitalize, true)
|
137
|
+
result.sub!(/\A\w/) { |match| match.upcase }
|
138
|
+
end
|
139
|
+
|
140
|
+
result
|
141
|
+
end
|
142
|
+
|
143
|
+
# Converts just the first character to uppercase.
|
144
|
+
#
|
145
|
+
# upcase_first('what a Lovely Day') # => "What a Lovely Day"
|
146
|
+
# upcase_first('w') # => "W"
|
147
|
+
# upcase_first('') # => ""
|
148
|
+
def upcase_first(string)
|
149
|
+
string.length > 0 ? string[0].upcase.concat(string[1..-1]) : ''
|
150
|
+
end
|
151
|
+
|
152
|
+
# Capitalizes all the words and replaces some characters in the string to
|
153
|
+
# create a nicer looking title. +titleize+ is meant for creating pretty
|
154
|
+
# output. It is not used in the Rails internals.
|
155
|
+
#
|
156
|
+
# +titleize+ is also aliased as +titlecase+.
|
157
|
+
#
|
158
|
+
# titleize('man from the boondocks') # => "Man From The Boondocks"
|
159
|
+
# titleize('x-men: the last stand') # => "X Men: The Last Stand"
|
160
|
+
# titleize('TheManWithoutAPast') # => "The Man Without A Past"
|
161
|
+
# titleize('raiders_of_the_lost_ark') # => "Raiders Of The Lost Ark"
|
162
|
+
def titleize(word)
|
163
|
+
humanize(underscore(word)).gsub(/\b(?<!['’`])[a-z]/) { |match| match.capitalize }
|
164
|
+
end
|
165
|
+
|
166
|
+
# Creates the name of a table like Rails does for models to table names.
|
167
|
+
# This method uses the #pluralize method on the last word in the string.
|
168
|
+
#
|
169
|
+
# tableize('RawScaledScorer') # => "raw_scaled_scorers"
|
170
|
+
# tableize('ham_and_egg') # => "ham_and_eggs"
|
171
|
+
# tableize('fancyCategory') # => "fancy_categories"
|
172
|
+
def tableize(class_name)
|
173
|
+
pluralize(underscore(class_name))
|
174
|
+
end
|
175
|
+
|
176
|
+
# Creates a class name from a plural table name like Rails does for table
|
177
|
+
# names to models. Note that this returns a string and not a Class (To
|
178
|
+
# convert to an actual class follow +classify+ with #constantize).
|
179
|
+
#
|
180
|
+
# classify('ham_and_eggs') # => "HamAndEgg"
|
181
|
+
# classify('posts') # => "Post"
|
182
|
+
#
|
183
|
+
# Singular names are not handled correctly:
|
184
|
+
#
|
185
|
+
# classify('calculus') # => "Calculus"
|
186
|
+
def classify(table_name)
|
187
|
+
# strip out any leading schema name
|
188
|
+
camelize(singularize(table_name.to_s.sub(/.*\./, ''.freeze)))
|
189
|
+
end
|
190
|
+
|
191
|
+
# Replaces underscores with dashes in the string.
|
192
|
+
#
|
193
|
+
# dasherize('puni_puni') # => "puni-puni"
|
194
|
+
def dasherize(underscored_word)
|
195
|
+
underscored_word.tr('_'.freeze, '-'.freeze)
|
196
|
+
end
|
197
|
+
|
198
|
+
# Removes the module part from the expression in the string.
|
199
|
+
#
|
200
|
+
# demodulize('ActiveRecord::CoreExtensions::String::Inflections') # => "Inflections"
|
201
|
+
# demodulize('Inflections') # => "Inflections"
|
202
|
+
# demodulize('::Inflections') # => "Inflections"
|
203
|
+
# demodulize('') # => ""
|
204
|
+
#
|
205
|
+
# See also #deconstantize.
|
206
|
+
def demodulize(path)
|
207
|
+
path = path.to_s
|
208
|
+
if i = path.rindex('::')
|
209
|
+
path[(i+2)..-1]
|
210
|
+
else
|
211
|
+
path
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
# Removes the rightmost segment from the constant expression in the string.
|
216
|
+
#
|
217
|
+
# deconstantize('Net::HTTP') # => "Net"
|
218
|
+
# deconstantize('::Net::HTTP') # => "::Net"
|
219
|
+
# deconstantize('String') # => ""
|
220
|
+
# deconstantize('::String') # => ""
|
221
|
+
# deconstantize('') # => ""
|
222
|
+
#
|
223
|
+
# See also #demodulize.
|
224
|
+
def deconstantize(path)
|
225
|
+
path.to_s[0, path.rindex('::') || 0] # implementation based on the one in facets' Module#spacename
|
226
|
+
end
|
227
|
+
|
228
|
+
# Creates a foreign key name from a class name.
|
229
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
230
|
+
# the method should put '_' between the name and 'id'.
|
231
|
+
#
|
232
|
+
# foreign_key('Message') # => "message_id"
|
233
|
+
# foreign_key('Message', false) # => "messageid"
|
234
|
+
# foreign_key('Admin::Post') # => "post_id"
|
235
|
+
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
|
236
|
+
underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
|
237
|
+
end
|
238
|
+
|
239
|
+
# Tries to find a constant with the name specified in the argument string.
|
240
|
+
#
|
241
|
+
# 'Module'.constantize # => Module
|
242
|
+
# 'Foo::Bar'.constantize # => Foo::Bar
|
243
|
+
#
|
244
|
+
# The name is assumed to be the one of a top-level constant, no matter
|
245
|
+
# whether it starts with "::" or not. No lexical context is taken into
|
246
|
+
# account:
|
247
|
+
#
|
248
|
+
# C = 'outside'
|
249
|
+
# module M
|
250
|
+
# C = 'inside'
|
251
|
+
# C # => 'inside'
|
252
|
+
# 'C'.constantize # => 'outside', same as ::C
|
253
|
+
# end
|
254
|
+
#
|
255
|
+
# NameError is raised when the name is not in CamelCase or the constant is
|
256
|
+
# unknown.
|
257
|
+
def constantize(camel_cased_word)
|
258
|
+
names = camel_cased_word.split('::'.freeze)
|
259
|
+
|
260
|
+
# Trigger a built-in NameError exception including the ill-formed constant in the message.
|
261
|
+
Object.const_get(camel_cased_word) if names.empty?
|
262
|
+
|
263
|
+
# Remove the first blank element in case of '::ClassName' notation.
|
264
|
+
names.shift if names.size > 1 && names.first.empty?
|
265
|
+
|
266
|
+
names.inject(Object) do |constant, name|
|
267
|
+
if constant == Object
|
268
|
+
constant.const_get(name)
|
269
|
+
else
|
270
|
+
candidate = constant.const_get(name)
|
271
|
+
next candidate if constant.const_defined?(name, false)
|
272
|
+
next candidate unless Object.const_defined?(name)
|
273
|
+
|
274
|
+
# Go down the ancestors to check if it is owned directly. The check
|
275
|
+
# stops when we reach Object or the end of ancestors tree.
|
276
|
+
constant = constant.ancestors.inject(constant) do |const, ancestor|
|
277
|
+
break const if ancestor == Object
|
278
|
+
break ancestor if ancestor.const_defined?(name, false)
|
279
|
+
const
|
280
|
+
end
|
281
|
+
|
282
|
+
# owner is in Object, so raise
|
283
|
+
constant.const_get(name, false)
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
# Tries to find a constant with the name specified in the argument string.
|
289
|
+
#
|
290
|
+
# safe_constantize('Module') # => Module
|
291
|
+
# safe_constantize('Foo::Bar') # => Foo::Bar
|
292
|
+
#
|
293
|
+
# The name is assumed to be the one of a top-level constant, no matter
|
294
|
+
# whether it starts with "::" or not. No lexical context is taken into
|
295
|
+
# account:
|
296
|
+
#
|
297
|
+
# C = 'outside'
|
298
|
+
# module M
|
299
|
+
# C = 'inside'
|
300
|
+
# C # => 'inside'
|
301
|
+
# safe_constantize('C') # => 'outside', same as ::C
|
302
|
+
# end
|
303
|
+
#
|
304
|
+
# +nil+ is returned when the name is not in CamelCase or the constant (or
|
305
|
+
# part of it) is unknown.
|
306
|
+
#
|
307
|
+
# safe_constantize('blargle') # => nil
|
308
|
+
# safe_constantize('UnknownModule') # => nil
|
309
|
+
# safe_constantize('UnknownModule::Foo::Bar') # => nil
|
310
|
+
def safe_constantize(camel_cased_word)
|
311
|
+
constantize(camel_cased_word)
|
312
|
+
rescue NameError => e
|
313
|
+
raise if e.name && !(camel_cased_word.to_s.split("::").include?(e.name.to_s) ||
|
314
|
+
e.name.to_s == camel_cased_word.to_s)
|
315
|
+
rescue ArgumentError => e
|
316
|
+
raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
|
317
|
+
end
|
318
|
+
|
319
|
+
# Returns the suffix that should be added to a number to denote the position
|
320
|
+
# in an ordered sequence such as 1st, 2nd, 3rd, 4th.
|
321
|
+
#
|
322
|
+
# ordinal(1) # => "st"
|
323
|
+
# ordinal(2) # => "nd"
|
324
|
+
# ordinal(1002) # => "nd"
|
325
|
+
# ordinal(1003) # => "rd"
|
326
|
+
# ordinal(-11) # => "th"
|
327
|
+
# ordinal(-1021) # => "st"
|
328
|
+
def ordinal(number)
|
329
|
+
abs_number = number.to_i.abs
|
330
|
+
|
331
|
+
if (11..13).include?(abs_number % 100)
|
332
|
+
"th"
|
333
|
+
else
|
334
|
+
case abs_number % 10
|
335
|
+
when 1; "st"
|
336
|
+
when 2; "nd"
|
337
|
+
when 3; "rd"
|
338
|
+
else "th"
|
339
|
+
end
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
# Turns a number into an ordinal string used to denote the position in an
|
344
|
+
# ordered sequence such as 1st, 2nd, 3rd, 4th.
|
345
|
+
#
|
346
|
+
# ordinalize(1) # => "1st"
|
347
|
+
# ordinalize(2) # => "2nd"
|
348
|
+
# ordinalize(1002) # => "1002nd"
|
349
|
+
# ordinalize(1003) # => "1003rd"
|
350
|
+
# ordinalize(-11) # => "-11th"
|
351
|
+
# ordinalize(-1021) # => "-1021st"
|
352
|
+
def ordinalize(number)
|
353
|
+
"#{number}#{ordinal(number)}"
|
354
|
+
end
|
355
|
+
|
356
|
+
private
|
357
|
+
|
358
|
+
# Mounts a regular expression, returned as a string to ease interpolation,
|
359
|
+
# that will match part by part the given constant.
|
360
|
+
#
|
361
|
+
# const_regexp("Foo::Bar::Baz") # => "Foo(::Bar(::Baz)?)?"
|
362
|
+
# const_regexp("::") # => "::"
|
363
|
+
def const_regexp(camel_cased_word) #:nodoc:
|
364
|
+
parts = camel_cased_word.split("::".freeze)
|
365
|
+
|
366
|
+
return Regexp.escape(camel_cased_word) if parts.blank?
|
367
|
+
|
368
|
+
last = parts.pop
|
369
|
+
|
370
|
+
parts.reverse.inject(last) do |acc, part|
|
371
|
+
part.empty? ? acc : "#{part}(::#{acc})?"
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
# Applies inflection rules for +singularize+ and +pluralize+.
|
376
|
+
#
|
377
|
+
# apply_inflections('post', inflections.plurals) # => "posts"
|
378
|
+
# apply_inflections('posts', inflections.singulars) # => "post"
|
379
|
+
def apply_inflections(word, rules)
|
380
|
+
result = word.to_s.dup
|
381
|
+
|
382
|
+
if word.empty? || inflections.uncountables.uncountable?(result)
|
383
|
+
result
|
384
|
+
else
|
385
|
+
rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) }
|
386
|
+
result
|
387
|
+
end
|
388
|
+
end
|
389
|
+
end
|
390
|
+
end
|