activesupport 6.0.0
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 +572 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +40 -0
- data/lib/active_support.rb +96 -0
- data/lib/active_support/actionable_error.rb +48 -0
- data/lib/active_support/all.rb +5 -0
- data/lib/active_support/array_inquirer.rb +48 -0
- data/lib/active_support/backtrace_cleaner.rb +132 -0
- data/lib/active_support/benchmarkable.rb +51 -0
- data/lib/active_support/builder.rb +8 -0
- data/lib/active_support/cache.rb +830 -0
- data/lib/active_support/cache/file_store.rb +196 -0
- data/lib/active_support/cache/mem_cache_store.rb +212 -0
- data/lib/active_support/cache/memory_store.rb +174 -0
- data/lib/active_support/cache/null_store.rb +48 -0
- data/lib/active_support/cache/redis_cache_store.rb +488 -0
- data/lib/active_support/cache/strategy/local_cache.rb +194 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
- data/lib/active_support/callbacks.rb +856 -0
- data/lib/active_support/concern.rb +171 -0
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
- data/lib/active_support/concurrency/share_lock.rb +227 -0
- data/lib/active_support/configurable.rb +146 -0
- data/lib/active_support/core_ext.rb +5 -0
- data/lib/active_support/core_ext/array.rb +9 -0
- data/lib/active_support/core_ext/array/access.rb +104 -0
- data/lib/active_support/core_ext/array/conversions.rb +213 -0
- 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/prepend_and_append.rb +5 -0
- data/lib/active_support/core_ext/array/wrap.rb +48 -0
- data/lib/active_support/core_ext/benchmark.rb +16 -0
- data/lib/active_support/core_ext/big_decimal.rb +3 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/class.rb +4 -0
- data/lib/active_support/core_ext/class/attribute.rb +141 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
- data/lib/active_support/core_ext/class/subclasses.rb +54 -0
- data/lib/active_support/core_ext/date.rb +7 -0
- data/lib/active_support/core_ext/date/acts_like.rb +10 -0
- data/lib/active_support/core_ext/date/blank.rb +14 -0
- data/lib/active_support/core_ext/date/calculations.rb +146 -0
- data/lib/active_support/core_ext/date/conversions.rb +96 -0
- data/lib/active_support/core_ext/date/zones.rb +8 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +351 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +41 -0
- data/lib/active_support/core_ext/date_time.rb +7 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
- data/lib/active_support/core_ext/date_time/blank.rb +14 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +211 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +107 -0
- data/lib/active_support/core_ext/digest.rb +3 -0
- data/lib/active_support/core_ext/digest/uuid.rb +53 -0
- data/lib/active_support/core_ext/enumerable.rb +188 -0
- data/lib/active_support/core_ext/file.rb +3 -0
- data/lib/active_support/core_ext/file/atomic.rb +70 -0
- data/lib/active_support/core_ext/hash.rb +10 -0
- data/lib/active_support/core_ext/hash/compact.rb +5 -0
- data/lib/active_support/core_ext/hash/conversions.rb +263 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +34 -0
- data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
- data/lib/active_support/core_ext/hash/except.rb +24 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +24 -0
- data/lib/active_support/core_ext/hash/keys.rb +143 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
- data/lib/active_support/core_ext/hash/slice.rb +26 -0
- data/lib/active_support/core_ext/hash/transform_values.rb +5 -0
- data/lib/active_support/core_ext/integer.rb +5 -0
- data/lib/active_support/core_ext/integer/inflections.rb +31 -0
- 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/kernel.rb +5 -0
- 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/load_error.rb +9 -0
- data/lib/active_support/core_ext/marshal.rb +24 -0
- data/lib/active_support/core_ext/module.rb +13 -0
- 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 +38 -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 +144 -0
- data/lib/active_support/core_ext/module/concerning.rb +134 -0
- data/lib/active_support/core_ext/module/delegation.rb +313 -0
- data/lib/active_support/core_ext/module/deprecation.rb +25 -0
- data/lib/active_support/core_ext/module/introspection.rb +86 -0
- data/lib/active_support/core_ext/module/reachable.rb +6 -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/name_error.rb +38 -0
- data/lib/active_support/core_ext/numeric.rb +5 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +66 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +136 -0
- data/lib/active_support/core_ext/numeric/inquiry.rb +5 -0
- data/lib/active_support/core_ext/numeric/time.rb +66 -0
- data/lib/active_support/core_ext/object.rb +16 -0
- data/lib/active_support/core_ext/object/acts_like.rb +21 -0
- data/lib/active_support/core_ext/object/blank.rb +155 -0
- data/lib/active_support/core_ext/object/conversions.rb +6 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +55 -0
- data/lib/active_support/core_ext/object/duplicable.rb +49 -0
- data/lib/active_support/core_ext/object/inclusion.rb +29 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +30 -0
- data/lib/active_support/core_ext/object/json.rb +228 -0
- data/lib/active_support/core_ext/object/to_param.rb +3 -0
- data/lib/active_support/core_ext/object/to_query.rb +89 -0
- data/lib/active_support/core_ext/object/try.rb +156 -0
- data/lib/active_support/core_ext/object/with_options.rb +82 -0
- data/lib/active_support/core_ext/range.rb +7 -0
- data/lib/active_support/core_ext/range/compare_range.rb +70 -0
- data/lib/active_support/core_ext/range/conversions.rb +41 -0
- data/lib/active_support/core_ext/range/each.rb +25 -0
- data/lib/active_support/core_ext/range/include_range.rb +9 -0
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +10 -0
- data/lib/active_support/core_ext/regexp.rb +7 -0
- data/lib/active_support/core_ext/securerandom.rb +45 -0
- data/lib/active_support/core_ext/string.rb +15 -0
- data/lib/active_support/core_ext/string/access.rb +114 -0
- data/lib/active_support/core_ext/string/behavior.rb +8 -0
- data/lib/active_support/core_ext/string/conversions.rb +59 -0
- data/lib/active_support/core_ext/string/exclude.rb +13 -0
- data/lib/active_support/core_ext/string/filters.rb +145 -0
- data/lib/active_support/core_ext/string/indent.rb +45 -0
- data/lib/active_support/core_ext/string/inflections.rb +259 -0
- data/lib/active_support/core_ext/string/inquiry.rb +15 -0
- data/lib/active_support/core_ext/string/multibyte.rb +58 -0
- data/lib/active_support/core_ext/string/output_safety.rb +314 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +6 -0
- 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/time.rb +7 -0
- data/lib/active_support/core_ext/time/acts_like.rb +10 -0
- data/lib/active_support/core_ext/time/calculations.rb +344 -0
- data/lib/active_support/core_ext/time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/time/conversions.rb +72 -0
- data/lib/active_support/core_ext/time/zones.rb +113 -0
- data/lib/active_support/core_ext/uri.rb +25 -0
- data/lib/active_support/current_attributes.rb +203 -0
- data/lib/active_support/dependencies.rb +806 -0
- data/lib/active_support/dependencies/autoload.rb +79 -0
- data/lib/active_support/dependencies/interlock.rb +57 -0
- data/lib/active_support/dependencies/zeitwerk_integration.rb +110 -0
- data/lib/active_support/deprecation.rb +46 -0
- data/lib/active_support/deprecation/behaviors.rb +109 -0
- data/lib/active_support/deprecation/constant_accessor.rb +52 -0
- data/lib/active_support/deprecation/instance_delegator.rb +39 -0
- data/lib/active_support/deprecation/method_wrappers.rb +78 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +173 -0
- data/lib/active_support/deprecation/reporting.rb +114 -0
- data/lib/active_support/descendants_tracker.rb +109 -0
- data/lib/active_support/digest.rb +20 -0
- data/lib/active_support/duration.rb +433 -0
- data/lib/active_support/duration/iso8601_parser.rb +124 -0
- data/lib/active_support/duration/iso8601_serializer.rb +54 -0
- data/lib/active_support/encrypted_configuration.rb +45 -0
- data/lib/active_support/encrypted_file.rb +100 -0
- data/lib/active_support/evented_file_update_checker.rb +235 -0
- data/lib/active_support/execution_wrapper.rb +129 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +163 -0
- data/lib/active_support/gem_version.rb +17 -0
- data/lib/active_support/gzip.rb +38 -0
- data/lib/active_support/hash_with_indifferent_access.rb +399 -0
- data/lib/active_support/i18n.rb +16 -0
- data/lib/active_support/i18n_railtie.rb +126 -0
- data/lib/active_support/inflections.rb +72 -0
- data/lib/active_support/inflector.rb +9 -0
- data/lib/active_support/inflector/inflections.rb +257 -0
- data/lib/active_support/inflector/methods.rb +398 -0
- data/lib/active_support/inflector/transliterate.rb +147 -0
- data/lib/active_support/json.rb +4 -0
- data/lib/active_support/json/decoding.rb +76 -0
- data/lib/active_support/json/encoding.rb +134 -0
- data/lib/active_support/key_generator.rb +41 -0
- data/lib/active_support/lazy_load_hooks.rb +82 -0
- data/lib/active_support/locale/en.rb +31 -0
- data/lib/active_support/locale/en.yml +135 -0
- data/lib/active_support/log_subscriber.rb +135 -0
- data/lib/active_support/log_subscriber/test_helper.rb +106 -0
- data/lib/active_support/logger.rb +93 -0
- data/lib/active_support/logger_silence.rb +45 -0
- data/lib/active_support/logger_thread_safe_level.rb +56 -0
- data/lib/active_support/message_encryptor.rb +227 -0
- data/lib/active_support/message_verifier.rb +205 -0
- data/lib/active_support/messages/metadata.rb +71 -0
- data/lib/active_support/messages/rotation_configuration.rb +22 -0
- data/lib/active_support/messages/rotator.rb +56 -0
- data/lib/active_support/multibyte.rb +23 -0
- data/lib/active_support/multibyte/chars.rb +216 -0
- data/lib/active_support/multibyte/unicode.rb +157 -0
- data/lib/active_support/notifications.rb +253 -0
- data/lib/active_support/notifications/fanout.rb +244 -0
- data/lib/active_support/notifications/instrumenter.rb +164 -0
- data/lib/active_support/number_helper.rb +378 -0
- data/lib/active_support/number_helper/number_converter.rb +184 -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 +31 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +70 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +61 -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 +56 -0
- data/lib/active_support/number_helper/rounding_helper.rb +66 -0
- data/lib/active_support/option_merger.rb +27 -0
- data/lib/active_support/ordered_hash.rb +50 -0
- data/lib/active_support/ordered_options.rb +85 -0
- data/lib/active_support/parameter_filter.rb +129 -0
- data/lib/active_support/per_thread_registry.rb +60 -0
- data/lib/active_support/proxy_object.rb +15 -0
- data/lib/active_support/rails.rb +29 -0
- data/lib/active_support/railtie.rb +80 -0
- data/lib/active_support/reloader.rb +130 -0
- data/lib/active_support/rescuable.rb +174 -0
- data/lib/active_support/security_utils.rb +31 -0
- data/lib/active_support/string_inquirer.rb +34 -0
- data/lib/active_support/subscriber.rb +169 -0
- data/lib/active_support/tagged_logging.rb +88 -0
- data/lib/active_support/test_case.rb +163 -0
- data/lib/active_support/testing/assertions.rb +228 -0
- data/lib/active_support/testing/autorun.rb +7 -0
- data/lib/active_support/testing/constant_lookup.rb +51 -0
- data/lib/active_support/testing/declarative.rb +28 -0
- data/lib/active_support/testing/deprecation.rb +38 -0
- data/lib/active_support/testing/file_fixtures.rb +38 -0
- data/lib/active_support/testing/isolation.rb +110 -0
- data/lib/active_support/testing/method_call_assertions.rb +70 -0
- data/lib/active_support/testing/parallelization.rb +128 -0
- data/lib/active_support/testing/setup_and_teardown.rb +55 -0
- data/lib/active_support/testing/stream.rb +44 -0
- data/lib/active_support/testing/tagged_logging.rb +27 -0
- data/lib/active_support/testing/time_helpers.rb +200 -0
- data/lib/active_support/time.rb +20 -0
- data/lib/active_support/time_with_zone.rb +561 -0
- data/lib/active_support/values/time_zone.rb +570 -0
- data/lib/active_support/version.rb +10 -0
- data/lib/active_support/xml_mini.rb +202 -0
- data/lib/active_support/xml_mini/jdom.rb +183 -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 +130 -0
- metadata +385 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/time_with_zone"
|
|
4
|
+
|
|
5
|
+
module ActiveSupport
|
|
6
|
+
module EachTimeWithZone #:nodoc:
|
|
7
|
+
def each(&block)
|
|
8
|
+
ensure_iteration_allowed
|
|
9
|
+
super
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def step(n = 1, &block)
|
|
13
|
+
ensure_iteration_allowed
|
|
14
|
+
super
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def ensure_iteration_allowed
|
|
20
|
+
raise TypeError, "can't iterate from #{first.class}" if first.is_a?(TimeWithZone)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
Range.prepend(ActiveSupport::EachTimeWithZone)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/deprecation"
|
|
4
|
+
|
|
5
|
+
ActiveSupport::Deprecation.warn "You have required `active_support/core_ext/range/include_range`. " \
|
|
6
|
+
"This file will be removed in Rails 6.1. You should require `active_support/core_ext/range/compare_range` " \
|
|
7
|
+
"instead."
|
|
8
|
+
|
|
9
|
+
require "active_support/core_ext/range/compare_range"
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/time_with_zone"
|
|
4
|
+
|
|
5
|
+
module ActiveSupport
|
|
6
|
+
module IncludeTimeWithZone #:nodoc:
|
|
7
|
+
# Extends the default Range#include? to support ActiveSupport::TimeWithZone.
|
|
8
|
+
#
|
|
9
|
+
# (1.hour.ago..1.hour.from_now).include?(Time.current) # => true
|
|
10
|
+
#
|
|
11
|
+
def include?(value)
|
|
12
|
+
if first.is_a?(TimeWithZone)
|
|
13
|
+
cover?(value)
|
|
14
|
+
elsif last.is_a?(TimeWithZone)
|
|
15
|
+
cover?(value)
|
|
16
|
+
else
|
|
17
|
+
super
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
Range.prepend(ActiveSupport::IncludeTimeWithZone)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
module SecureRandom
|
|
6
|
+
BASE58_ALPHABET = ("0".."9").to_a + ("A".."Z").to_a + ("a".."z").to_a - ["0", "O", "I", "l"]
|
|
7
|
+
BASE36_ALPHABET = ("0".."9").to_a + ("a".."z").to_a
|
|
8
|
+
|
|
9
|
+
# SecureRandom.base58 generates a random base58 string.
|
|
10
|
+
#
|
|
11
|
+
# The argument _n_ specifies the length of the random string to be generated.
|
|
12
|
+
#
|
|
13
|
+
# If _n_ is not specified or is +nil+, 16 is assumed. It may be larger in the future.
|
|
14
|
+
#
|
|
15
|
+
# The result may contain alphanumeric characters except 0, O, I and l.
|
|
16
|
+
#
|
|
17
|
+
# p SecureRandom.base58 # => "4kUgL2pdQMSCQtjE"
|
|
18
|
+
# p SecureRandom.base58(24) # => "77TMHrHJFvFDwodq8w7Ev2m7"
|
|
19
|
+
def self.base58(n = 16)
|
|
20
|
+
SecureRandom.random_bytes(n).unpack("C*").map do |byte|
|
|
21
|
+
idx = byte % 64
|
|
22
|
+
idx = SecureRandom.random_number(58) if idx >= 58
|
|
23
|
+
BASE58_ALPHABET[idx]
|
|
24
|
+
end.join
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# SecureRandom.base36 generates a random base36 string in lowercase.
|
|
28
|
+
#
|
|
29
|
+
# The argument _n_ specifies the length of the random string to be generated.
|
|
30
|
+
#
|
|
31
|
+
# If _n_ is not specified or is +nil+, 16 is assumed. It may be larger in the future.
|
|
32
|
+
# This method can be used over +base58+ if a deterministic case key is necessary.
|
|
33
|
+
#
|
|
34
|
+
# The result will contain alphanumeric characters in lowercase.
|
|
35
|
+
#
|
|
36
|
+
# p SecureRandom.base36 # => "4kugl2pdqmscqtje"
|
|
37
|
+
# p SecureRandom.base36(24) # => "77tmhrhjfvfdwodq8w7ev2m7"
|
|
38
|
+
def self.base36(n = 16)
|
|
39
|
+
SecureRandom.random_bytes(n).unpack("C*").map do |byte|
|
|
40
|
+
idx = byte % 64
|
|
41
|
+
idx = SecureRandom.random_number(36) if idx >= 36
|
|
42
|
+
BASE36_ALPHABET[idx]
|
|
43
|
+
end.join
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/core_ext/string/conversions"
|
|
4
|
+
require "active_support/core_ext/string/filters"
|
|
5
|
+
require "active_support/core_ext/string/multibyte"
|
|
6
|
+
require "active_support/core_ext/string/starts_ends_with"
|
|
7
|
+
require "active_support/core_ext/string/inflections"
|
|
8
|
+
require "active_support/core_ext/string/access"
|
|
9
|
+
require "active_support/core_ext/string/behavior"
|
|
10
|
+
require "active_support/core_ext/string/output_safety"
|
|
11
|
+
require "active_support/core_ext/string/exclude"
|
|
12
|
+
require "active_support/core_ext/string/strip"
|
|
13
|
+
require "active_support/core_ext/string/inquiry"
|
|
14
|
+
require "active_support/core_ext/string/indent"
|
|
15
|
+
require "active_support/core_ext/string/zones"
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class String
|
|
4
|
+
# If you pass a single integer, returns a substring of one character at that
|
|
5
|
+
# position. The first character of the string is at position 0, the next at
|
|
6
|
+
# position 1, and so on. If a range is supplied, a substring containing
|
|
7
|
+
# characters at offsets given by the range is returned. In both cases, if an
|
|
8
|
+
# offset is negative, it is counted from the end of the string. Returns +nil+
|
|
9
|
+
# if the initial offset falls outside the string. Returns an empty string if
|
|
10
|
+
# the beginning of the range is greater than the end of the string.
|
|
11
|
+
#
|
|
12
|
+
# str = "hello"
|
|
13
|
+
# str.at(0) # => "h"
|
|
14
|
+
# str.at(1..3) # => "ell"
|
|
15
|
+
# str.at(-2) # => "l"
|
|
16
|
+
# str.at(-2..-1) # => "lo"
|
|
17
|
+
# str.at(5) # => nil
|
|
18
|
+
# str.at(5..-1) # => ""
|
|
19
|
+
#
|
|
20
|
+
# If a Regexp is given, the matching portion of the string is returned.
|
|
21
|
+
# If a String is given, that given string is returned if it occurs in
|
|
22
|
+
# the string. In both cases, +nil+ is returned if there is no match.
|
|
23
|
+
#
|
|
24
|
+
# str = "hello"
|
|
25
|
+
# str.at(/lo/) # => "lo"
|
|
26
|
+
# str.at(/ol/) # => nil
|
|
27
|
+
# str.at("lo") # => "lo"
|
|
28
|
+
# str.at("ol") # => nil
|
|
29
|
+
def at(position)
|
|
30
|
+
self[position]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Returns a substring from the given position to the end of the string.
|
|
34
|
+
# If the position is negative, it is counted from the end of the string.
|
|
35
|
+
#
|
|
36
|
+
# str = "hello"
|
|
37
|
+
# str.from(0) # => "hello"
|
|
38
|
+
# str.from(3) # => "lo"
|
|
39
|
+
# str.from(-2) # => "lo"
|
|
40
|
+
#
|
|
41
|
+
# You can mix it with +to+ method and do fun things like:
|
|
42
|
+
#
|
|
43
|
+
# str = "hello"
|
|
44
|
+
# str.from(0).to(-1) # => "hello"
|
|
45
|
+
# str.from(1).to(-2) # => "ell"
|
|
46
|
+
def from(position)
|
|
47
|
+
self[position..-1]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Returns a substring from the beginning of the string to the given position.
|
|
51
|
+
# If the position is negative, it is counted from the end of the string.
|
|
52
|
+
#
|
|
53
|
+
# str = "hello"
|
|
54
|
+
# str.to(0) # => "h"
|
|
55
|
+
# str.to(3) # => "hell"
|
|
56
|
+
# str.to(-2) # => "hell"
|
|
57
|
+
#
|
|
58
|
+
# You can mix it with +from+ method and do fun things like:
|
|
59
|
+
#
|
|
60
|
+
# str = "hello"
|
|
61
|
+
# str.from(0).to(-1) # => "hello"
|
|
62
|
+
# str.from(1).to(-2) # => "ell"
|
|
63
|
+
def to(position)
|
|
64
|
+
self[0..position]
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Returns the first character. If a limit is supplied, returns a substring
|
|
68
|
+
# from the beginning of the string until it reaches the limit value. If the
|
|
69
|
+
# given limit is greater than or equal to the string length, returns a copy of self.
|
|
70
|
+
#
|
|
71
|
+
# str = "hello"
|
|
72
|
+
# str.first # => "h"
|
|
73
|
+
# str.first(1) # => "h"
|
|
74
|
+
# str.first(2) # => "he"
|
|
75
|
+
# str.first(0) # => ""
|
|
76
|
+
# str.first(6) # => "hello"
|
|
77
|
+
def first(limit = 1)
|
|
78
|
+
ActiveSupport::Deprecation.warn(
|
|
79
|
+
"Calling String#first with a negative integer limit " \
|
|
80
|
+
"will raise an ArgumentError in Rails 6.1."
|
|
81
|
+
) if limit < 0
|
|
82
|
+
if limit == 0
|
|
83
|
+
""
|
|
84
|
+
elsif limit >= size
|
|
85
|
+
dup
|
|
86
|
+
else
|
|
87
|
+
to(limit - 1)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Returns the last character of the string. If a limit is supplied, returns a substring
|
|
92
|
+
# from the end of the string until it reaches the limit value (counting backwards). If
|
|
93
|
+
# the given limit is greater than or equal to the string length, returns a copy of self.
|
|
94
|
+
#
|
|
95
|
+
# str = "hello"
|
|
96
|
+
# str.last # => "o"
|
|
97
|
+
# str.last(1) # => "o"
|
|
98
|
+
# str.last(2) # => "lo"
|
|
99
|
+
# str.last(0) # => ""
|
|
100
|
+
# str.last(6) # => "hello"
|
|
101
|
+
def last(limit = 1)
|
|
102
|
+
ActiveSupport::Deprecation.warn(
|
|
103
|
+
"Calling String#last with a negative integer limit " \
|
|
104
|
+
"will raise an ArgumentError in Rails 6.1."
|
|
105
|
+
) if limit < 0
|
|
106
|
+
if limit == 0
|
|
107
|
+
""
|
|
108
|
+
elsif limit >= size
|
|
109
|
+
dup
|
|
110
|
+
else
|
|
111
|
+
from(-limit)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "date"
|
|
4
|
+
require "active_support/core_ext/time/calculations"
|
|
5
|
+
|
|
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
|
+
def to_time(form = :local)
|
|
22
|
+
parts = Date._parse(self, false)
|
|
23
|
+
used_keys = %i(year mon mday hour min sec sec_fraction offset)
|
|
24
|
+
return if (parts.keys & used_keys).empty?
|
|
25
|
+
|
|
26
|
+
now = Time.now
|
|
27
|
+
time = Time.new(
|
|
28
|
+
parts.fetch(:year, now.year),
|
|
29
|
+
parts.fetch(:mon, now.month),
|
|
30
|
+
parts.fetch(:mday, now.day),
|
|
31
|
+
parts.fetch(:hour, 0),
|
|
32
|
+
parts.fetch(:min, 0),
|
|
33
|
+
parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0),
|
|
34
|
+
parts.fetch(:offset, form == :utc ? 0 : nil)
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
form == :utc ? time.utc : time.to_time
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Converts a string to a Date value.
|
|
41
|
+
#
|
|
42
|
+
# "1-1-2012".to_date # => Sun, 01 Jan 2012
|
|
43
|
+
# "01/01/2012".to_date # => Sun, 01 Jan 2012
|
|
44
|
+
# "2012-12-13".to_date # => Thu, 13 Dec 2012
|
|
45
|
+
# "12/13/2012".to_date # => ArgumentError: invalid date
|
|
46
|
+
def to_date
|
|
47
|
+
::Date.parse(self, false) unless blank?
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Converts a string to a DateTime value.
|
|
51
|
+
#
|
|
52
|
+
# "1-1-2012".to_datetime # => Sun, 01 Jan 2012 00:00:00 +0000
|
|
53
|
+
# "01/01/2012 23:59:59".to_datetime # => Sun, 01 Jan 2012 23:59:59 +0000
|
|
54
|
+
# "2012-12-13 12:50".to_datetime # => Thu, 13 Dec 2012 12:50:00 +0000
|
|
55
|
+
# "12/13/2012".to_datetime # => ArgumentError: invalid date
|
|
56
|
+
def to_datetime
|
|
57
|
+
::DateTime.parse(self, false) unless blank?
|
|
58
|
+
end
|
|
59
|
+
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,145 @@
|
|
|
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+ after a given <tt>length</tt> if +text+ is longer than <tt>length</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
|
+
# for a total length not exceeding <tt>length</tt>:
|
|
63
|
+
#
|
|
64
|
+
# 'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
|
|
65
|
+
# # => "And they f... (continued)"
|
|
66
|
+
def truncate(truncate_at, options = {})
|
|
67
|
+
return dup unless length > truncate_at
|
|
68
|
+
|
|
69
|
+
omission = options[:omission] || "..."
|
|
70
|
+
length_with_room_for_omission = truncate_at - omission.length
|
|
71
|
+
stop = \
|
|
72
|
+
if options[:separator]
|
|
73
|
+
rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
|
|
74
|
+
else
|
|
75
|
+
length_with_room_for_omission
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
+"#{self[0, stop]}#{omission}"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Truncates +text+ to at most <tt>bytesize</tt> bytes in length without
|
|
82
|
+
# breaking string encoding by splitting multibyte characters or breaking
|
|
83
|
+
# grapheme clusters ("perceptual characters") by truncating at combining
|
|
84
|
+
# characters.
|
|
85
|
+
#
|
|
86
|
+
# >> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".size
|
|
87
|
+
# => 20
|
|
88
|
+
# >> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".bytesize
|
|
89
|
+
# => 80
|
|
90
|
+
# >> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".truncate_bytes(20)
|
|
91
|
+
# => "🔪🔪🔪🔪…"
|
|
92
|
+
#
|
|
93
|
+
# The truncated text ends with the <tt>:omission</tt> string, defaulting
|
|
94
|
+
# to "…", for a total length not exceeding <tt>bytesize</tt>.
|
|
95
|
+
def truncate_bytes(truncate_at, omission: "…")
|
|
96
|
+
omission ||= ""
|
|
97
|
+
|
|
98
|
+
case
|
|
99
|
+
when bytesize <= truncate_at
|
|
100
|
+
dup
|
|
101
|
+
when omission.bytesize > truncate_at
|
|
102
|
+
raise ArgumentError, "Omission #{omission.inspect} is #{omission.bytesize}, larger than the truncation length of #{truncate_at} bytes"
|
|
103
|
+
when omission.bytesize == truncate_at
|
|
104
|
+
omission.dup
|
|
105
|
+
else
|
|
106
|
+
self.class.new.tap do |cut|
|
|
107
|
+
cut_at = truncate_at - omission.bytesize
|
|
108
|
+
|
|
109
|
+
scan(/\X/) do |grapheme|
|
|
110
|
+
if cut.bytesize + grapheme.bytesize <= cut_at
|
|
111
|
+
cut << grapheme
|
|
112
|
+
else
|
|
113
|
+
break
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
cut << omission
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Truncates a given +text+ after a given number of words (<tt>words_count</tt>):
|
|
123
|
+
#
|
|
124
|
+
# 'Once upon a time in a world far far away'.truncate_words(4)
|
|
125
|
+
# # => "Once upon a time..."
|
|
126
|
+
#
|
|
127
|
+
# Pass a string or regexp <tt>:separator</tt> to specify a different separator of words:
|
|
128
|
+
#
|
|
129
|
+
# 'Once<br>upon<br>a<br>time<br>in<br>a<br>world'.truncate_words(5, separator: '<br>')
|
|
130
|
+
# # => "Once<br>upon<br>a<br>time<br>in..."
|
|
131
|
+
#
|
|
132
|
+
# The last characters will be replaced with the <tt>:omission</tt> string (defaults to "..."):
|
|
133
|
+
#
|
|
134
|
+
# 'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)')
|
|
135
|
+
# # => "And they found that many... (continued)"
|
|
136
|
+
def truncate_words(words_count, options = {})
|
|
137
|
+
sep = options[:separator] || /\s+/
|
|
138
|
+
sep = Regexp.escape(sep.to_s) unless Regexp === sep
|
|
139
|
+
if self =~ /\A((?>.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m
|
|
140
|
+
$1 + (options[:omission] || "...")
|
|
141
|
+
else
|
|
142
|
+
dup
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|