activesupport 1.2.4 → 8.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +505 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +40 -0
- data/lib/active_support/actionable_error.rb +50 -0
- data/lib/active_support/all.rb +5 -0
- data/lib/active_support/array_inquirer.rb +50 -0
- data/lib/active_support/backtrace_cleaner.rb +234 -0
- data/lib/active_support/benchmark.rb +21 -0
- data/lib/active_support/benchmarkable.rb +53 -0
- data/lib/active_support/broadcast_logger.rb +238 -0
- data/lib/active_support/builder.rb +8 -0
- data/lib/active_support/cache/coder.rb +153 -0
- data/lib/active_support/cache/entry.rb +134 -0
- data/lib/active_support/cache/file_store.rb +244 -0
- data/lib/active_support/cache/mem_cache_store.rb +288 -0
- data/lib/active_support/cache/memory_store.rb +264 -0
- data/lib/active_support/cache/null_store.rb +62 -0
- data/lib/active_support/cache/redis_cache_store.rb +498 -0
- data/lib/active_support/cache/serializer_with_fallback.rb +152 -0
- data/lib/active_support/cache/strategy/local_cache.rb +246 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
- data/lib/active_support/cache.rb +1170 -0
- data/lib/active_support/callbacks.rb +960 -0
- data/lib/active_support/class_attribute.rb +33 -0
- data/lib/active_support/code_generator.rb +79 -0
- data/lib/active_support/concern.rb +217 -0
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +18 -0
- data/lib/active_support/concurrency/null_lock.rb +13 -0
- data/lib/active_support/concurrency/share_lock.rb +225 -0
- data/lib/active_support/concurrency/thread_monitor.rb +55 -0
- data/lib/active_support/configurable.rb +193 -0
- data/lib/active_support/configuration_file.rb +60 -0
- data/lib/active_support/continuous_integration.rb +145 -0
- data/lib/active_support/core_ext/array/access.rb +100 -0
- data/lib/active_support/core_ext/array/conversions.rb +209 -26
- data/lib/active_support/core_ext/array/extract.rb +21 -0
- data/lib/active_support/core_ext/array/extract_options.rb +31 -0
- data/lib/active_support/core_ext/array/grouping.rb +109 -0
- data/lib/active_support/core_ext/array/inquiry.rb +19 -0
- data/lib/active_support/core_ext/array/wrap.rb +48 -0
- data/lib/active_support/core_ext/array.rb +8 -4
- data/lib/active_support/core_ext/benchmark.rb +6 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/big_decimal.rb +3 -0
- data/lib/active_support/core_ext/class/attribute.rb +137 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
- data/lib/active_support/core_ext/class/subclasses.rb +24 -0
- data/lib/active_support/core_ext/class.rb +4 -0
- data/lib/active_support/core_ext/date/acts_like.rb +10 -0
- data/lib/active_support/core_ext/date/blank.rb +18 -0
- data/lib/active_support/core_ext/date/calculations.rb +161 -0
- data/lib/active_support/core_ext/date/conversions.rb +95 -28
- data/lib/active_support/core_ext/date/zones.rb +8 -0
- data/lib/active_support/core_ext/date.rb +6 -5
- data/lib/active_support/core_ext/date_and_time/calculations.rb +374 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +23 -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 +16 -0
- data/lib/active_support/core_ext/date_time/blank.rb +18 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +215 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +108 -0
- data/lib/active_support/core_ext/date_time.rb +7 -0
- data/lib/active_support/core_ext/digest/uuid.rb +76 -0
- data/lib/active_support/core_ext/digest.rb +3 -0
- data/lib/active_support/core_ext/enumerable.rb +277 -7
- data/lib/active_support/core_ext/erb/util.rb +201 -0
- data/lib/active_support/core_ext/file/atomic.rb +72 -0
- data/lib/active_support/core_ext/file.rb +3 -0
- data/lib/active_support/core_ext/hash/conversions.rb +262 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +43 -0
- data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
- data/lib/active_support/core_ext/hash/except.rb +12 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +19 -55
- data/lib/active_support/core_ext/hash/keys.rb +134 -44
- data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -22
- data/lib/active_support/core_ext/hash/slice.rb +27 -0
- data/lib/active_support/core_ext/hash.rb +9 -8
- data/lib/active_support/core_ext/integer/inflections.rb +29 -13
- data/lib/active_support/core_ext/integer/multiple.rb +12 -0
- data/lib/active_support/core_ext/integer/time.rb +22 -0
- data/lib/active_support/core_ext/integer.rb +4 -6
- data/lib/active_support/core_ext/kernel/concern.rb +14 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +45 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +8 -0
- data/lib/active_support/core_ext/kernel.rb +4 -78
- data/lib/active_support/core_ext/load_error.rb +6 -35
- data/lib/active_support/core_ext/module/aliasing.rb +31 -0
- data/lib/active_support/core_ext/module/anonymous.rb +30 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +48 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +214 -0
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +175 -0
- data/lib/active_support/core_ext/module/concerning.rb +140 -0
- data/lib/active_support/core_ext/module/delegation.rb +225 -0
- data/lib/active_support/core_ext/module/deprecation.rb +25 -0
- data/lib/active_support/core_ext/module/introspection.rb +65 -0
- data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
- data/lib/active_support/core_ext/module/remove_method.rb +17 -0
- data/lib/active_support/core_ext/module.rb +13 -0
- data/lib/active_support/core_ext/name_error.rb +59 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +73 -42
- data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
- data/lib/active_support/core_ext/numeric/time.rb +64 -57
- data/lib/active_support/core_ext/numeric.rb +4 -6
- data/lib/active_support/core_ext/object/acts_like.rb +45 -0
- data/lib/active_support/core_ext/object/blank.rb +199 -0
- data/lib/active_support/core_ext/object/conversions.rb +6 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +71 -0
- data/lib/active_support/core_ext/object/duplicable.rb +69 -0
- data/lib/active_support/core_ext/object/inclusion.rb +37 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +32 -0
- data/lib/active_support/core_ext/object/json.rb +267 -0
- data/lib/active_support/core_ext/object/to_param.rb +3 -0
- data/lib/active_support/core_ext/object/to_query.rb +93 -0
- data/lib/active_support/core_ext/object/try.rb +158 -0
- data/lib/active_support/core_ext/object/with.rb +46 -0
- data/lib/active_support/core_ext/object/with_options.rb +101 -0
- data/lib/active_support/core_ext/object.rb +17 -0
- data/lib/active_support/core_ext/pathname/blank.rb +20 -0
- data/lib/active_support/core_ext/pathname/existence.rb +23 -0
- data/lib/active_support/core_ext/pathname.rb +4 -0
- data/lib/active_support/core_ext/range/compare_range.rb +57 -0
- data/lib/active_support/core_ext/range/conversions.rb +58 -17
- data/lib/active_support/core_ext/range/overlap.rb +40 -0
- data/lib/active_support/core_ext/range/sole.rb +17 -0
- data/lib/active_support/core_ext/range.rb +5 -4
- data/lib/active_support/core_ext/regexp.rb +14 -0
- data/lib/active_support/core_ext/securerandom.rb +57 -0
- data/lib/active_support/core_ext/string/access.rb +93 -56
- data/lib/active_support/core_ext/string/behavior.rb +8 -0
- data/lib/active_support/core_ext/string/conversions.rb +57 -16
- data/lib/active_support/core_ext/string/exclude.rb +13 -0
- data/lib/active_support/core_ext/string/filters.rb +151 -0
- data/lib/active_support/core_ext/string/indent.rb +45 -0
- data/lib/active_support/core_ext/string/inflections.rb +297 -54
- data/lib/active_support/core_ext/string/inquiry.rb +16 -0
- data/lib/active_support/core_ext/string/multibyte.rb +67 -0
- data/lib/active_support/core_ext/string/output_safety.rb +235 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -18
- data/lib/active_support/core_ext/string/strip.rb +27 -0
- data/lib/active_support/core_ext/string/zones.rb +16 -0
- data/lib/active_support/core_ext/string.rb +14 -10
- data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
- data/lib/active_support/core_ext/symbol.rb +3 -0
- data/lib/active_support/core_ext/thread/backtrace/location.rb +7 -0
- data/lib/active_support/core_ext/time/acts_like.rb +10 -0
- data/lib/active_support/core_ext/time/calculations.rb +358 -153
- data/lib/active_support/core_ext/time/compatibility.rb +15 -0
- data/lib/active_support/core_ext/time/conversions.rb +69 -30
- data/lib/active_support/core_ext/time/zones.rb +97 -0
- data/lib/active_support/core_ext/time.rb +6 -6
- data/lib/active_support/core_ext.rb +5 -1
- data/lib/active_support/current_attributes/test_helper.rb +13 -0
- data/lib/active_support/current_attributes.rb +243 -0
- data/lib/active_support/deep_mergeable.rb +53 -0
- data/lib/active_support/delegation.rb +183 -0
- data/lib/active_support/dependencies/autoload.rb +72 -0
- data/lib/active_support/dependencies/interlock.rb +55 -0
- data/lib/active_support/dependencies/require_dependency.rb +28 -0
- data/lib/active_support/dependencies.rb +84 -222
- data/lib/active_support/deprecation/behaviors.rb +148 -0
- data/lib/active_support/deprecation/constant_accessor.rb +74 -0
- data/lib/active_support/deprecation/deprecators.rb +104 -0
- data/lib/active_support/deprecation/disallowed.rb +54 -0
- data/lib/active_support/deprecation/method_wrappers.rb +68 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +189 -0
- data/lib/active_support/deprecation/reporting.rb +162 -0
- data/lib/active_support/deprecation.rb +81 -0
- data/lib/active_support/deprecator.rb +7 -0
- data/lib/active_support/descendants_tracker.rb +112 -0
- data/lib/active_support/digest.rb +22 -0
- data/lib/active_support/duration/iso8601_parser.rb +123 -0
- data/lib/active_support/duration/iso8601_serializer.rb +64 -0
- data/lib/active_support/duration.rb +524 -0
- data/lib/active_support/editor.rb +70 -0
- data/lib/active_support/encrypted_configuration.rb +126 -0
- data/lib/active_support/encrypted_file.rb +133 -0
- data/lib/active_support/environment_inquirer.rb +40 -0
- data/lib/active_support/error_reporter/test_helper.rb +15 -0
- data/lib/active_support/error_reporter.rb +318 -0
- data/lib/active_support/event_reporter/test_helper.rb +32 -0
- data/lib/active_support/event_reporter.rb +592 -0
- data/lib/active_support/evented_file_update_checker.rb +185 -0
- data/lib/active_support/execution_context/test_helper.rb +13 -0
- data/lib/active_support/execution_context.rb +110 -0
- data/lib/active_support/execution_wrapper.rb +150 -0
- data/lib/active_support/executor/test_helper.rb +7 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +166 -0
- data/lib/active_support/fork_tracker.rb +43 -0
- data/lib/active_support/gem_version.rb +17 -0
- data/lib/active_support/gzip.rb +41 -0
- data/lib/active_support/hash_with_indifferent_access.rb +464 -0
- data/lib/active_support/html_safe_translation.rb +56 -0
- data/lib/active_support/i18n.rb +17 -0
- data/lib/active_support/i18n_railtie.rb +140 -0
- data/lib/active_support/inflections.rb +68 -49
- data/lib/active_support/inflector/inflections.rb +290 -0
- data/lib/active_support/inflector/methods.rb +387 -0
- data/lib/active_support/inflector/transliterate.rb +147 -0
- data/lib/active_support/inflector.rb +7 -164
- data/lib/active_support/isolated_execution_state.rb +76 -0
- data/lib/active_support/json/decoding.rb +78 -0
- data/lib/active_support/json/encoding.rb +256 -0
- data/lib/active_support/json.rb +4 -0
- data/lib/active_support/key_generator.rb +66 -0
- data/lib/active_support/lazy_load_hooks.rb +107 -0
- data/lib/active_support/locale/en.rb +33 -0
- data/lib/active_support/locale/en.yml +141 -0
- data/lib/active_support/log_subscriber/test_helper.rb +106 -0
- data/lib/active_support/log_subscriber.rb +188 -0
- data/lib/active_support/logger.rb +55 -0
- data/lib/active_support/logger_silence.rb +21 -0
- data/lib/active_support/logger_thread_safe_level.rb +50 -0
- data/lib/active_support/message_encryptor.rb +374 -0
- data/lib/active_support/message_encryptors.rb +193 -0
- data/lib/active_support/message_pack/cache_serializer.rb +23 -0
- data/lib/active_support/message_pack/extensions.rb +310 -0
- data/lib/active_support/message_pack/serializer.rb +63 -0
- data/lib/active_support/message_pack.rb +50 -0
- data/lib/active_support/message_verifier.rb +377 -0
- data/lib/active_support/message_verifiers.rb +189 -0
- data/lib/active_support/messages/codec.rb +65 -0
- data/lib/active_support/messages/metadata.rb +146 -0
- data/lib/active_support/messages/rotation_configuration.rb +23 -0
- data/lib/active_support/messages/rotation_coordinator.rb +102 -0
- data/lib/active_support/messages/rotator.rb +69 -0
- data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
- data/lib/active_support/multibyte/chars.rb +188 -0
- data/lib/active_support/multibyte/unicode.rb +42 -0
- data/lib/active_support/multibyte.rb +27 -0
- data/lib/active_support/notifications/fanout.rb +467 -0
- data/lib/active_support/notifications/instrumenter.rb +240 -0
- data/lib/active_support/notifications.rb +281 -0
- data/lib/active_support/number_helper/number_converter.rb +190 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +30 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +60 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +59 -0
- data/lib/active_support/number_helper/rounding_helper.rb +46 -0
- data/lib/active_support/number_helper.rb +479 -0
- data/lib/active_support/option_merger.rb +38 -0
- data/lib/active_support/ordered_hash.rb +50 -0
- data/lib/active_support/ordered_options.rb +141 -25
- data/lib/active_support/parameter_filter.rb +157 -0
- data/lib/active_support/rails.rb +26 -0
- data/lib/active_support/railtie.rb +180 -0
- data/lib/active_support/reloader.rb +138 -0
- data/lib/active_support/rescuable.rb +176 -0
- data/lib/active_support/secure_compare_rotator.rb +58 -0
- data/lib/active_support/security_utils.rb +38 -0
- data/lib/active_support/string_inquirer.rb +35 -0
- data/lib/active_support/structured_event_subscriber.rb +99 -0
- data/lib/active_support/subscriber.rb +141 -0
- data/lib/active_support/syntax_error_proxy.rb +67 -0
- data/lib/active_support/tagged_logging.rb +157 -0
- data/lib/active_support/test_case.rb +365 -0
- data/lib/active_support/testing/assertions.rb +369 -0
- data/lib/active_support/testing/autorun.rb +10 -0
- data/lib/active_support/testing/constant_lookup.rb +51 -0
- data/lib/active_support/testing/constant_stubbing.rb +54 -0
- data/lib/active_support/testing/declarative.rb +28 -0
- data/lib/active_support/testing/deprecation.rb +82 -0
- data/lib/active_support/testing/error_reporter_assertions.rb +124 -0
- data/lib/active_support/testing/event_reporter_assertions.rb +227 -0
- data/lib/active_support/testing/file_fixtures.rb +38 -0
- data/lib/active_support/testing/isolation.rb +121 -0
- data/lib/active_support/testing/method_call_assertions.rb +69 -0
- data/lib/active_support/testing/notification_assertions.rb +92 -0
- data/lib/active_support/testing/parallelization/server.rb +98 -0
- data/lib/active_support/testing/parallelization/worker.rb +107 -0
- data/lib/active_support/testing/parallelization.rb +79 -0
- data/lib/active_support/testing/parallelize_executor.rb +81 -0
- data/lib/active_support/testing/setup_and_teardown.rb +57 -0
- data/lib/active_support/testing/stream.rb +41 -0
- data/lib/active_support/testing/tagged_logging.rb +27 -0
- data/lib/active_support/testing/tests_without_assertions.rb +19 -0
- data/lib/active_support/testing/time_helpers.rb +273 -0
- data/lib/active_support/time.rb +20 -0
- data/lib/active_support/time_with_zone.rb +613 -0
- data/lib/active_support/values/time_zone.rb +599 -158
- data/lib/active_support/version.rb +7 -6
- data/lib/active_support/xml_mini/jdom.rb +175 -0
- data/lib/active_support/xml_mini/libxml.rb +80 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +83 -0
- data/lib/active_support/xml_mini/nokogiri.rb +83 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +86 -0
- data/lib/active_support/xml_mini/rexml.rb +137 -0
- data/lib/active_support/xml_mini.rb +212 -0
- data/lib/active_support.rb +122 -10
- metadata +524 -93
- data/CHANGELOG +0 -283
- data/lib/active_support/binding_of_caller.rb +0 -84
- data/lib/active_support/breakpoint.rb +0 -523
- data/lib/active_support/class_attribute_accessors.rb +0 -57
- data/lib/active_support/class_inheritable_attributes.rb +0 -117
- data/lib/active_support/clean_logger.rb +0 -36
- data/lib/active_support/core_ext/blank.rb +0 -38
- data/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +0 -14
- data/lib/active_support/core_ext/cgi.rb +0 -5
- data/lib/active_support/core_ext/exception.rb +0 -29
- data/lib/active_support/core_ext/integer/even_odd.rb +0 -24
- data/lib/active_support/core_ext/object_and_class.rb +0 -44
- data/lib/active_support/module_attribute_accessors.rb +0 -57
- data/lib/active_support/whiny_nil.rb +0 -38
|
@@ -1,19 +1,60 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
module String #:nodoc:
|
|
6
|
-
# Converting strings to other objects
|
|
7
|
-
module Conversions
|
|
8
|
-
# Form can be either :utc (default) or :local.
|
|
9
|
-
def to_time(form = :utc)
|
|
10
|
-
::Time.send(form, *ParseDate.parsedate(self))
|
|
11
|
-
end
|
|
3
|
+
require "date"
|
|
4
|
+
require "active_support/core_ext/time/calculations"
|
|
12
5
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
6
|
+
class String
|
|
7
|
+
# Converts a string to a Time value.
|
|
8
|
+
# The +form+ can be either +:utc+ or +:local+ (default +:local+).
|
|
9
|
+
#
|
|
10
|
+
# The time is parsed using Time.parse method.
|
|
11
|
+
# If +form+ is +:local+, then the time is in the system timezone.
|
|
12
|
+
# If the date part is missing then the current date is used and if
|
|
13
|
+
# the time part is missing then it is assumed to be 00:00:00.
|
|
14
|
+
#
|
|
15
|
+
# "13-12-2012".to_time # => 2012-12-13 00:00:00 +0100
|
|
16
|
+
# "06:12".to_time # => 2012-12-13 06:12:00 +0100
|
|
17
|
+
# "2012-12-13 06:12".to_time # => 2012-12-13 06:12:00 +0100
|
|
18
|
+
# "2012-12-13T06:12".to_time # => 2012-12-13 06:12:00 +0100
|
|
19
|
+
# "2012-12-13T06:12".to_time(:utc) # => 2012-12-13 06:12:00 UTC
|
|
20
|
+
# "12/13/2012".to_time # => ArgumentError: argument out of range
|
|
21
|
+
# "1604326192".to_time # => ArgumentError: argument out of range
|
|
22
|
+
def to_time(form = :local)
|
|
23
|
+
parts = Date._parse(self, false)
|
|
24
|
+
used_keys = %i(year mon mday hour min sec sec_fraction offset)
|
|
25
|
+
return if !parts.keys.intersect?(used_keys)
|
|
26
|
+
|
|
27
|
+
now = Time.now
|
|
28
|
+
time = Time.new(
|
|
29
|
+
parts.fetch(:year, now.year),
|
|
30
|
+
parts.fetch(:mon, now.month),
|
|
31
|
+
parts.fetch(:mday, now.day),
|
|
32
|
+
parts.fetch(:hour, 0),
|
|
33
|
+
parts.fetch(:min, 0),
|
|
34
|
+
parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0),
|
|
35
|
+
parts.fetch(:offset, form == :utc ? 0 : nil)
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
form == :utc ? time.utc : time.to_time
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Converts a string to a Date value.
|
|
42
|
+
#
|
|
43
|
+
# "1-1-2012".to_date # => Sun, 01 Jan 2012
|
|
44
|
+
# "01/01/2012".to_date # => Sun, 01 Jan 2012
|
|
45
|
+
# "2012-12-13".to_date # => Thu, 13 Dec 2012
|
|
46
|
+
# "12/13/2012".to_date # => ArgumentError: invalid date
|
|
47
|
+
def to_date
|
|
48
|
+
::Date.parse(self, false) unless blank?
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Converts a string to a DateTime value.
|
|
52
|
+
#
|
|
53
|
+
# "1-1-2012".to_datetime # => Sun, 01 Jan 2012 00:00:00 +0000
|
|
54
|
+
# "01/01/2012 23:59:59".to_datetime # => Sun, 01 Jan 2012 23:59:59 +0000
|
|
55
|
+
# "2012-12-13 12:50".to_datetime # => Thu, 13 Dec 2012 12:50:00 +0000
|
|
56
|
+
# "12/13/2012".to_datetime # => ArgumentError: invalid date
|
|
57
|
+
def to_datetime
|
|
58
|
+
::DateTime.parse(self, false) unless blank?
|
|
18
59
|
end
|
|
19
|
-
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class String
|
|
4
|
+
# The inverse of <tt>String#include?</tt>. Returns true if the string
|
|
5
|
+
# does not include the other string.
|
|
6
|
+
#
|
|
7
|
+
# "hello".exclude? "lo" # => false
|
|
8
|
+
# "hello".exclude? "ol" # => true
|
|
9
|
+
# "hello".exclude? ?h # => false
|
|
10
|
+
def exclude?(string)
|
|
11
|
+
!include?(string)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class String
|
|
4
|
+
# Returns the string, first removing all whitespace on both ends of
|
|
5
|
+
# the string, and then changing remaining consecutive whitespace
|
|
6
|
+
# groups into one space each.
|
|
7
|
+
#
|
|
8
|
+
# Note that it handles both ASCII and Unicode whitespace.
|
|
9
|
+
#
|
|
10
|
+
# %{ Multi-line
|
|
11
|
+
# string }.squish # => "Multi-line string"
|
|
12
|
+
# " foo bar \n \t boo".squish # => "foo bar boo"
|
|
13
|
+
def squish
|
|
14
|
+
dup.squish!
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Performs a destructive squish. See String#squish.
|
|
18
|
+
# str = " foo bar \n \t boo"
|
|
19
|
+
# str.squish! # => "foo bar boo"
|
|
20
|
+
# str # => "foo bar boo"
|
|
21
|
+
def squish!
|
|
22
|
+
gsub!(/[[:space:]]+/, " ")
|
|
23
|
+
strip!
|
|
24
|
+
self
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Returns a new string with all occurrences of the patterns removed.
|
|
28
|
+
# str = "foo bar test"
|
|
29
|
+
# str.remove(" test") # => "foo bar"
|
|
30
|
+
# str.remove(" test", /bar/) # => "foo "
|
|
31
|
+
# str # => "foo bar test"
|
|
32
|
+
def remove(*patterns)
|
|
33
|
+
dup.remove!(*patterns)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Alters the string by removing all occurrences of the patterns.
|
|
37
|
+
# str = "foo bar test"
|
|
38
|
+
# str.remove!(" test", /bar/) # => "foo "
|
|
39
|
+
# str # => "foo "
|
|
40
|
+
def remove!(*patterns)
|
|
41
|
+
patterns.each do |pattern|
|
|
42
|
+
gsub! pattern, ""
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
self
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Truncates a given +text+ to length <tt>truncate_to</tt> if +text+ is longer than <tt>truncate_to</tt>:
|
|
49
|
+
#
|
|
50
|
+
# 'Once upon a time in a world far far away'.truncate(27)
|
|
51
|
+
# # => "Once upon a time in a wo..."
|
|
52
|
+
#
|
|
53
|
+
# Pass a string or regexp <tt>:separator</tt> to truncate +text+ at a natural break:
|
|
54
|
+
#
|
|
55
|
+
# 'Once upon a time in a world far far away'.truncate(27, separator: ' ')
|
|
56
|
+
# # => "Once upon a time in a..."
|
|
57
|
+
#
|
|
58
|
+
# 'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
|
|
59
|
+
# # => "Once upon a time in a..."
|
|
60
|
+
#
|
|
61
|
+
# The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...").
|
|
62
|
+
# The total length will not exceed <tt>truncate_to</tt> unless both +text+ and <tt>:omission</tt>
|
|
63
|
+
# are longer than <tt>truncate_to</tt>:
|
|
64
|
+
#
|
|
65
|
+
# 'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
|
|
66
|
+
# # => "And they f... (continued)"
|
|
67
|
+
#
|
|
68
|
+
# 'And they found that many people were sleeping better.'.truncate(4, omission: '... (continued)')
|
|
69
|
+
# # => "... (continued)"
|
|
70
|
+
def truncate(truncate_to, options = {})
|
|
71
|
+
return dup unless length > truncate_to
|
|
72
|
+
|
|
73
|
+
omission = options[:omission] || "..."
|
|
74
|
+
length_with_room_for_omission = truncate_to - omission.length
|
|
75
|
+
stop = \
|
|
76
|
+
if options[:separator]
|
|
77
|
+
rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
|
|
78
|
+
else
|
|
79
|
+
length_with_room_for_omission
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
+"#{self[0, stop]}#{omission}"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Truncates +text+ to at most <tt>truncate_to</tt> bytes in length without
|
|
86
|
+
# breaking string encoding by splitting multibyte characters or breaking
|
|
87
|
+
# grapheme clusters ("perceptual characters") by truncating at combining
|
|
88
|
+
# characters.
|
|
89
|
+
#
|
|
90
|
+
# >> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".size
|
|
91
|
+
# # => 20
|
|
92
|
+
# >> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".bytesize
|
|
93
|
+
# # => 80
|
|
94
|
+
# >> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".truncate_bytes(20)
|
|
95
|
+
# # => "🔪🔪🔪🔪…"
|
|
96
|
+
#
|
|
97
|
+
# The truncated text ends with the <tt>:omission</tt> string, defaulting
|
|
98
|
+
# to "…", for a total length not exceeding <tt>truncate_to</tt>.
|
|
99
|
+
#
|
|
100
|
+
# Raises +ArgumentError+ when the bytesize of <tt>:omission</tt> exceeds <tt>truncate_to</tt>.
|
|
101
|
+
def truncate_bytes(truncate_to, omission: "…")
|
|
102
|
+
omission ||= ""
|
|
103
|
+
|
|
104
|
+
case
|
|
105
|
+
when bytesize <= truncate_to
|
|
106
|
+
dup
|
|
107
|
+
when omission.bytesize > truncate_to
|
|
108
|
+
raise ArgumentError, "Omission #{omission.inspect} is #{omission.bytesize}, larger than the truncation length of #{truncate_to} bytes"
|
|
109
|
+
when omission.bytesize == truncate_to
|
|
110
|
+
omission.dup
|
|
111
|
+
else
|
|
112
|
+
self.class.new.force_encoding(encoding).tap do |cut|
|
|
113
|
+
cut_at = truncate_to - omission.bytesize
|
|
114
|
+
|
|
115
|
+
each_grapheme_cluster do |grapheme|
|
|
116
|
+
if cut.bytesize + grapheme.bytesize <= cut_at
|
|
117
|
+
cut << grapheme
|
|
118
|
+
else
|
|
119
|
+
break
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
cut << omission
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Truncates a given +text+ after a given number of words (<tt>words_count</tt>):
|
|
129
|
+
#
|
|
130
|
+
# 'Once upon a time in a world far far away'.truncate_words(4)
|
|
131
|
+
# # => "Once upon a time..."
|
|
132
|
+
#
|
|
133
|
+
# Pass a string or regexp <tt>:separator</tt> to specify a different separator of words:
|
|
134
|
+
#
|
|
135
|
+
# 'Once<br>upon<br>a<br>time<br>in<br>a<br>world'.truncate_words(5, separator: '<br>')
|
|
136
|
+
# # => "Once<br>upon<br>a<br>time<br>in..."
|
|
137
|
+
#
|
|
138
|
+
# The last characters will be replaced with the <tt>:omission</tt> string (defaults to "..."):
|
|
139
|
+
#
|
|
140
|
+
# 'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)')
|
|
141
|
+
# # => "And they found that many... (continued)"
|
|
142
|
+
def truncate_words(words_count, options = {})
|
|
143
|
+
sep = options[:separator] || /\s+/
|
|
144
|
+
sep = Regexp.escape(sep.to_s) unless Regexp === sep
|
|
145
|
+
if self =~ /\A((?>.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m
|
|
146
|
+
$1 + (options[:omission] || "...")
|
|
147
|
+
else
|
|
148
|
+
dup
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class String
|
|
4
|
+
# Same as +indent+, except it indents the receiver in-place.
|
|
5
|
+
#
|
|
6
|
+
# Returns the indented string, or +nil+ if there was nothing to indent.
|
|
7
|
+
def indent!(amount, indent_string = nil, indent_empty_lines = false)
|
|
8
|
+
indent_string = indent_string || self[/^[ \t]/] || " "
|
|
9
|
+
re = indent_empty_lines ? /^/ : /^(?!$)/
|
|
10
|
+
gsub!(re, indent_string * amount)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Indents the lines in the receiver:
|
|
14
|
+
#
|
|
15
|
+
# <<EOS.indent(2)
|
|
16
|
+
# def some_method
|
|
17
|
+
# some_code
|
|
18
|
+
# end
|
|
19
|
+
# EOS
|
|
20
|
+
# # =>
|
|
21
|
+
# def some_method
|
|
22
|
+
# some_code
|
|
23
|
+
# end
|
|
24
|
+
#
|
|
25
|
+
# The second argument, +indent_string+, specifies which indent string to
|
|
26
|
+
# use. The default is +nil+, which tells the method to make a guess by
|
|
27
|
+
# peeking at the first indented line, and fall back to a space if there is
|
|
28
|
+
# none.
|
|
29
|
+
#
|
|
30
|
+
# " foo".indent(2) # => " foo"
|
|
31
|
+
# "foo\n\t\tbar".indent(2) # => "\t\tfoo\n\t\t\t\tbar"
|
|
32
|
+
# "foo".indent(2, "\t") # => "\t\tfoo"
|
|
33
|
+
#
|
|
34
|
+
# While +indent_string+ is typically one space or tab, it may be any string.
|
|
35
|
+
#
|
|
36
|
+
# The third argument, +indent_empty_lines+, is a flag that says whether
|
|
37
|
+
# empty lines should be indented. Default is false.
|
|
38
|
+
#
|
|
39
|
+
# "foo\n\nbar".indent(2) # => " foo\n\n bar"
|
|
40
|
+
# "foo\n\nbar".indent(2, nil, true) # => " foo\n \n bar"
|
|
41
|
+
#
|
|
42
|
+
def indent(amount, indent_string = nil, indent_empty_lines = false)
|
|
43
|
+
dup.tap { |_| _.indent!(amount, indent_string, indent_empty_lines) }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -1,57 +1,300 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/inflector/methods"
|
|
4
|
+
require "active_support/inflector/transliterate"
|
|
5
|
+
|
|
6
|
+
# String inflections define new methods on the String class to transform names for different purposes.
|
|
7
|
+
# For instance, you can figure out the name of a table from the name of a class.
|
|
8
|
+
#
|
|
9
|
+
# 'ScaleScore'.tableize # => "scale_scores"
|
|
10
|
+
#
|
|
11
|
+
class String
|
|
12
|
+
# Returns the plural form of the word in the string.
|
|
13
|
+
#
|
|
14
|
+
# If the optional parameter +count+ is specified,
|
|
15
|
+
# the singular form will be returned if <tt>count == 1</tt>.
|
|
16
|
+
# For any other value of +count+ the plural will be returned.
|
|
17
|
+
#
|
|
18
|
+
# If the optional parameter +locale+ is specified,
|
|
19
|
+
# the word will be pluralized as a word of that language.
|
|
20
|
+
# By default, this parameter is set to <tt>:en</tt>.
|
|
21
|
+
# You must define your own inflection rules for languages other than English.
|
|
22
|
+
#
|
|
23
|
+
# 'post'.pluralize # => "posts"
|
|
24
|
+
# 'octopus'.pluralize # => "octopi"
|
|
25
|
+
# 'sheep'.pluralize # => "sheep"
|
|
26
|
+
# 'words'.pluralize # => "words"
|
|
27
|
+
# 'the blue mailman'.pluralize # => "the blue mailmen"
|
|
28
|
+
# 'CamelOctopus'.pluralize # => "CamelOctopi"
|
|
29
|
+
# 'apple'.pluralize(1) # => "apple"
|
|
30
|
+
# 'apple'.pluralize(2) # => "apples"
|
|
31
|
+
# 'ley'.pluralize(:es) # => "leyes"
|
|
32
|
+
# 'ley'.pluralize(1, :es) # => "ley"
|
|
33
|
+
#
|
|
34
|
+
# See ActiveSupport::Inflector.pluralize.
|
|
35
|
+
def pluralize(count = nil, locale = :en)
|
|
36
|
+
locale = count if count.is_a?(Symbol)
|
|
37
|
+
if count == 1
|
|
38
|
+
dup
|
|
39
|
+
else
|
|
40
|
+
ActiveSupport::Inflector.pluralize(self, locale)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The reverse of +pluralize+, returns the singular form of a word in a string.
|
|
45
|
+
#
|
|
46
|
+
# If the optional parameter +locale+ is specified,
|
|
47
|
+
# the word will be singularized as a word of that language.
|
|
48
|
+
# By default, this parameter is set to <tt>:en</tt>.
|
|
49
|
+
# You must define your own inflection rules for languages other than English.
|
|
50
|
+
#
|
|
51
|
+
# 'posts'.singularize # => "post"
|
|
52
|
+
# 'octopi'.singularize # => "octopus"
|
|
53
|
+
# 'sheep'.singularize # => "sheep"
|
|
54
|
+
# 'word'.singularize # => "word"
|
|
55
|
+
# 'the blue mailmen'.singularize # => "the blue mailman"
|
|
56
|
+
# 'CamelOctopi'.singularize # => "CamelOctopus"
|
|
57
|
+
# 'leyes'.singularize(:es) # => "ley"
|
|
58
|
+
#
|
|
59
|
+
# See ActiveSupport::Inflector.singularize.
|
|
60
|
+
def singularize(locale = :en)
|
|
61
|
+
ActiveSupport::Inflector.singularize(self, locale)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# +constantize+ tries to find a declared constant with the name specified
|
|
65
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
|
66
|
+
# or is not initialized.
|
|
67
|
+
#
|
|
68
|
+
# 'Module'.constantize # => Module
|
|
69
|
+
# 'Class'.constantize # => Class
|
|
70
|
+
# 'blargle'.constantize # => NameError: wrong constant name blargle
|
|
71
|
+
#
|
|
72
|
+
# See ActiveSupport::Inflector.constantize.
|
|
73
|
+
def constantize
|
|
74
|
+
ActiveSupport::Inflector.constantize(self)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# +safe_constantize+ tries to find a declared constant with the name specified
|
|
78
|
+
# in the string. It returns +nil+ when the name is not in CamelCase
|
|
79
|
+
# or is not initialized.
|
|
80
|
+
#
|
|
81
|
+
# 'Module'.safe_constantize # => Module
|
|
82
|
+
# 'Class'.safe_constantize # => Class
|
|
83
|
+
# 'blargle'.safe_constantize # => nil
|
|
84
|
+
#
|
|
85
|
+
# See ActiveSupport::Inflector.safe_constantize.
|
|
86
|
+
def safe_constantize
|
|
87
|
+
ActiveSupport::Inflector.safe_constantize(self)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
|
|
91
|
+
# is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
|
|
92
|
+
#
|
|
93
|
+
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
|
|
94
|
+
#
|
|
95
|
+
# 'active_record'.camelize # => "ActiveRecord"
|
|
96
|
+
# 'active_record'.camelize(:lower) # => "activeRecord"
|
|
97
|
+
# 'active_record/errors'.camelize # => "ActiveRecord::Errors"
|
|
98
|
+
# 'active_record/errors'.camelize(:lower) # => "activeRecord::Errors"
|
|
99
|
+
#
|
|
100
|
+
# See ActiveSupport::Inflector.camelize.
|
|
101
|
+
def camelize(first_letter = :upper)
|
|
102
|
+
case first_letter
|
|
103
|
+
when :upper
|
|
104
|
+
ActiveSupport::Inflector.camelize(self, true)
|
|
105
|
+
when :lower
|
|
106
|
+
ActiveSupport::Inflector.camelize(self, false)
|
|
107
|
+
else
|
|
108
|
+
raise ArgumentError, "Invalid option, use either :upper or :lower."
|
|
55
109
|
end
|
|
56
110
|
end
|
|
111
|
+
alias_method :camelcase, :camelize
|
|
112
|
+
|
|
113
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
|
114
|
+
# a nicer looking title. +titleize+ is meant for creating pretty output. It is not
|
|
115
|
+
# used in the \Rails internals.
|
|
116
|
+
#
|
|
117
|
+
# The trailing '_id','Id'.. can be kept and capitalized by setting the
|
|
118
|
+
# optional parameter +keep_id_suffix+ to true.
|
|
119
|
+
# By default, this parameter is false.
|
|
120
|
+
#
|
|
121
|
+
# 'man from the boondocks'.titleize # => "Man From The Boondocks"
|
|
122
|
+
# 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
|
|
123
|
+
# 'string_ending_with_id'.titleize(keep_id_suffix: true) # => "String Ending With Id"
|
|
124
|
+
#
|
|
125
|
+
# See ActiveSupport::Inflector.titleize.
|
|
126
|
+
def titleize(keep_id_suffix: false)
|
|
127
|
+
ActiveSupport::Inflector.titleize(self, keep_id_suffix: keep_id_suffix)
|
|
128
|
+
end
|
|
129
|
+
alias_method :titlecase, :titleize
|
|
130
|
+
|
|
131
|
+
# The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
|
|
132
|
+
#
|
|
133
|
+
# +underscore+ will also change '::' to '/' to convert namespaces to paths.
|
|
134
|
+
#
|
|
135
|
+
# 'ActiveModel'.underscore # => "active_model"
|
|
136
|
+
# 'ActiveModel::Errors'.underscore # => "active_model/errors"
|
|
137
|
+
#
|
|
138
|
+
# See ActiveSupport::Inflector.underscore.
|
|
139
|
+
def underscore
|
|
140
|
+
ActiveSupport::Inflector.underscore(self)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Replaces underscores with dashes in the string.
|
|
144
|
+
#
|
|
145
|
+
# 'puni_puni'.dasherize # => "puni-puni"
|
|
146
|
+
#
|
|
147
|
+
# See ActiveSupport::Inflector.dasherize.
|
|
148
|
+
def dasherize
|
|
149
|
+
ActiveSupport::Inflector.dasherize(self)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Removes the module part from the constant expression in the string.
|
|
153
|
+
#
|
|
154
|
+
# 'ActiveSupport::Inflector::Inflections'.demodulize # => "Inflections"
|
|
155
|
+
# 'Inflections'.demodulize # => "Inflections"
|
|
156
|
+
# '::Inflections'.demodulize # => "Inflections"
|
|
157
|
+
# ''.demodulize # => ''
|
|
158
|
+
#
|
|
159
|
+
# See ActiveSupport::Inflector.demodulize.
|
|
160
|
+
#
|
|
161
|
+
# See also +deconstantize+.
|
|
162
|
+
def demodulize
|
|
163
|
+
ActiveSupport::Inflector.demodulize(self)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Removes the rightmost segment from the constant expression in the string.
|
|
167
|
+
#
|
|
168
|
+
# 'Net::HTTP'.deconstantize # => "Net"
|
|
169
|
+
# '::Net::HTTP'.deconstantize # => "::Net"
|
|
170
|
+
# 'String'.deconstantize # => ""
|
|
171
|
+
# '::String'.deconstantize # => ""
|
|
172
|
+
# ''.deconstantize # => ""
|
|
173
|
+
#
|
|
174
|
+
# See ActiveSupport::Inflector.deconstantize.
|
|
175
|
+
#
|
|
176
|
+
# See also +demodulize+.
|
|
177
|
+
def deconstantize
|
|
178
|
+
ActiveSupport::Inflector.deconstantize(self)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
|
|
182
|
+
#
|
|
183
|
+
# If the optional parameter +locale+ is specified,
|
|
184
|
+
# the word will be parameterized as a word of that language.
|
|
185
|
+
# By default, this parameter is set to <tt>nil</tt> and it will use
|
|
186
|
+
# the configured <tt>I18n.locale</tt>.
|
|
187
|
+
#
|
|
188
|
+
# class Person
|
|
189
|
+
# def to_param
|
|
190
|
+
# "#{id}-#{name.parameterize}"
|
|
191
|
+
# end
|
|
192
|
+
# end
|
|
193
|
+
#
|
|
194
|
+
# @person = Person.find(1)
|
|
195
|
+
# # => #<Person id: 1, name: "Donald E. Knuth">
|
|
196
|
+
#
|
|
197
|
+
# <%= link_to(@person.name, person_path) %>
|
|
198
|
+
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
|
|
199
|
+
#
|
|
200
|
+
# To preserve the case of the characters in a string, use the +preserve_case+ argument.
|
|
201
|
+
#
|
|
202
|
+
# class Person
|
|
203
|
+
# def to_param
|
|
204
|
+
# "#{id}-#{name.parameterize(preserve_case: true)}"
|
|
205
|
+
# end
|
|
206
|
+
# end
|
|
207
|
+
#
|
|
208
|
+
# @person = Person.find(1)
|
|
209
|
+
# # => #<Person id: 1, name: "Donald E. Knuth">
|
|
210
|
+
#
|
|
211
|
+
# <%= link_to(@person.name, person_path) %>
|
|
212
|
+
# # => <a href="/person/1-Donald-E-Knuth">Donald E. Knuth</a>
|
|
213
|
+
#
|
|
214
|
+
# See ActiveSupport::Inflector.parameterize.
|
|
215
|
+
def parameterize(separator: "-", preserve_case: false, locale: nil)
|
|
216
|
+
ActiveSupport::Inflector.parameterize(self, separator: separator, preserve_case: preserve_case, locale: locale)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# Creates the name of a table like \Rails does for models to table names. This method
|
|
220
|
+
# uses the +pluralize+ method on the last word in the string.
|
|
221
|
+
#
|
|
222
|
+
# 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
|
|
223
|
+
# 'ham_and_egg'.tableize # => "ham_and_eggs"
|
|
224
|
+
# 'fancyCategory'.tableize # => "fancy_categories"
|
|
225
|
+
#
|
|
226
|
+
# See ActiveSupport::Inflector.tableize.
|
|
227
|
+
def tableize
|
|
228
|
+
ActiveSupport::Inflector.tableize(self)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# Creates a class name from a plural table name like \Rails does for table names to models.
|
|
232
|
+
# Note that this returns a string and not a class. (To convert to an actual class
|
|
233
|
+
# follow +classify+ with +constantize+.)
|
|
234
|
+
#
|
|
235
|
+
# 'ham_and_eggs'.classify # => "HamAndEgg"
|
|
236
|
+
# 'posts'.classify # => "Post"
|
|
237
|
+
#
|
|
238
|
+
# See ActiveSupport::Inflector.classify.
|
|
239
|
+
def classify
|
|
240
|
+
ActiveSupport::Inflector.classify(self)
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
# Capitalizes the first word, turns underscores into spaces, and (by default) strips a
|
|
244
|
+
# trailing '_id' if present.
|
|
245
|
+
# Like +titleize+, this is meant for creating pretty output.
|
|
246
|
+
#
|
|
247
|
+
# The capitalization of the first word can be turned off by setting the
|
|
248
|
+
# optional parameter +capitalize+ to false.
|
|
249
|
+
# By default, this parameter is true.
|
|
250
|
+
#
|
|
251
|
+
# The trailing '_id' can be kept and capitalized by setting the
|
|
252
|
+
# optional parameter +keep_id_suffix+ to true.
|
|
253
|
+
# By default, this parameter is false.
|
|
254
|
+
#
|
|
255
|
+
# 'employee_salary'.humanize # => "Employee salary"
|
|
256
|
+
# 'author_id'.humanize # => "Author"
|
|
257
|
+
# 'author_id'.humanize(capitalize: false) # => "author"
|
|
258
|
+
# '_id'.humanize # => "Id"
|
|
259
|
+
# 'author_id'.humanize(keep_id_suffix: true) # => "Author id"
|
|
260
|
+
#
|
|
261
|
+
# See ActiveSupport::Inflector.humanize.
|
|
262
|
+
def humanize(capitalize: true, keep_id_suffix: false)
|
|
263
|
+
ActiveSupport::Inflector.humanize(self, capitalize: capitalize, keep_id_suffix: keep_id_suffix)
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# Converts the first character to uppercase.
|
|
267
|
+
#
|
|
268
|
+
# 'what a Lovely Day'.upcase_first # => "What a Lovely Day"
|
|
269
|
+
# 'w'.upcase_first # => "W"
|
|
270
|
+
# ''.upcase_first # => ""
|
|
271
|
+
#
|
|
272
|
+
# See ActiveSupport::Inflector.upcase_first.
|
|
273
|
+
def upcase_first
|
|
274
|
+
ActiveSupport::Inflector.upcase_first(self)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# Converts the first character to lowercase.
|
|
278
|
+
#
|
|
279
|
+
# 'If they enjoyed The Matrix'.downcase_first # => "if they enjoyed The Matrix"
|
|
280
|
+
# 'I'.downcase_first # => "i"
|
|
281
|
+
# ''.downcase_first # => ""
|
|
282
|
+
#
|
|
283
|
+
# See ActiveSupport::Inflector.downcase_first.
|
|
284
|
+
def downcase_first
|
|
285
|
+
ActiveSupport::Inflector.downcase_first(self)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# Creates a foreign key name from a class name.
|
|
289
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
|
290
|
+
# the method should put '_' between the name and 'id'.
|
|
291
|
+
#
|
|
292
|
+
# 'Message'.foreign_key # => "message_id"
|
|
293
|
+
# 'Message'.foreign_key(false) # => "messageid"
|
|
294
|
+
# 'Admin::Post'.foreign_key # => "post_id"
|
|
295
|
+
#
|
|
296
|
+
# See ActiveSupport::Inflector.foreign_key.
|
|
297
|
+
def foreign_key(separate_class_name_and_id_with_underscore = true)
|
|
298
|
+
ActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
|
|
299
|
+
end
|
|
57
300
|
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/string_inquirer"
|
|
4
|
+
require "active_support/environment_inquirer"
|
|
5
|
+
|
|
6
|
+
class String
|
|
7
|
+
# Wraps the current string in the ActiveSupport::StringInquirer class,
|
|
8
|
+
# which gives you a prettier way to test for equality.
|
|
9
|
+
#
|
|
10
|
+
# env = 'production'.inquiry
|
|
11
|
+
# env.production? # => true
|
|
12
|
+
# env.development? # => false
|
|
13
|
+
def inquiry
|
|
14
|
+
ActiveSupport::StringInquirer.new(self)
|
|
15
|
+
end
|
|
16
|
+
end
|