activesupport 3.0.pre → 3.0.0.rc
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.
- data/CHANGELOG +121 -1
- data/README.rdoc +33 -0
- data/lib/active_support.rb +42 -4
- data/lib/active_support/backtrace_cleaner.rb +31 -9
- data/lib/active_support/benchmarkable.rb +1 -0
- data/lib/active_support/buffered_logger.rb +5 -2
- data/lib/active_support/builder.rb +6 -0
- data/lib/active_support/cache.rb +454 -84
- data/lib/active_support/cache/compressed_mem_cache_store.rb +6 -13
- data/lib/active_support/cache/file_store.rb +140 -41
- data/lib/active_support/cache/mem_cache_store.rb +121 -76
- data/lib/active_support/cache/memory_store.rb +127 -27
- data/lib/active_support/cache/strategy/local_cache.rb +111 -58
- data/lib/active_support/cache/synchronized_memory_store.rb +2 -38
- data/lib/active_support/callbacks.rb +105 -76
- data/lib/active_support/configurable.rb +19 -18
- data/lib/active_support/core_ext/array.rb +1 -0
- data/lib/active_support/core_ext/array/access.rb +1 -1
- data/lib/active_support/core_ext/array/conversions.rb +29 -54
- data/lib/active_support/core_ext/array/extract_options.rb +16 -1
- data/lib/active_support/core_ext/array/grouping.rb +1 -1
- data/lib/active_support/core_ext/array/random_access.rb +26 -5
- data/lib/active_support/core_ext/array/uniq_by.rb +17 -0
- data/lib/active_support/core_ext/array/wrap.rb +13 -9
- data/lib/active_support/core_ext/benchmark.rb +0 -12
- data/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +2 -0
- data/lib/active_support/core_ext/class.rb +2 -1
- data/lib/active_support/core_ext/class/attribute.rb +67 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +33 -27
- data/lib/active_support/core_ext/class/delegating_attributes.rb +35 -41
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +23 -14
- data/lib/active_support/core_ext/class/subclasses.rb +50 -0
- data/lib/active_support/core_ext/date/calculations.rb +35 -12
- data/lib/active_support/core_ext/date/conversions.rb +5 -5
- data/lib/active_support/core_ext/date/zones.rb +14 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +3 -1
- data/lib/active_support/core_ext/date_time/conversions.rb +24 -5
- data/lib/active_support/core_ext/date_time/zones.rb +4 -0
- data/lib/active_support/core_ext/enumerable.rb +5 -9
- data/lib/active_support/core_ext/exception.rb +0 -47
- data/lib/active_support/core_ext/file.rb +1 -0
- data/lib/active_support/core_ext/file/atomic.rb +3 -2
- data/lib/active_support/core_ext/file/path.rb +5 -0
- data/lib/active_support/core_ext/float/rounding.rb +3 -2
- data/lib/active_support/core_ext/hash/conversions.rb +65 -145
- data/lib/active_support/core_ext/hash/deep_merge.rb +6 -7
- data/lib/active_support/core_ext/hash/except.rb +8 -1
- data/lib/active_support/core_ext/hash/indifferent_access.rb +5 -0
- data/lib/active_support/core_ext/hash/keys.rb +10 -11
- data/lib/active_support/core_ext/hash/slice.rb +6 -0
- data/lib/active_support/core_ext/integer.rb +1 -1
- data/lib/active_support/core_ext/integer/multiple.rb +6 -0
- data/lib/active_support/core_ext/kernel.rb +1 -1
- data/lib/active_support/core_ext/kernel/debugger.rb +3 -2
- data/lib/active_support/core_ext/kernel/reporting.rb +2 -1
- data/lib/active_support/core_ext/kernel/requires.rb +2 -2
- data/lib/active_support/core_ext/kernel/singleton_class.rb +13 -0
- data/lib/active_support/core_ext/load_error.rb +17 -30
- data/lib/active_support/core_ext/logger.rb +2 -40
- data/lib/active_support/core_ext/module.rb +5 -3
- data/lib/active_support/core_ext/module/aliasing.rb +1 -1
- data/lib/active_support/core_ext/module/anonymous.rb +24 -0
- data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +1 -1
- data/lib/active_support/core_ext/module/attr_internal.rb +2 -2
- data/lib/active_support/core_ext/module/attribute_accessors.rb +20 -22
- data/lib/active_support/core_ext/module/delegation.rb +21 -10
- data/lib/active_support/core_ext/module/introspection.rb +8 -8
- data/lib/active_support/core_ext/module/method_names.rb +14 -0
- data/lib/active_support/core_ext/module/reachable.rb +10 -0
- data/lib/active_support/core_ext/module/remove_method.rb +11 -0
- data/lib/active_support/core_ext/module/synchronization.rb +2 -1
- data/lib/active_support/core_ext/name_error.rb +3 -1
- data/lib/active_support/core_ext/object.rb +5 -3
- data/lib/active_support/core_ext/object/blank.rb +20 -2
- data/lib/active_support/core_ext/object/conversions.rb +2 -16
- data/lib/active_support/core_ext/object/duplicable.rb +23 -1
- data/lib/active_support/core_ext/object/instance_variables.rb +0 -7
- data/lib/active_support/core_ext/object/to_json.rb +19 -0
- data/lib/active_support/core_ext/object/to_param.rb +49 -0
- data/lib/active_support/core_ext/object/to_query.rb +27 -0
- data/lib/active_support/core_ext/object/with_options.rb +2 -0
- data/lib/active_support/core_ext/proc.rb +4 -4
- data/lib/active_support/core_ext/range/overlaps.rb +1 -1
- data/lib/active_support/core_ext/regexp.rb +0 -22
- data/lib/active_support/core_ext/rexml.rb +4 -1
- data/lib/active_support/core_ext/string.rb +3 -2
- data/lib/active_support/core_ext/string/access.rb +4 -2
- data/lib/active_support/core_ext/string/conversions.rb +28 -1
- data/lib/active_support/core_ext/string/encoding.rb +11 -0
- data/lib/active_support/core_ext/string/exclude.rb +6 -0
- data/lib/active_support/core_ext/string/filters.rb +29 -0
- data/lib/active_support/core_ext/string/inflections.rb +12 -12
- data/lib/active_support/core_ext/string/interpolation.rb +2 -92
- data/lib/active_support/core_ext/string/multibyte.rb +16 -19
- data/lib/active_support/core_ext/string/output_safety.rb +101 -35
- data/lib/active_support/core_ext/string/starts_ends_with.rb +0 -14
- data/lib/active_support/core_ext/string/xchar.rb +1 -1
- data/lib/active_support/core_ext/time/calculations.rb +14 -7
- data/lib/active_support/core_ext/time/conversions.rb +1 -0
- data/lib/active_support/core_ext/time/marshal.rb +56 -0
- data/lib/active_support/core_ext/time/zones.rb +2 -5
- data/lib/active_support/core_ext/uri.rb +10 -4
- data/lib/active_support/dependencies.rb +200 -197
- data/lib/active_support/dependencies/autoload.rb +50 -0
- data/lib/active_support/deprecation/behaviors.rb +13 -9
- data/lib/active_support/deprecation/method_wrappers.rb +10 -9
- data/lib/active_support/deprecation/proxy_wrappers.rb +7 -0
- data/lib/active_support/deprecation/reporting.rb +5 -3
- data/lib/active_support/descendants_tracker.rb +43 -0
- data/lib/active_support/duration.rb +8 -4
- data/lib/active_support/file_update_checker.rb +36 -0
- data/lib/active_support/hash_with_indifferent_access.rb +9 -1
- data/lib/active_support/i18n.rb +9 -0
- data/lib/active_support/i18n_railtie.rb +81 -0
- data/lib/active_support/inflections.rb +1 -1
- data/lib/active_support/inflector.rb +4 -407
- data/lib/active_support/inflector/inflections.rb +211 -0
- data/lib/active_support/inflector/methods.rb +151 -0
- data/lib/active_support/inflector/transliterate.rb +97 -0
- data/lib/active_support/json/backends/jsongem.rb +12 -9
- data/lib/active_support/json/backends/yajl.rb +40 -0
- data/lib/active_support/json/backends/yaml.rb +1 -1
- data/lib/active_support/json/decoding.rb +17 -2
- data/lib/active_support/json/encoding.rb +48 -31
- data/lib/active_support/json/variable.rb +2 -4
- data/lib/active_support/lazy_load_hooks.rb +27 -0
- data/lib/active_support/locale/en.yml +5 -2
- data/lib/active_support/log_subscriber.rb +123 -0
- data/lib/active_support/log_subscriber/test_helper.rb +99 -0
- data/lib/active_support/memoizable.rb +1 -1
- data/lib/active_support/message_encryptor.rb +1 -0
- data/lib/active_support/message_verifier.rb +2 -1
- data/lib/active_support/multibyte.rb +8 -23
- data/lib/active_support/multibyte/chars.rb +213 -446
- data/lib/active_support/multibyte/unicode.rb +392 -0
- data/lib/active_support/multibyte/utils.rb +6 -7
- data/lib/active_support/notifications.rb +29 -122
- data/lib/active_support/notifications/fanout.rb +61 -0
- data/lib/active_support/notifications/instrumenter.rb +54 -0
- data/lib/active_support/ordered_hash.rb +59 -14
- data/lib/active_support/ordered_options.rb +6 -0
- data/lib/active_support/railtie.rb +60 -0
- data/lib/active_support/rescuable.rb +7 -4
- data/lib/active_support/ruby/shim.rb +4 -6
- data/lib/active_support/test_case.rb +2 -7
- data/lib/active_support/testing/assertions.rb +15 -0
- data/lib/active_support/testing/declarative.rb +1 -1
- data/lib/active_support/testing/isolation.rb +64 -17
- data/lib/active_support/testing/performance.rb +306 -335
- data/lib/active_support/testing/setup_and_teardown.rb +51 -29
- data/lib/active_support/time.rb +24 -3
- data/lib/active_support/time_with_zone.rb +10 -14
- data/lib/active_support/values/time_zone.rb +192 -234
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +3 -2
- data/lib/active_support/whiny_nil.rb +9 -7
- data/lib/active_support/xml_mini.rb +130 -1
- data/lib/active_support/xml_mini/jdom.rb +2 -0
- data/lib/active_support/xml_mini/libxml.rb +23 -86
- data/lib/active_support/xml_mini/libxmlsax.rb +85 -0
- data/lib/active_support/xml_mini/nokogiri.rb +27 -24
- data/lib/active_support/xml_mini/nokogirisax.rb +83 -0
- data/lib/active_support/xml_mini/rexml.rb +8 -2
- metadata +62 -195
- data/README +0 -43
- data/lib/active_support/autoload.rb +0 -28
- data/lib/active_support/core_ext/boolean.rb +0 -1
- data/lib/active_support/core_ext/boolean/conversions.rb +0 -11
- data/lib/active_support/core_ext/class/removal.rb +0 -53
- data/lib/active_support/core_ext/date.rb +0 -7
- data/lib/active_support/core_ext/date_time.rb +0 -5
- data/lib/active_support/core_ext/integer/even_odd.rb +0 -16
- data/lib/active_support/core_ext/kernel/daemonizing.rb +0 -7
- data/lib/active_support/core_ext/module/inclusion.rb +0 -30
- data/lib/active_support/core_ext/module/loading.rb +0 -23
- data/lib/active_support/core_ext/nil.rb +0 -1
- data/lib/active_support/core_ext/nil/conversions.rb +0 -5
- data/lib/active_support/core_ext/object/extending.rb +0 -80
- data/lib/active_support/core_ext/object/metaclass.rb +0 -13
- data/lib/active_support/core_ext/object/misc.rb +0 -3
- data/lib/active_support/core_ext/object/returning.rb +0 -42
- data/lib/active_support/core_ext/object/tap.rb +0 -16
- data/lib/active_support/core_ext/string/bytesize.rb +0 -5
- data/lib/active_support/core_ext/string/iterators.rb +0 -13
- data/lib/active_support/core_ext/symbol.rb +0 -1
- data/lib/active_support/core_ext/symbol/to_proc.rb +0 -14
- data/lib/active_support/core_ext/time.rb +0 -10
- data/lib/active_support/core_ext/time/marshal_with_utc_flag.rb +0 -22
- data/lib/active_support/deprecated_callbacks.rb +0 -283
- data/lib/active_support/multibyte/unicode_database.rb +0 -71
- data/lib/active_support/vendor.rb +0 -16
- data/lib/active_support/vendor/builder-2.1.2/lib/blankslate.rb +0 -113
- data/lib/active_support/vendor/builder-2.1.2/lib/builder.rb +0 -13
- data/lib/active_support/vendor/builder-2.1.2/lib/builder/blankslate.rb +0 -20
- data/lib/active_support/vendor/builder-2.1.2/lib/builder/css.rb +0 -250
- data/lib/active_support/vendor/builder-2.1.2/lib/builder/xchar.rb +0 -115
- data/lib/active_support/vendor/builder-2.1.2/lib/builder/xmlbase.rb +0 -139
- data/lib/active_support/vendor/builder-2.1.2/lib/builder/xmlevents.rb +0 -63
- data/lib/active_support/vendor/builder-2.1.2/lib/builder/xmlmarkup.rb +0 -328
- data/lib/active_support/vendor/i18n-0.1.3/MIT-LICENSE +0 -20
- data/lib/active_support/vendor/i18n-0.1.3/README.textile +0 -20
- data/lib/active_support/vendor/i18n-0.1.3/Rakefile +0 -5
- data/lib/active_support/vendor/i18n-0.1.3/i18n.gemspec +0 -27
- data/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb +0 -204
- data/lib/active_support/vendor/i18n-0.1.3/lib/i18n/backend/simple.rb +0 -215
- data/lib/active_support/vendor/i18n-0.1.3/lib/i18n/exceptions.rb +0 -53
- data/lib/active_support/vendor/i18n-0.1.3/test/all.rb +0 -5
- data/lib/active_support/vendor/i18n-0.1.3/test/i18n_exceptions_test.rb +0 -99
- data/lib/active_support/vendor/i18n-0.1.3/test/i18n_test.rb +0 -124
- data/lib/active_support/vendor/i18n-0.1.3/test/locale/en.rb +0 -1
- data/lib/active_support/vendor/i18n-0.1.3/test/locale/en.yml +0 -3
- data/lib/active_support/vendor/i18n-0.1.3/test/simple_backend_test.rb +0 -567
- data/lib/active_support/vendor/memcache-client-1.7.5/lib/memcache.rb +0 -1133
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo.rb +0 -33
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/data_timezone.rb +0 -47
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/data_timezone_info.rb +0 -228
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Africa/Algiers.rb +0 -55
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Africa/Cairo.rb +0 -219
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Africa/Casablanca.rb +0 -42
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Africa/Harare.rb +0 -18
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Africa/Johannesburg.rb +0 -25
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Africa/Monrovia.rb +0 -22
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Africa/Nairobi.rb +0 -23
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Argentina/Buenos_Aires.rb +0 -166
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Argentina/San_Juan.rb +0 -86
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Bogota.rb +0 -23
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Caracas.rb +0 -23
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Chicago.rb +0 -283
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Chihuahua.rb +0 -136
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Denver.rb +0 -204
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Godthab.rb +0 -161
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Guatemala.rb +0 -27
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Halifax.rb +0 -274
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Indiana/Indianapolis.rb +0 -149
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Juneau.rb +0 -194
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/La_Paz.rb +0 -22
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Lima.rb +0 -35
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Los_Angeles.rb +0 -232
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Mazatlan.rb +0 -139
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Mexico_City.rb +0 -144
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Monterrey.rb +0 -131
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/New_York.rb +0 -282
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Phoenix.rb +0 -30
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Regina.rb +0 -74
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Santiago.rb +0 -205
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Sao_Paulo.rb +0 -171
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/St_Johns.rb +0 -288
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/America/Tijuana.rb +0 -196
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Almaty.rb +0 -67
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Baghdad.rb +0 -73
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Baku.rb +0 -161
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Bangkok.rb +0 -20
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Chongqing.rb +0 -33
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Colombo.rb +0 -30
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Dhaka.rb +0 -27
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Hong_Kong.rb +0 -87
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Irkutsk.rb +0 -165
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Jakarta.rb +0 -30
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Jerusalem.rb +0 -163
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Kabul.rb +0 -20
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Kamchatka.rb +0 -163
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Karachi.rb +0 -32
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Kathmandu.rb +0 -20
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Kolkata.rb +0 -25
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Krasnoyarsk.rb +0 -163
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Kuala_Lumpur.rb +0 -31
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Kuwait.rb +0 -18
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Magadan.rb +0 -163
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Muscat.rb +0 -18
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Novosibirsk.rb +0 -164
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Rangoon.rb +0 -24
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Riyadh.rb +0 -18
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Seoul.rb +0 -34
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Shanghai.rb +0 -35
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Singapore.rb +0 -33
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Taipei.rb +0 -59
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Tashkent.rb +0 -47
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Tbilisi.rb +0 -78
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Tehran.rb +0 -121
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Tokyo.rb +0 -30
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Ulaanbaatar.rb +0 -65
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Urumqi.rb +0 -33
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Vladivostok.rb +0 -164
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Yakutsk.rb +0 -163
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Yekaterinburg.rb +0 -165
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Asia/Yerevan.rb +0 -165
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Atlantic/Azores.rb +0 -270
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Atlantic/Cape_Verde.rb +0 -23
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Atlantic/South_Georgia.rb +0 -18
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Australia/Adelaide.rb +0 -187
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Australia/Brisbane.rb +0 -35
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Australia/Darwin.rb +0 -29
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Australia/Hobart.rb +0 -193
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Australia/Melbourne.rb +0 -185
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Australia/Perth.rb +0 -37
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Australia/Sydney.rb +0 -185
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Etc/UTC.rb +0 -16
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Amsterdam.rb +0 -228
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Athens.rb +0 -185
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Belgrade.rb +0 -163
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Berlin.rb +0 -188
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Bratislava.rb +0 -13
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Brussels.rb +0 -232
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Bucharest.rb +0 -181
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Budapest.rb +0 -197
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Copenhagen.rb +0 -179
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Dublin.rb +0 -276
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Helsinki.rb +0 -163
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Istanbul.rb +0 -218
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Kiev.rb +0 -168
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Lisbon.rb +0 -268
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Ljubljana.rb +0 -13
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/London.rb +0 -288
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Madrid.rb +0 -211
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Minsk.rb +0 -170
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Moscow.rb +0 -181
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Paris.rb +0 -232
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Prague.rb +0 -187
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Riga.rb +0 -176
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Rome.rb +0 -215
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Sarajevo.rb +0 -13
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Skopje.rb +0 -13
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Sofia.rb +0 -173
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Stockholm.rb +0 -165
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Tallinn.rb +0 -172
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Vienna.rb +0 -183
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Vilnius.rb +0 -170
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Warsaw.rb +0 -212
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Europe/Zagreb.rb +0 -13
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Pacific/Auckland.rb +0 -202
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Pacific/Fiji.rb +0 -23
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Pacific/Guam.rb +0 -22
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Pacific/Honolulu.rb +0 -28
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Pacific/Majuro.rb +0 -20
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Pacific/Midway.rb +0 -25
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Pacific/Noumea.rb +0 -25
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Pacific/Pago_Pago.rb +0 -26
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Pacific/Port_Moresby.rb +0 -20
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/definitions/Pacific/Tongatapu.rb +0 -27
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/info_timezone.rb +0 -52
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/linked_timezone.rb +0 -51
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/linked_timezone_info.rb +0 -44
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/offset_rationals.rb +0 -98
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/ruby_core_support.rb +0 -56
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/time_or_datetime.rb +0 -292
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/timezone.rb +0 -508
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/timezone_definition.rb +0 -56
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/timezone_info.rb +0 -40
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/timezone_offset_info.rb +0 -94
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/timezone_period.rb +0 -198
- data/lib/active_support/vendor/tzinfo-0.3.13/lib/tzinfo/timezone_transition_info.rb +0 -129
@@ -0,0 +1,211 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
module Inflector
|
3
|
+
# A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
|
4
|
+
# inflection rules. Examples:
|
5
|
+
#
|
6
|
+
# ActiveSupport::Inflector.inflections do |inflect|
|
7
|
+
# inflect.plural /^(ox)$/i, '\1\2en'
|
8
|
+
# inflect.singular /^(ox)en/i, '\1'
|
9
|
+
#
|
10
|
+
# inflect.irregular 'octopus', 'octopi'
|
11
|
+
#
|
12
|
+
# inflect.uncountable "equipment"
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
|
16
|
+
# pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
|
17
|
+
# already have been loaded.
|
18
|
+
class Inflections
|
19
|
+
def self.instance
|
20
|
+
@__instance__ ||= new
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :plurals, :singulars, :uncountables, :humans
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
@plurals, @singulars, @uncountables, @humans = [], [], [], []
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
|
30
|
+
# The replacement should always be a string that may include references to the matched data from the rule.
|
31
|
+
def plural(rule, replacement)
|
32
|
+
@uncountables.delete(rule) if rule.is_a?(String)
|
33
|
+
@uncountables.delete(replacement)
|
34
|
+
@plurals.insert(0, [rule, replacement])
|
35
|
+
end
|
36
|
+
|
37
|
+
# Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
|
38
|
+
# The replacement should always be a string that may include references to the matched data from the rule.
|
39
|
+
def singular(rule, replacement)
|
40
|
+
@uncountables.delete(rule) if rule.is_a?(String)
|
41
|
+
@uncountables.delete(replacement)
|
42
|
+
@singulars.insert(0, [rule, replacement])
|
43
|
+
end
|
44
|
+
|
45
|
+
# Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
|
46
|
+
# for strings, not regular expressions. You simply pass the irregular in singular and plural form.
|
47
|
+
#
|
48
|
+
# Examples:
|
49
|
+
# irregular 'octopus', 'octopi'
|
50
|
+
# irregular 'person', 'people'
|
51
|
+
def irregular(singular, plural)
|
52
|
+
@uncountables.delete(singular)
|
53
|
+
@uncountables.delete(plural)
|
54
|
+
if singular[0,1].upcase == plural[0,1].upcase
|
55
|
+
plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
|
56
|
+
plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1])
|
57
|
+
singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
|
58
|
+
else
|
59
|
+
plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
|
60
|
+
plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
|
61
|
+
plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
|
62
|
+
plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
|
63
|
+
singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
|
64
|
+
singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Add uncountable words that shouldn't be attempted inflected.
|
69
|
+
#
|
70
|
+
# Examples:
|
71
|
+
# uncountable "money"
|
72
|
+
# uncountable "money", "information"
|
73
|
+
# uncountable %w( money information rice )
|
74
|
+
def uncountable(*words)
|
75
|
+
(@uncountables << words).flatten!
|
76
|
+
end
|
77
|
+
|
78
|
+
# Specifies a humanized form of a string by a regular expression rule or by a string mapping.
|
79
|
+
# When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
|
80
|
+
# When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
|
81
|
+
#
|
82
|
+
# Examples:
|
83
|
+
# human /_cnt$/i, '\1_count'
|
84
|
+
# human "legacy_col_person_name", "Name"
|
85
|
+
def human(rule, replacement)
|
86
|
+
@humans.insert(0, [rule, replacement])
|
87
|
+
end
|
88
|
+
|
89
|
+
# Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
|
90
|
+
# Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
|
91
|
+
# <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
|
92
|
+
#
|
93
|
+
# Examples:
|
94
|
+
# clear :all
|
95
|
+
# clear :plurals
|
96
|
+
def clear(scope = :all)
|
97
|
+
case scope
|
98
|
+
when :all
|
99
|
+
@plurals, @singulars, @uncountables = [], [], []
|
100
|
+
else
|
101
|
+
instance_variable_set "@#{scope}", []
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# Yields a singleton instance of Inflector::Inflections so you can specify additional
|
107
|
+
# inflector rules.
|
108
|
+
#
|
109
|
+
# Example:
|
110
|
+
# ActiveSupport::Inflector.inflections do |inflect|
|
111
|
+
# inflect.uncountable "rails"
|
112
|
+
# end
|
113
|
+
def inflections
|
114
|
+
if block_given?
|
115
|
+
yield Inflections.instance
|
116
|
+
else
|
117
|
+
Inflections.instance
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns the plural form of the word in the string.
|
122
|
+
#
|
123
|
+
# Examples:
|
124
|
+
# "post".pluralize # => "posts"
|
125
|
+
# "octopus".pluralize # => "octopi"
|
126
|
+
# "sheep".pluralize # => "sheep"
|
127
|
+
# "words".pluralize # => "words"
|
128
|
+
# "CamelOctopus".pluralize # => "CamelOctopi"
|
129
|
+
def pluralize(word)
|
130
|
+
result = word.to_s.dup
|
131
|
+
|
132
|
+
if word.empty? || inflections.uncountables.include?(result.downcase)
|
133
|
+
result
|
134
|
+
else
|
135
|
+
inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
136
|
+
result
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# The reverse of +pluralize+, returns the singular form of a word in a string.
|
141
|
+
#
|
142
|
+
# Examples:
|
143
|
+
# "posts".singularize # => "post"
|
144
|
+
# "octopi".singularize # => "octopus"
|
145
|
+
# "sheep".singularize # => "sheep"
|
146
|
+
# "word".singularize # => "word"
|
147
|
+
# "CamelOctopi".singularize # => "CamelOctopus"
|
148
|
+
def singularize(word)
|
149
|
+
result = word.to_s.dup
|
150
|
+
|
151
|
+
if inflections.uncountables.any? { |inflection| result =~ /#{inflection}\Z/i }
|
152
|
+
result
|
153
|
+
else
|
154
|
+
inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
155
|
+
result
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Capitalizes the first word and turns underscores into spaces and strips a
|
160
|
+
# trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
|
161
|
+
#
|
162
|
+
# Examples:
|
163
|
+
# "employee_salary" # => "Employee salary"
|
164
|
+
# "author_id" # => "Author"
|
165
|
+
def humanize(lower_case_and_underscored_word)
|
166
|
+
result = lower_case_and_underscored_word.to_s.dup
|
167
|
+
|
168
|
+
inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
169
|
+
result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
|
170
|
+
end
|
171
|
+
|
172
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
173
|
+
# a nicer looking title. +titleize+ is meant for creating pretty output. It is not
|
174
|
+
# used in the Rails internals.
|
175
|
+
#
|
176
|
+
# +titleize+ is also aliased as as +titlecase+.
|
177
|
+
#
|
178
|
+
# Examples:
|
179
|
+
# "man from the boondocks".titleize # => "Man From The Boondocks"
|
180
|
+
# "x-men: the last stand".titleize # => "X Men: The Last Stand"
|
181
|
+
def titleize(word)
|
182
|
+
humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize }
|
183
|
+
end
|
184
|
+
|
185
|
+
# Create the name of a table like Rails does for models to table names. This method
|
186
|
+
# uses the +pluralize+ method on the last word in the string.
|
187
|
+
#
|
188
|
+
# Examples
|
189
|
+
# "RawScaledScorer".tableize # => "raw_scaled_scorers"
|
190
|
+
# "egg_and_ham".tableize # => "egg_and_hams"
|
191
|
+
# "fancyCategory".tableize # => "fancy_categories"
|
192
|
+
def tableize(class_name)
|
193
|
+
pluralize(underscore(class_name))
|
194
|
+
end
|
195
|
+
|
196
|
+
# Create a class name from a plural table name like Rails does for table names to models.
|
197
|
+
# Note that this returns a string and not a Class. (To convert to an actual class
|
198
|
+
# follow +classify+ with +constantize+.)
|
199
|
+
#
|
200
|
+
# Examples:
|
201
|
+
# "egg_and_hams".classify # => "EggAndHam"
|
202
|
+
# "posts".classify # => "Post"
|
203
|
+
#
|
204
|
+
# Singular names are not handled correctly:
|
205
|
+
# "business".classify # => "Busines"
|
206
|
+
def classify(table_name)
|
207
|
+
# strip out any leading schema name
|
208
|
+
camelize(singularize(table_name.to_s.sub(/.*\./, '')))
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
# The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
|
3
|
+
# and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
|
4
|
+
# in inflections.rb.
|
5
|
+
#
|
6
|
+
# The Rails core team has stated patches for the inflections library will not be accepted
|
7
|
+
# in order to avoid breaking legacy applications which may be relying on errant inflections.
|
8
|
+
# If you discover an incorrect inflection and require it for your application, you'll need
|
9
|
+
# to correct it yourself (explained below).
|
10
|
+
module Inflector
|
11
|
+
extend self
|
12
|
+
|
13
|
+
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
|
14
|
+
# is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
|
15
|
+
#
|
16
|
+
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
|
17
|
+
#
|
18
|
+
# Examples:
|
19
|
+
# "active_record".camelize # => "ActiveRecord"
|
20
|
+
# "active_record".camelize(:lower) # => "activeRecord"
|
21
|
+
# "active_record/errors".camelize # => "ActiveRecord::Errors"
|
22
|
+
# "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
|
23
|
+
#
|
24
|
+
# As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
|
25
|
+
# though there are cases where that does not hold:
|
26
|
+
#
|
27
|
+
# "SSLError".underscore.camelize # => "SslError"
|
28
|
+
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
29
|
+
if first_letter_in_uppercase
|
30
|
+
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
31
|
+
else
|
32
|
+
lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Makes an underscored, lowercase form from the expression in the string.
|
37
|
+
#
|
38
|
+
# Changes '::' to '/' to convert namespaces to paths.
|
39
|
+
#
|
40
|
+
# Examples:
|
41
|
+
# "ActiveRecord".underscore # => "active_record"
|
42
|
+
# "ActiveRecord::Errors".underscore # => active_record/errors
|
43
|
+
#
|
44
|
+
# As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
|
45
|
+
# though there are cases where that does not hold:
|
46
|
+
#
|
47
|
+
# "SSLError".underscore.camelize # => "SslError"
|
48
|
+
def underscore(camel_cased_word)
|
49
|
+
word = camel_cased_word.to_s.dup
|
50
|
+
word.gsub!(/::/, '/')
|
51
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
52
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
53
|
+
word.tr!("-", "_")
|
54
|
+
word.downcase!
|
55
|
+
word
|
56
|
+
end
|
57
|
+
|
58
|
+
# Replaces underscores with dashes in the string.
|
59
|
+
#
|
60
|
+
# Example:
|
61
|
+
# "puni_puni" # => "puni-puni"
|
62
|
+
def dasherize(underscored_word)
|
63
|
+
underscored_word.gsub(/_/, '-')
|
64
|
+
end
|
65
|
+
|
66
|
+
# Removes the module part from the expression in the string.
|
67
|
+
#
|
68
|
+
# Examples:
|
69
|
+
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
|
70
|
+
# "Inflections".demodulize # => "Inflections"
|
71
|
+
def demodulize(class_name_in_module)
|
72
|
+
class_name_in_module.to_s.gsub(/^.*::/, '')
|
73
|
+
end
|
74
|
+
|
75
|
+
# Creates a foreign key name from a class name.
|
76
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
77
|
+
# the method should put '_' between the name and 'id'.
|
78
|
+
#
|
79
|
+
# Examples:
|
80
|
+
# "Message".foreign_key # => "message_id"
|
81
|
+
# "Message".foreign_key(false) # => "messageid"
|
82
|
+
# "Admin::Post".foreign_key # => "post_id"
|
83
|
+
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
|
84
|
+
underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
|
85
|
+
end
|
86
|
+
|
87
|
+
# Ruby 1.9 introduces an inherit argument for Module#const_get and
|
88
|
+
# #const_defined? and changes their default behavior.
|
89
|
+
if Module.method(:const_get).arity == 1
|
90
|
+
# Tries to find a constant with the name specified in the argument string:
|
91
|
+
#
|
92
|
+
# "Module".constantize # => Module
|
93
|
+
# "Test::Unit".constantize # => Test::Unit
|
94
|
+
#
|
95
|
+
# The name is assumed to be the one of a top-level constant, no matter whether
|
96
|
+
# it starts with "::" or not. No lexical context is taken into account:
|
97
|
+
#
|
98
|
+
# C = 'outside'
|
99
|
+
# module M
|
100
|
+
# C = 'inside'
|
101
|
+
# C # => 'inside'
|
102
|
+
# "C".constantize # => 'outside', same as ::C
|
103
|
+
# end
|
104
|
+
#
|
105
|
+
# NameError is raised when the name is not in CamelCase or the constant is
|
106
|
+
# unknown.
|
107
|
+
def constantize(camel_cased_word)
|
108
|
+
names = camel_cased_word.split('::')
|
109
|
+
names.shift if names.empty? || names.first.empty?
|
110
|
+
|
111
|
+
constant = Object
|
112
|
+
names.each do |name|
|
113
|
+
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
|
114
|
+
end
|
115
|
+
constant
|
116
|
+
end
|
117
|
+
else
|
118
|
+
def constantize(camel_cased_word) #:nodoc:
|
119
|
+
names = camel_cased_word.split('::')
|
120
|
+
names.shift if names.empty? || names.first.empty?
|
121
|
+
|
122
|
+
constant = Object
|
123
|
+
names.each do |name|
|
124
|
+
constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
|
125
|
+
end
|
126
|
+
constant
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# Turns a number into an ordinal string used to denote the position in an
|
131
|
+
# ordered sequence such as 1st, 2nd, 3rd, 4th.
|
132
|
+
#
|
133
|
+
# Examples:
|
134
|
+
# ordinalize(1) # => "1st"
|
135
|
+
# ordinalize(2) # => "2nd"
|
136
|
+
# ordinalize(1002) # => "1002nd"
|
137
|
+
# ordinalize(1003) # => "1003rd"
|
138
|
+
def ordinalize(number)
|
139
|
+
if (11..13).include?(number.to_i % 100)
|
140
|
+
"#{number}th"
|
141
|
+
else
|
142
|
+
case number.to_i % 10
|
143
|
+
when 1; "#{number}st"
|
144
|
+
when 2; "#{number}nd"
|
145
|
+
when 3; "#{number}rd"
|
146
|
+
else "#{number}th"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'active_support/core_ext/string/multibyte'
|
3
|
+
|
4
|
+
module ActiveSupport
|
5
|
+
module Inflector
|
6
|
+
|
7
|
+
# Replaces non-ASCII characters with an ASCII approximation, or if none
|
8
|
+
# exists, a replacement character which defaults to "?".
|
9
|
+
#
|
10
|
+
# transliterate("Ærøskøbing")
|
11
|
+
# # => "AEroskobing"
|
12
|
+
#
|
13
|
+
# Default approximations are provided for Western/Latin characters,
|
14
|
+
# e.g, "ø", "ñ", "é", "ß", etc.
|
15
|
+
#
|
16
|
+
# This method is I18n aware, so you can set up custom approximations for a
|
17
|
+
# locale. This can be useful, for example, to transliterate German's "ü"
|
18
|
+
# and "ö" to "ue" and "oe", or to add support for transliterating Russian
|
19
|
+
# to ASCII.
|
20
|
+
#
|
21
|
+
# In order to make your custom transliterations available, you must set
|
22
|
+
# them as the <tt>i18n.transliterate.rule</tt> i18n key:
|
23
|
+
#
|
24
|
+
# # Store the transliterations in locales/de.yml
|
25
|
+
# i18n:
|
26
|
+
# transliterate:
|
27
|
+
# rule:
|
28
|
+
# ü: "ue"
|
29
|
+
# ö: "oe"
|
30
|
+
#
|
31
|
+
# # Or set them using Ruby
|
32
|
+
# I18n.backend.store_translations(:de, :i18n => {
|
33
|
+
# :transliterate => {
|
34
|
+
# :rule => {
|
35
|
+
# "ü" => "ue",
|
36
|
+
# "ö" => "oe"
|
37
|
+
# }
|
38
|
+
# }
|
39
|
+
# })
|
40
|
+
#
|
41
|
+
# The value for <tt>i18n.transliterate.rule</tt> can be a simple Hash that maps
|
42
|
+
# characters to ASCII approximations as shown above, or, for more complex
|
43
|
+
# requirements, a Proc:
|
44
|
+
#
|
45
|
+
# I18n.backend.store_translations(:de, :i18n => {
|
46
|
+
# :transliterate => {
|
47
|
+
# :rule => lambda {|string| MyTransliterator.transliterate(string)}
|
48
|
+
# }
|
49
|
+
# })
|
50
|
+
#
|
51
|
+
# Now you can have different transliterations for each locale:
|
52
|
+
#
|
53
|
+
# I18n.locale = :en
|
54
|
+
# transliterate("Jürgen")
|
55
|
+
# # => "Jurgen"
|
56
|
+
#
|
57
|
+
# I18n.locale = :de
|
58
|
+
# transliterate("Jürgen")
|
59
|
+
# # => "Juergen"
|
60
|
+
def transliterate(string, replacement = "?")
|
61
|
+
I18n.transliterate(ActiveSupport::Multibyte::Unicode.normalize(
|
62
|
+
ActiveSupport::Multibyte::Unicode.tidy_bytes(string), :c),
|
63
|
+
:replacement => replacement)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
|
67
|
+
#
|
68
|
+
# ==== Examples
|
69
|
+
#
|
70
|
+
# class Person
|
71
|
+
# def to_param
|
72
|
+
# "#{id}-#{name.parameterize}"
|
73
|
+
# end
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# @person = Person.find(1)
|
77
|
+
# # => #<Person id: 1, name: "Donald E. Knuth">
|
78
|
+
#
|
79
|
+
# <%= link_to(@person.name, person_path(@person)) %>
|
80
|
+
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
|
81
|
+
def parameterize(string, sep = '-')
|
82
|
+
# replace accented chars with their ascii equivalents
|
83
|
+
parameterized_string = transliterate(string)
|
84
|
+
# Turn unwanted chars into the separator
|
85
|
+
parameterized_string.gsub!(/[^a-z0-9\-_]+/i, sep)
|
86
|
+
unless sep.nil? || sep.empty?
|
87
|
+
re_sep = Regexp.escape(sep)
|
88
|
+
# No more than one of the separator in a row.
|
89
|
+
parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
|
90
|
+
# Remove leading/trailing separator.
|
91
|
+
parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
|
92
|
+
end
|
93
|
+
parameterized_string.downcase
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|