activesupport 5.0.7.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1018 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +39 -0
- data/lib/active_support.rb +99 -0
- data/lib/active_support/all.rb +3 -0
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +103 -0
- data/lib/active_support/benchmarkable.rb +49 -0
- data/lib/active_support/builder.rb +6 -0
- data/lib/active_support/cache.rb +701 -0
- data/lib/active_support/cache/file_store.rb +204 -0
- data/lib/active_support/cache/mem_cache_store.rb +207 -0
- data/lib/active_support/cache/memory_store.rb +167 -0
- data/lib/active_support/cache/null_store.rb +41 -0
- data/lib/active_support/cache/strategy/local_cache.rb +172 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
- data/lib/active_support/callbacks.rb +791 -0
- data/lib/active_support/concern.rb +142 -0
- data/lib/active_support/concurrency/latch.rb +26 -0
- data/lib/active_support/concurrency/share_lock.rb +226 -0
- data/lib/active_support/configurable.rb +148 -0
- data/lib/active_support/core_ext.rb +4 -0
- data/lib/active_support/core_ext/array.rb +7 -0
- data/lib/active_support/core_ext/array/access.rb +90 -0
- data/lib/active_support/core_ext/array/conversions.rb +211 -0
- data/lib/active_support/core_ext/array/extract_options.rb +29 -0
- data/lib/active_support/core_ext/array/grouping.rb +107 -0
- data/lib/active_support/core_ext/array/inquiry.rb +17 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
- data/lib/active_support/core_ext/array/wrap.rb +46 -0
- data/lib/active_support/core_ext/benchmark.rb +14 -0
- data/lib/active_support/core_ext/big_decimal.rb +1 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/class.rb +2 -0
- data/lib/active_support/core_ext/class/attribute.rb +128 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -0
- data/lib/active_support/core_ext/class/subclasses.rb +41 -0
- data/lib/active_support/core_ext/date.rb +5 -0
- data/lib/active_support/core_ext/date/acts_like.rb +8 -0
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +143 -0
- data/lib/active_support/core_ext/date/conversions.rb +95 -0
- data/lib/active_support/core_ext/date/zones.rb +6 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time.rb +5 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +14 -0
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +199 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +105 -0
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +146 -0
- data/lib/active_support/core_ext/file.rb +1 -0
- data/lib/active_support/core_ext/file/atomic.rb +68 -0
- data/lib/active_support/core_ext/hash.rb +9 -0
- data/lib/active_support/core_ext/hash/compact.rb +24 -0
- data/lib/active_support/core_ext/hash/conversions.rb +262 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +38 -0
- data/lib/active_support/core_ext/hash/except.rb +22 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +23 -0
- data/lib/active_support/core_ext/hash/keys.rb +170 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -0
- data/lib/active_support/core_ext/hash/slice.rb +48 -0
- data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
- data/lib/active_support/core_ext/integer.rb +3 -0
- data/lib/active_support/core_ext/integer/inflections.rb +29 -0
- data/lib/active_support/core_ext/integer/multiple.rb +10 -0
- data/lib/active_support/core_ext/integer/time.rb +29 -0
- data/lib/active_support/core_ext/kernel.rb +4 -0
- data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/active_support/core_ext/kernel/concern.rb +12 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +3 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +43 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +6 -0
- data/lib/active_support/core_ext/load_error.rb +31 -0
- data/lib/active_support/core_ext/marshal.rb +22 -0
- data/lib/active_support/core_ext/module.rb +12 -0
- data/lib/active_support/core_ext/module/aliasing.rb +74 -0
- data/lib/active_support/core_ext/module/anonymous.rb +28 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +36 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
- data/lib/active_support/core_ext/module/concerning.rb +135 -0
- data/lib/active_support/core_ext/module/delegation.rb +216 -0
- data/lib/active_support/core_ext/module/deprecation.rb +23 -0
- data/lib/active_support/core_ext/module/introspection.rb +68 -0
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
- data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
- data/lib/active_support/core_ext/module/reachable.rb +8 -0
- data/lib/active_support/core_ext/module/remove_method.rb +35 -0
- data/lib/active_support/core_ext/name_error.rb +31 -0
- data/lib/active_support/core_ext/numeric.rb +4 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +64 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +144 -0
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +74 -0
- data/lib/active_support/core_ext/object.rb +14 -0
- data/lib/active_support/core_ext/object/acts_like.rb +10 -0
- data/lib/active_support/core_ext/object/blank.rb +143 -0
- data/lib/active_support/core_ext/object/conversions.rb +4 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
- data/lib/active_support/core_ext/object/duplicable.rb +124 -0
- data/lib/active_support/core_ext/object/inclusion.rb +27 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +28 -0
- data/lib/active_support/core_ext/object/json.rb +205 -0
- data/lib/active_support/core_ext/object/to_param.rb +1 -0
- data/lib/active_support/core_ext/object/to_query.rb +84 -0
- data/lib/active_support/core_ext/object/try.rb +146 -0
- data/lib/active_support/core_ext/object/with_options.rb +69 -0
- data/lib/active_support/core_ext/range.rb +4 -0
- data/lib/active_support/core_ext/range/conversions.rb +31 -0
- data/lib/active_support/core_ext/range/each.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +8 -0
- data/lib/active_support/core_ext/regexp.rb +5 -0
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string.rb +13 -0
- data/lib/active_support/core_ext/string/access.rb +104 -0
- data/lib/active_support/core_ext/string/behavior.rb +6 -0
- data/lib/active_support/core_ext/string/conversions.rb +57 -0
- data/lib/active_support/core_ext/string/exclude.rb +11 -0
- data/lib/active_support/core_ext/string/filters.rb +102 -0
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +244 -0
- data/lib/active_support/core_ext/string/inquiry.rb +13 -0
- data/lib/active_support/core_ext/string/multibyte.rb +53 -0
- data/lib/active_support/core_ext/string/output_safety.rb +260 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/active_support/core_ext/string/strip.rb +23 -0
- data/lib/active_support/core_ext/string/zones.rb +14 -0
- data/lib/active_support/core_ext/struct.rb +3 -0
- data/lib/active_support/core_ext/time.rb +5 -0
- data/lib/active_support/core_ext/time/acts_like.rb +8 -0
- data/lib/active_support/core_ext/time/calculations.rb +290 -0
- data/lib/active_support/core_ext/time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/time/conversions.rb +67 -0
- data/lib/active_support/core_ext/time/marshal.rb +3 -0
- data/lib/active_support/core_ext/time/zones.rb +111 -0
- data/lib/active_support/core_ext/uri.rb +24 -0
- data/lib/active_support/dependencies.rb +755 -0
- data/lib/active_support/dependencies/autoload.rb +77 -0
- data/lib/active_support/dependencies/interlock.rb +55 -0
- data/lib/active_support/deprecation.rb +43 -0
- data/lib/active_support/deprecation/behaviors.rb +90 -0
- data/lib/active_support/deprecation/instance_delegator.rb +37 -0
- data/lib/active_support/deprecation/method_wrappers.rb +70 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +149 -0
- data/lib/active_support/deprecation/reporting.rb +112 -0
- data/lib/active_support/descendants_tracker.rb +60 -0
- data/lib/active_support/duration.rb +235 -0
- data/lib/active_support/duration/iso8601_parser.rb +122 -0
- data/lib/active_support/duration/iso8601_serializer.rb +51 -0
- data/lib/active_support/evented_file_update_checker.rb +199 -0
- data/lib/active_support/execution_wrapper.rb +126 -0
- data/lib/active_support/executor.rb +6 -0
- data/lib/active_support/file_update_checker.rb +157 -0
- data/lib/active_support/gem_version.rb +15 -0
- data/lib/active_support/gzip.rb +36 -0
- data/lib/active_support/hash_with_indifferent_access.rb +329 -0
- data/lib/active_support/i18n.rb +13 -0
- data/lib/active_support/i18n_railtie.rb +115 -0
- data/lib/active_support/inflections.rb +70 -0
- data/lib/active_support/inflector.rb +7 -0
- data/lib/active_support/inflector/inflections.rb +242 -0
- data/lib/active_support/inflector/methods.rb +390 -0
- data/lib/active_support/inflector/transliterate.rb +112 -0
- data/lib/active_support/json.rb +2 -0
- data/lib/active_support/json/decoding.rb +74 -0
- data/lib/active_support/json/encoding.rb +127 -0
- data/lib/active_support/key_generator.rb +71 -0
- data/lib/active_support/lazy_load_hooks.rb +76 -0
- data/lib/active_support/locale/en.yml +135 -0
- data/lib/active_support/log_subscriber.rb +109 -0
- data/lib/active_support/log_subscriber/test_helper.rb +104 -0
- data/lib/active_support/logger.rb +106 -0
- data/lib/active_support/logger_silence.rb +28 -0
- data/lib/active_support/logger_thread_safe_level.rb +31 -0
- data/lib/active_support/message_encryptor.rb +114 -0
- data/lib/active_support/message_verifier.rb +134 -0
- data/lib/active_support/multibyte.rb +21 -0
- data/lib/active_support/multibyte/chars.rb +231 -0
- data/lib/active_support/multibyte/unicode.rb +413 -0
- data/lib/active_support/notifications.rb +212 -0
- data/lib/active_support/notifications/fanout.rb +157 -0
- data/lib/active_support/notifications/instrumenter.rb +91 -0
- data/lib/active_support/number_helper.rb +368 -0
- data/lib/active_support/number_helper/number_converter.rb +182 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
- data/lib/active_support/option_merger.rb +25 -0
- data/lib/active_support/ordered_hash.rb +48 -0
- data/lib/active_support/ordered_options.rb +81 -0
- data/lib/active_support/per_thread_registry.rb +58 -0
- data/lib/active_support/proxy_object.rb +13 -0
- data/lib/active_support/rails.rb +27 -0
- data/lib/active_support/railtie.rb +51 -0
- data/lib/active_support/reloader.rb +129 -0
- data/lib/active_support/rescuable.rb +173 -0
- data/lib/active_support/security_utils.rb +27 -0
- data/lib/active_support/string_inquirer.rb +26 -0
- data/lib/active_support/subscriber.rb +120 -0
- data/lib/active_support/tagged_logging.rb +77 -0
- data/lib/active_support/test_case.rb +88 -0
- data/lib/active_support/testing/assertions.rb +99 -0
- data/lib/active_support/testing/autorun.rb +5 -0
- data/lib/active_support/testing/constant_lookup.rb +50 -0
- data/lib/active_support/testing/declarative.rb +26 -0
- data/lib/active_support/testing/deprecation.rb +36 -0
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +115 -0
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/setup_and_teardown.rb +50 -0
- data/lib/active_support/testing/stream.rb +42 -0
- data/lib/active_support/testing/tagged_logging.rb +25 -0
- data/lib/active_support/testing/time_helpers.rb +136 -0
- data/lib/active_support/time.rb +18 -0
- data/lib/active_support/time_with_zone.rb +511 -0
- data/lib/active_support/values/time_zone.rb +484 -0
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +8 -0
- data/lib/active_support/xml_mini.rb +209 -0
- data/lib/active_support/xml_mini/jdom.rb +181 -0
- data/lib/active_support/xml_mini/libxml.rb +77 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +82 -0
- data/lib/active_support/xml_mini/nokogiri.rb +81 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +85 -0
- data/lib/active_support/xml_mini/rexml.rb +128 -0
- metadata +349 -0
@@ -0,0 +1,23 @@
|
|
1
|
+
class Module
|
2
|
+
# deprecate :foo
|
3
|
+
# deprecate bar: 'message'
|
4
|
+
# deprecate :foo, :bar, baz: 'warning!', qux: 'gone!'
|
5
|
+
#
|
6
|
+
# You can also use custom deprecator instance:
|
7
|
+
#
|
8
|
+
# deprecate :foo, deprecator: MyLib::Deprecator.new
|
9
|
+
# deprecate :foo, bar: "warning!", deprecator: MyLib::Deprecator.new
|
10
|
+
#
|
11
|
+
# \Custom deprecators must respond to <tt>deprecation_warning(deprecated_method_name, message, caller_backtrace)</tt>
|
12
|
+
# method where you can implement your custom warning behavior.
|
13
|
+
#
|
14
|
+
# class MyLib::Deprecator
|
15
|
+
# def deprecation_warning(deprecated_method_name, message, caller_backtrace = nil)
|
16
|
+
# message = "#{deprecated_method_name} is deprecated and will be removed from MyLibrary | #{message}"
|
17
|
+
# Kernel.warn message
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
def deprecate(*method_names)
|
21
|
+
ActiveSupport::Deprecation.deprecate_methods(self, *method_names)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
|
3
|
+
class Module
|
4
|
+
# Returns the name of the module containing this one.
|
5
|
+
#
|
6
|
+
# M::N.parent_name # => "M"
|
7
|
+
def parent_name
|
8
|
+
if defined?(@parent_name)
|
9
|
+
@parent_name
|
10
|
+
else
|
11
|
+
parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
|
12
|
+
@parent_name = parent_name unless frozen?
|
13
|
+
parent_name
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns the module which contains this one according to its name.
|
18
|
+
#
|
19
|
+
# module M
|
20
|
+
# module N
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
# X = M::N
|
24
|
+
#
|
25
|
+
# M::N.parent # => M
|
26
|
+
# X.parent # => M
|
27
|
+
#
|
28
|
+
# The parent of top-level and anonymous modules is Object.
|
29
|
+
#
|
30
|
+
# M.parent # => Object
|
31
|
+
# Module.new.parent # => Object
|
32
|
+
def parent
|
33
|
+
parent_name ? ActiveSupport::Inflector.constantize(parent_name) : Object
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns all the parents of this module according to its name, ordered from
|
37
|
+
# nested outwards. The receiver is not contained within the result.
|
38
|
+
#
|
39
|
+
# module M
|
40
|
+
# module N
|
41
|
+
# end
|
42
|
+
# end
|
43
|
+
# X = M::N
|
44
|
+
#
|
45
|
+
# M.parents # => [Object]
|
46
|
+
# M::N.parents # => [M, Object]
|
47
|
+
# X.parents # => [M, Object]
|
48
|
+
def parents
|
49
|
+
parents = []
|
50
|
+
if parent_name
|
51
|
+
parts = parent_name.split('::')
|
52
|
+
until parts.empty?
|
53
|
+
parents << ActiveSupport::Inflector.constantize(parts * '::')
|
54
|
+
parts.pop
|
55
|
+
end
|
56
|
+
end
|
57
|
+
parents << Object unless parents.include? Object
|
58
|
+
parents
|
59
|
+
end
|
60
|
+
|
61
|
+
def local_constants #:nodoc:
|
62
|
+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
63
|
+
Module#local_constants is deprecated and will be removed in Rails 5.1.
|
64
|
+
Use Module#constants(false) instead.
|
65
|
+
MSG
|
66
|
+
constants(false)
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Allows code reuse in the methods below without polluting Module.
|
5
|
+
#++
|
6
|
+
|
7
|
+
module ActiveSupport
|
8
|
+
module QualifiedConstUtils
|
9
|
+
def self.raise_if_absolute(path)
|
10
|
+
raise NameError.new("wrong constant name #$&") if path =~ /\A::[^:]+/
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.names(path)
|
14
|
+
path.split('::')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Extends the API for constants to be able to deal with qualified names. Arguments
|
21
|
+
# are assumed to be relative to the receiver.
|
22
|
+
#
|
23
|
+
#--
|
24
|
+
# Qualified names are required to be relative because we are extending existing
|
25
|
+
# methods that expect constant names, ie, relative paths of length 1. For example,
|
26
|
+
# Object.const_get('::String') raises NameError and so does qualified_const_get.
|
27
|
+
#++
|
28
|
+
class Module
|
29
|
+
def qualified_const_defined?(path, search_parents=true)
|
30
|
+
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
|
31
|
+
Module#qualified_const_defined? is deprecated in favour of the builtin
|
32
|
+
Module#const_defined? and will be removed in Rails 5.1.
|
33
|
+
MESSAGE
|
34
|
+
|
35
|
+
ActiveSupport::QualifiedConstUtils.raise_if_absolute(path)
|
36
|
+
|
37
|
+
ActiveSupport::QualifiedConstUtils.names(path).inject(self) do |mod, name|
|
38
|
+
return unless mod.const_defined?(name, search_parents)
|
39
|
+
mod.const_get(name)
|
40
|
+
end
|
41
|
+
return true
|
42
|
+
end
|
43
|
+
|
44
|
+
def qualified_const_get(path)
|
45
|
+
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
|
46
|
+
Module#qualified_const_get is deprecated in favour of the builtin
|
47
|
+
Module#const_get and will be removed in Rails 5.1.
|
48
|
+
MESSAGE
|
49
|
+
|
50
|
+
ActiveSupport::QualifiedConstUtils.raise_if_absolute(path)
|
51
|
+
|
52
|
+
ActiveSupport::QualifiedConstUtils.names(path).inject(self) do |mod, name|
|
53
|
+
mod.const_get(name)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def qualified_const_set(path, value)
|
58
|
+
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
|
59
|
+
Module#qualified_const_set is deprecated in favour of the builtin
|
60
|
+
Module#const_set and will be removed in Rails 5.1.
|
61
|
+
MESSAGE
|
62
|
+
|
63
|
+
ActiveSupport::QualifiedConstUtils.raise_if_absolute(path)
|
64
|
+
|
65
|
+
const_name = path.demodulize
|
66
|
+
mod_name = path.deconstantize
|
67
|
+
mod = mod_name.empty? ? self : const_get(mod_name)
|
68
|
+
mod.const_set(const_name, value)
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class Module
|
2
|
+
# Removes the named method, if it exists.
|
3
|
+
def remove_possible_method(method)
|
4
|
+
if method_defined?(method) || private_method_defined?(method)
|
5
|
+
undef_method(method)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
# Removes the named singleton method, if it exists.
|
10
|
+
def remove_possible_singleton_method(method)
|
11
|
+
singleton_class.instance_eval do
|
12
|
+
remove_possible_method(method)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Replaces the existing method definition, if there is one, with the passed
|
17
|
+
# block as its body.
|
18
|
+
def redefine_method(method, &block)
|
19
|
+
visibility = method_visibility(method)
|
20
|
+
remove_possible_method(method)
|
21
|
+
define_method(method, &block)
|
22
|
+
send(visibility, method)
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_visibility(method) # :nodoc:
|
26
|
+
case
|
27
|
+
when private_method_defined?(method)
|
28
|
+
:private
|
29
|
+
when protected_method_defined?(method)
|
30
|
+
:protected
|
31
|
+
else
|
32
|
+
:public
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class NameError
|
2
|
+
# Extract the name of the missing constant from the exception message.
|
3
|
+
#
|
4
|
+
# begin
|
5
|
+
# HelloWorld
|
6
|
+
# rescue NameError => e
|
7
|
+
# e.missing_name
|
8
|
+
# end
|
9
|
+
# # => "HelloWorld"
|
10
|
+
def missing_name
|
11
|
+
if /undefined local variable or method/ !~ message
|
12
|
+
$1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ message
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Was this exception raised because the given name was missing?
|
17
|
+
#
|
18
|
+
# begin
|
19
|
+
# HelloWorld
|
20
|
+
# rescue NameError => e
|
21
|
+
# e.missing_name?("HelloWorld")
|
22
|
+
# end
|
23
|
+
# # => true
|
24
|
+
def missing_name?(name)
|
25
|
+
if name.is_a? Symbol
|
26
|
+
self.name == name
|
27
|
+
else
|
28
|
+
missing_name == name.to_s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class Numeric
|
2
|
+
KILOBYTE = 1024
|
3
|
+
MEGABYTE = KILOBYTE * 1024
|
4
|
+
GIGABYTE = MEGABYTE * 1024
|
5
|
+
TERABYTE = GIGABYTE * 1024
|
6
|
+
PETABYTE = TERABYTE * 1024
|
7
|
+
EXABYTE = PETABYTE * 1024
|
8
|
+
|
9
|
+
# Enables the use of byte calculations and declarations, like 45.bytes + 2.6.megabytes
|
10
|
+
#
|
11
|
+
# 2.bytes # => 2
|
12
|
+
def bytes
|
13
|
+
self
|
14
|
+
end
|
15
|
+
alias :byte :bytes
|
16
|
+
|
17
|
+
# Returns the number of bytes equivalent to the kilobytes provided.
|
18
|
+
#
|
19
|
+
# 2.kilobytes # => 2048
|
20
|
+
def kilobytes
|
21
|
+
self * KILOBYTE
|
22
|
+
end
|
23
|
+
alias :kilobyte :kilobytes
|
24
|
+
|
25
|
+
# Returns the number of bytes equivalent to the megabytes provided.
|
26
|
+
#
|
27
|
+
# 2.megabytes # => 2_097_152
|
28
|
+
def megabytes
|
29
|
+
self * MEGABYTE
|
30
|
+
end
|
31
|
+
alias :megabyte :megabytes
|
32
|
+
|
33
|
+
# Returns the number of bytes equivalent to the gigabytes provided.
|
34
|
+
#
|
35
|
+
# 2.gigabytes # => 2_147_483_648
|
36
|
+
def gigabytes
|
37
|
+
self * GIGABYTE
|
38
|
+
end
|
39
|
+
alias :gigabyte :gigabytes
|
40
|
+
|
41
|
+
# Returns the number of bytes equivalent to the terabytes provided.
|
42
|
+
#
|
43
|
+
# 2.terabytes # => 2_199_023_255_552
|
44
|
+
def terabytes
|
45
|
+
self * TERABYTE
|
46
|
+
end
|
47
|
+
alias :terabyte :terabytes
|
48
|
+
|
49
|
+
# Returns the number of bytes equivalent to the petabytes provided.
|
50
|
+
#
|
51
|
+
# 2.petabytes # => 2_251_799_813_685_248
|
52
|
+
def petabytes
|
53
|
+
self * PETABYTE
|
54
|
+
end
|
55
|
+
alias :petabyte :petabytes
|
56
|
+
|
57
|
+
# Returns the number of bytes equivalent to the exabytes provided.
|
58
|
+
#
|
59
|
+
# 2.exabytes # => 2_305_843_009_213_693_952
|
60
|
+
def exabytes
|
61
|
+
self * EXABYTE
|
62
|
+
end
|
63
|
+
alias :exabyte :exabytes
|
64
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'active_support/core_ext/big_decimal/conversions'
|
2
|
+
require 'active_support/number_helper'
|
3
|
+
require 'active_support/core_ext/module/deprecation'
|
4
|
+
|
5
|
+
module ActiveSupport::NumericWithFormat
|
6
|
+
|
7
|
+
# Provides options for converting numbers into formatted strings.
|
8
|
+
# Options are provided for phone numbers, currency, percentage,
|
9
|
+
# precision, positional notation, file size and pretty printing.
|
10
|
+
#
|
11
|
+
# ==== Options
|
12
|
+
#
|
13
|
+
# For details on which formats use which options, see ActiveSupport::NumberHelper
|
14
|
+
#
|
15
|
+
# ==== Examples
|
16
|
+
#
|
17
|
+
# Phone Numbers:
|
18
|
+
# 5551234.to_s(:phone) # => "555-1234"
|
19
|
+
# 1235551234.to_s(:phone) # => "123-555-1234"
|
20
|
+
# 1235551234.to_s(:phone, area_code: true) # => "(123) 555-1234"
|
21
|
+
# 1235551234.to_s(:phone, delimiter: ' ') # => "123 555 1234"
|
22
|
+
# 1235551234.to_s(:phone, area_code: true, extension: 555) # => "(123) 555-1234 x 555"
|
23
|
+
# 1235551234.to_s(:phone, country_code: 1) # => "+1-123-555-1234"
|
24
|
+
# 1235551234.to_s(:phone, country_code: 1, extension: 1343, delimiter: '.')
|
25
|
+
# # => "+1.123.555.1234 x 1343"
|
26
|
+
#
|
27
|
+
# Currency:
|
28
|
+
# 1234567890.50.to_s(:currency) # => "$1,234,567,890.50"
|
29
|
+
# 1234567890.506.to_s(:currency) # => "$1,234,567,890.51"
|
30
|
+
# 1234567890.506.to_s(:currency, precision: 3) # => "$1,234,567,890.506"
|
31
|
+
# 1234567890.506.to_s(:currency, locale: :fr) # => "1 234 567 890,51 €"
|
32
|
+
# -1234567890.50.to_s(:currency, negative_format: '(%u%n)')
|
33
|
+
# # => "($1,234,567,890.50)"
|
34
|
+
# 1234567890.50.to_s(:currency, unit: '£', separator: ',', delimiter: '')
|
35
|
+
# # => "£1234567890,50"
|
36
|
+
# 1234567890.50.to_s(:currency, unit: '£', separator: ',', delimiter: '', format: '%n %u')
|
37
|
+
# # => "1234567890,50 £"
|
38
|
+
#
|
39
|
+
# Percentage:
|
40
|
+
# 100.to_s(:percentage) # => "100.000%"
|
41
|
+
# 100.to_s(:percentage, precision: 0) # => "100%"
|
42
|
+
# 1000.to_s(:percentage, delimiter: '.', separator: ',') # => "1.000,000%"
|
43
|
+
# 302.24398923423.to_s(:percentage, precision: 5) # => "302.24399%"
|
44
|
+
# 1000.to_s(:percentage, locale: :fr) # => "1 000,000%"
|
45
|
+
# 100.to_s(:percentage, format: '%n %') # => "100.000 %"
|
46
|
+
#
|
47
|
+
# Delimited:
|
48
|
+
# 12345678.to_s(:delimited) # => "12,345,678"
|
49
|
+
# 12345678.05.to_s(:delimited) # => "12,345,678.05"
|
50
|
+
# 12345678.to_s(:delimited, delimiter: '.') # => "12.345.678"
|
51
|
+
# 12345678.to_s(:delimited, delimiter: ',') # => "12,345,678"
|
52
|
+
# 12345678.05.to_s(:delimited, separator: ' ') # => "12,345,678 05"
|
53
|
+
# 12345678.05.to_s(:delimited, locale: :fr) # => "12 345 678,05"
|
54
|
+
# 98765432.98.to_s(:delimited, delimiter: ' ', separator: ',')
|
55
|
+
# # => "98 765 432,98"
|
56
|
+
#
|
57
|
+
# Rounded:
|
58
|
+
# 111.2345.to_s(:rounded) # => "111.235"
|
59
|
+
# 111.2345.to_s(:rounded, precision: 2) # => "111.23"
|
60
|
+
# 13.to_s(:rounded, precision: 5) # => "13.00000"
|
61
|
+
# 389.32314.to_s(:rounded, precision: 0) # => "389"
|
62
|
+
# 111.2345.to_s(:rounded, significant: true) # => "111"
|
63
|
+
# 111.2345.to_s(:rounded, precision: 1, significant: true) # => "100"
|
64
|
+
# 13.to_s(:rounded, precision: 5, significant: true) # => "13.000"
|
65
|
+
# 111.234.to_s(:rounded, locale: :fr) # => "111,234"
|
66
|
+
# 13.to_s(:rounded, precision: 5, significant: true, strip_insignificant_zeros: true)
|
67
|
+
# # => "13"
|
68
|
+
# 389.32314.to_s(:rounded, precision: 4, significant: true) # => "389.3"
|
69
|
+
# 1111.2345.to_s(:rounded, precision: 2, separator: ',', delimiter: '.')
|
70
|
+
# # => "1.111,23"
|
71
|
+
#
|
72
|
+
# Human-friendly size in Bytes:
|
73
|
+
# 123.to_s(:human_size) # => "123 Bytes"
|
74
|
+
# 1234.to_s(:human_size) # => "1.21 KB"
|
75
|
+
# 12345.to_s(:human_size) # => "12.1 KB"
|
76
|
+
# 1234567.to_s(:human_size) # => "1.18 MB"
|
77
|
+
# 1234567890.to_s(:human_size) # => "1.15 GB"
|
78
|
+
# 1234567890123.to_s(:human_size) # => "1.12 TB"
|
79
|
+
# 1234567890123456.to_s(:human_size) # => "1.1 PB"
|
80
|
+
# 1234567890123456789.to_s(:human_size) # => "1.07 EB"
|
81
|
+
# 1234567.to_s(:human_size, precision: 2) # => "1.2 MB"
|
82
|
+
# 483989.to_s(:human_size, precision: 2) # => "470 KB"
|
83
|
+
# 1234567.to_s(:human_size, precision: 2, separator: ',') # => "1,2 MB"
|
84
|
+
# 1234567890123.to_s(:human_size, precision: 5) # => "1.1228 TB"
|
85
|
+
# 524288000.to_s(:human_size, precision: 5) # => "500 MB"
|
86
|
+
#
|
87
|
+
# Human-friendly format:
|
88
|
+
# 123.to_s(:human) # => "123"
|
89
|
+
# 1234.to_s(:human) # => "1.23 Thousand"
|
90
|
+
# 12345.to_s(:human) # => "12.3 Thousand"
|
91
|
+
# 1234567.to_s(:human) # => "1.23 Million"
|
92
|
+
# 1234567890.to_s(:human) # => "1.23 Billion"
|
93
|
+
# 1234567890123.to_s(:human) # => "1.23 Trillion"
|
94
|
+
# 1234567890123456.to_s(:human) # => "1.23 Quadrillion"
|
95
|
+
# 1234567890123456789.to_s(:human) # => "1230 Quadrillion"
|
96
|
+
# 489939.to_s(:human, precision: 2) # => "490 Thousand"
|
97
|
+
# 489939.to_s(:human, precision: 4) # => "489.9 Thousand"
|
98
|
+
# 1234567.to_s(:human, precision: 4,
|
99
|
+
# significant: false) # => "1.2346 Million"
|
100
|
+
# 1234567.to_s(:human, precision: 1,
|
101
|
+
# separator: ',',
|
102
|
+
# significant: false) # => "1,2 Million"
|
103
|
+
def to_s(format = nil, options = nil)
|
104
|
+
case format
|
105
|
+
when nil
|
106
|
+
super()
|
107
|
+
when Integer, String
|
108
|
+
super(format)
|
109
|
+
when :phone
|
110
|
+
return ActiveSupport::NumberHelper.number_to_phone(self, options || {})
|
111
|
+
when :currency
|
112
|
+
return ActiveSupport::NumberHelper.number_to_currency(self, options || {})
|
113
|
+
when :percentage
|
114
|
+
return ActiveSupport::NumberHelper.number_to_percentage(self, options || {})
|
115
|
+
when :delimited
|
116
|
+
return ActiveSupport::NumberHelper.number_to_delimited(self, options || {})
|
117
|
+
when :rounded
|
118
|
+
return ActiveSupport::NumberHelper.number_to_rounded(self, options || {})
|
119
|
+
when :human
|
120
|
+
return ActiveSupport::NumberHelper.number_to_human(self, options || {})
|
121
|
+
when :human_size
|
122
|
+
return ActiveSupport::NumberHelper.number_to_human_size(self, options || {})
|
123
|
+
when Symbol
|
124
|
+
super()
|
125
|
+
else
|
126
|
+
super(format)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def to_formatted_s(*args)
|
131
|
+
to_s(*args)
|
132
|
+
end
|
133
|
+
deprecate to_formatted_s: :to_s
|
134
|
+
end
|
135
|
+
|
136
|
+
# Ruby 2.4+ unifies Fixnum & Bignum into Integer.
|
137
|
+
if 0.class == Integer
|
138
|
+
Integer.prepend ActiveSupport::NumericWithFormat
|
139
|
+
else
|
140
|
+
Fixnum.prepend ActiveSupport::NumericWithFormat
|
141
|
+
Bignum.prepend ActiveSupport::NumericWithFormat
|
142
|
+
end
|
143
|
+
Float.prepend ActiveSupport::NumericWithFormat
|
144
|
+
BigDecimal.prepend ActiveSupport::NumericWithFormat
|