activesupport 4.0.13 → 4.2.11.3
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 +5 -5
- data/CHANGELOG.md +406 -418
- data/MIT-LICENSE +1 -1
- data/README.rdoc +7 -2
- data/lib/active_support/backtrace_cleaner.rb +8 -8
- data/lib/active_support/benchmarkable.rb +0 -10
- data/lib/active_support/cache/file_store.rb +32 -22
- data/lib/active_support/cache/mem_cache_store.rb +5 -7
- data/lib/active_support/cache/memory_store.rb +1 -0
- data/lib/active_support/cache/strategy/local_cache.rb +11 -30
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
- data/lib/active_support/cache.rb +75 -41
- data/lib/active_support/callbacks.rb +482 -261
- data/lib/active_support/concern.rb +23 -7
- data/lib/active_support/configurable.rb +1 -1
- data/lib/active_support/core_ext/array/access.rb +11 -1
- data/lib/active_support/core_ext/array/conversions.rb +2 -17
- data/lib/active_support/core_ext/array/grouping.rb +29 -12
- data/lib/active_support/core_ext/array/prepend_and_append.rb +2 -2
- data/lib/active_support/core_ext/array.rb +0 -1
- data/lib/active_support/core_ext/big_decimal/conversions.rb +0 -15
- data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +16 -0
- data/lib/active_support/core_ext/class/attribute.rb +1 -2
- data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -170
- data/lib/active_support/core_ext/class/delegating_attributes.rb +13 -8
- data/lib/active_support/core_ext/class/subclasses.rb +0 -2
- data/lib/active_support/core_ext/class.rb +0 -1
- data/lib/active_support/core_ext/date/calculations.rb +10 -0
- data/lib/active_support/core_ext/date/conversions.rb +9 -1
- data/lib/active_support/core_ext/date/zones.rb +2 -33
- data/lib/active_support/core_ext/date_and_time/calculations.rb +41 -11
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +15 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +41 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +45 -22
- data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +4 -2
- data/lib/active_support/core_ext/date_time/zones.rb +3 -21
- data/lib/active_support/core_ext/date_time.rb +1 -0
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +17 -1
- data/lib/active_support/core_ext/file/atomic.rb +1 -1
- data/lib/active_support/core_ext/hash/compact.rb +24 -0
- data/lib/active_support/core_ext/hash/conversions.rb +9 -8
- data/lib/active_support/core_ext/hash/except.rb +8 -2
- data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -0
- data/lib/active_support/core_ext/hash/keys.rb +25 -19
- data/lib/active_support/core_ext/hash/slice.rb +8 -2
- data/lib/active_support/core_ext/hash/transform_values.rb +23 -0
- data/lib/active_support/core_ext/hash.rb +2 -1
- data/lib/active_support/core_ext/integer/time.rb +0 -15
- data/lib/active_support/core_ext/kernel/concern.rb +10 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +1 -1
- data/lib/active_support/core_ext/kernel/reporting.rb +13 -2
- data/lib/active_support/core_ext/kernel.rb +3 -2
- data/lib/active_support/core_ext/load_error.rb +4 -1
- data/lib/active_support/core_ext/marshal.rb +8 -5
- data/lib/active_support/core_ext/module/aliasing.rb +2 -2
- data/lib/active_support/core_ext/module/attr_internal.rb +2 -1
- data/lib/active_support/core_ext/module/attribute_accessors.rb +160 -14
- data/lib/active_support/core_ext/module/concerning.rb +135 -0
- data/lib/active_support/core_ext/module/delegation.rb +53 -25
- data/lib/active_support/core_ext/module/deprecation.rb +0 -2
- data/lib/active_support/core_ext/module/introspection.rb +0 -16
- data/lib/active_support/core_ext/module/method_transplanting.rb +13 -0
- data/lib/active_support/core_ext/module.rb +1 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +11 -3
- data/lib/active_support/core_ext/numeric/time.rb +4 -29
- data/lib/active_support/core_ext/object/blank.rb +44 -18
- data/lib/active_support/core_ext/object/deep_dup.rb +6 -6
- data/lib/active_support/core_ext/object/duplicable.rb +72 -33
- data/lib/active_support/core_ext/object/inclusion.rb +16 -15
- data/lib/active_support/core_ext/object/itself.rb +15 -0
- data/lib/active_support/core_ext/object/json.rb +197 -0
- data/lib/active_support/core_ext/object/to_query.rb +14 -6
- data/lib/active_support/core_ext/object/try.rb +36 -14
- data/lib/active_support/core_ext/object/with_options.rb +30 -3
- data/lib/active_support/core_ext/object.rb +2 -1
- data/lib/active_support/core_ext/string/access.rb +35 -35
- data/lib/active_support/core_ext/string/conversions.rb +10 -9
- data/lib/active_support/core_ext/string/exclude.rb +3 -3
- data/lib/active_support/core_ext/string/filters.rb +51 -3
- data/lib/active_support/core_ext/string/inflections.rb +15 -10
- data/lib/active_support/core_ext/string/output_safety.rb +97 -33
- data/lib/active_support/core_ext/string/zones.rb +1 -0
- data/lib/active_support/core_ext/thread.rb +12 -5
- data/lib/active_support/core_ext/time/calculations.rb +47 -68
- data/lib/active_support/core_ext/time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/time/conversions.rb +4 -2
- data/lib/active_support/core_ext/time/zones.rb +2 -20
- data/lib/active_support/core_ext/time.rb +1 -0
- data/lib/active_support/core_ext.rb +0 -1
- data/lib/active_support/dependencies/autoload.rb +1 -1
- data/lib/active_support/dependencies.rb +64 -25
- data/lib/active_support/deprecation/behaviors.rb +4 -4
- data/lib/active_support/deprecation.rb +4 -4
- data/lib/active_support/duration.rb +55 -11
- data/lib/active_support/file_update_checker.rb +1 -1
- data/lib/active_support/gem_version.rb +15 -0
- data/lib/active_support/hash_with_indifferent_access.rb +39 -11
- data/lib/active_support/i18n.rb +4 -4
- data/lib/active_support/i18n_railtie.rb +1 -7
- data/lib/active_support/inflections.rb +6 -1
- data/lib/active_support/inflector/inflections.rb +19 -19
- data/lib/active_support/inflector/methods.rb +66 -25
- data/lib/active_support/json/decoding.rb +15 -22
- data/lib/active_support/json/encoding.rb +125 -286
- data/lib/active_support/key_generator.rb +8 -10
- data/lib/active_support/lazy_load_hooks.rb +1 -1
- data/lib/active_support/log_subscriber/test_helper.rb +1 -1
- data/lib/active_support/logger.rb +51 -1
- data/lib/active_support/logger_silence.rb +7 -4
- data/lib/active_support/logger_thread_safe_level.rb +32 -0
- data/lib/active_support/message_encryptor.rb +14 -6
- data/lib/active_support/message_verifier.rb +16 -12
- data/lib/active_support/multibyte/chars.rb +2 -3
- data/lib/active_support/multibyte/unicode.rb +46 -58
- data/lib/active_support/notifications/fanout.rb +12 -7
- data/lib/active_support/notifications/instrumenter.rb +2 -1
- data/lib/active_support/notifications.rb +11 -6
- data/lib/active_support/number_helper/number_converter.rb +182 -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 +23 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +66 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +58 -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 +49 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +87 -0
- data/lib/active_support/number_helper.rb +32 -324
- data/lib/active_support/ordered_options.rb +8 -0
- data/lib/active_support/per_thread_registry.rb +13 -10
- data/lib/active_support/security_utils.rb +27 -0
- data/lib/active_support/subscriber.rb +35 -3
- data/lib/active_support/test_case.rb +52 -19
- data/lib/active_support/testing/assertions.rb +1 -31
- data/lib/active_support/testing/autorun.rb +2 -2
- data/lib/active_support/testing/constant_lookup.rb +1 -5
- data/lib/active_support/testing/declarative.rb +7 -21
- data/lib/active_support/testing/isolation.rb +29 -69
- data/lib/active_support/testing/setup_and_teardown.rb +17 -2
- data/lib/active_support/testing/tagged_logging.rb +2 -2
- data/lib/active_support/testing/time_helpers.rb +134 -0
- data/lib/active_support/time.rb +0 -2
- data/lib/active_support/time_with_zone.rb +60 -40
- data/lib/active_support/values/time_zone.rb +101 -101
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +4 -7
- data/lib/active_support/xml_mini/jdom.rb +6 -5
- data/lib/active_support/xml_mini/libxml.rb +1 -3
- data/lib/active_support/xml_mini/libxmlsax.rb +1 -4
- data/lib/active_support/xml_mini/nokogiri.rb +1 -3
- data/lib/active_support/xml_mini/nokogirisax.rb +1 -3
- data/lib/active_support/xml_mini/rexml.rb +7 -8
- data/lib/active_support/xml_mini.rb +33 -15
- data/lib/active_support.rb +27 -2
- metadata +43 -43
- data/lib/active_support/basic_object.rb +0 -11
- data/lib/active_support/buffered_logger.rb +0 -21
- data/lib/active_support/core_ext/array/uniq_by.rb +0 -19
- data/lib/active_support/core_ext/hash/diff.rb +0 -14
- data/lib/active_support/core_ext/logger.rb +0 -67
- data/lib/active_support/core_ext/object/to_json.rb +0 -27
- data/lib/active_support/core_ext/proc.rb +0 -17
- data/lib/active_support/core_ext/string/encoding.rb +0 -8
- data/lib/active_support/file_watcher.rb +0 -36
- data/lib/active_support/json/variable.rb +0 -18
- data/lib/active_support/testing/pending.rb +0 -14
@@ -26,7 +26,7 @@ module ActiveSupport
|
|
26
26
|
# scope :disabled, -> { where(disabled: true) }
|
27
27
|
# end
|
28
28
|
#
|
29
|
-
#
|
29
|
+
# class_methods do
|
30
30
|
# ...
|
31
31
|
# end
|
32
32
|
# end
|
@@ -95,32 +95,48 @@ module ActiveSupport
|
|
95
95
|
# end
|
96
96
|
#
|
97
97
|
# class Host
|
98
|
-
# include Bar # works, Bar takes care
|
98
|
+
# include Bar # It works, now Bar takes care of its dependencies
|
99
99
|
# end
|
100
100
|
module Concern
|
101
|
+
class MultipleIncludedBlocks < StandardError #:nodoc:
|
102
|
+
def initialize
|
103
|
+
super "Cannot define multiple 'included' blocks for a Concern"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
101
107
|
def self.extended(base) #:nodoc:
|
102
|
-
base.instance_variable_set(
|
108
|
+
base.instance_variable_set(:@_dependencies, [])
|
103
109
|
end
|
104
110
|
|
105
111
|
def append_features(base)
|
106
|
-
if base.instance_variable_defined?(
|
107
|
-
base.instance_variable_get(
|
112
|
+
if base.instance_variable_defined?(:@_dependencies)
|
113
|
+
base.instance_variable_get(:@_dependencies) << self
|
108
114
|
return false
|
109
115
|
else
|
110
116
|
return false if base < self
|
111
117
|
@_dependencies.each { |dep| base.send(:include, dep) }
|
112
118
|
super
|
113
|
-
base.extend const_get(
|
114
|
-
base.class_eval(&@_included_block) if instance_variable_defined?(
|
119
|
+
base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods)
|
120
|
+
base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block)
|
115
121
|
end
|
116
122
|
end
|
117
123
|
|
118
124
|
def included(base = nil, &block)
|
119
125
|
if base.nil?
|
126
|
+
raise MultipleIncludedBlocks if instance_variable_defined?(:@_included_block)
|
127
|
+
|
120
128
|
@_included_block = block
|
121
129
|
else
|
122
130
|
super
|
123
131
|
end
|
124
132
|
end
|
133
|
+
|
134
|
+
def class_methods(&class_methods_module_definition)
|
135
|
+
mod = const_defined?(:ClassMethods, false) ?
|
136
|
+
const_get(:ClassMethods) :
|
137
|
+
const_set(:ClassMethods, Module.new)
|
138
|
+
|
139
|
+
mod.module_eval(&class_methods_module_definition)
|
140
|
+
end
|
125
141
|
end
|
126
142
|
end
|
@@ -107,7 +107,7 @@ module ActiveSupport
|
|
107
107
|
options = names.extract_options!
|
108
108
|
|
109
109
|
names.each do |name|
|
110
|
-
raise NameError.new('invalid config attribute name') unless name =~
|
110
|
+
raise NameError.new('invalid config attribute name') unless name =~ /\A[_A-Za-z]\w*\z/
|
111
111
|
|
112
112
|
reader, reader_line = "def #{name}; config.#{name}; end", __LINE__
|
113
113
|
writer, writer_line = "def #{name}=(value); config.#{name} = value; end", __LINE__
|
@@ -5,6 +5,8 @@ class Array
|
|
5
5
|
# %w( a b c d ).from(2) # => ["c", "d"]
|
6
6
|
# %w( a b c d ).from(10) # => []
|
7
7
|
# %w().from(0) # => []
|
8
|
+
# %w( a b c d ).from(-2) # => ["c", "d"]
|
9
|
+
# %w( a b c ).from(-10) # => []
|
8
10
|
def from(position)
|
9
11
|
self[position, length] || []
|
10
12
|
end
|
@@ -15,8 +17,14 @@ class Array
|
|
15
17
|
# %w( a b c d ).to(2) # => ["a", "b", "c"]
|
16
18
|
# %w( a b c d ).to(10) # => ["a", "b", "c", "d"]
|
17
19
|
# %w().to(0) # => []
|
20
|
+
# %w( a b c d ).to(-2) # => ["a", "b", "c"]
|
21
|
+
# %w( a b c ).to(-10) # => []
|
18
22
|
def to(position)
|
19
|
-
|
23
|
+
if position >= 0
|
24
|
+
first position + 1
|
25
|
+
else
|
26
|
+
self[0..position]
|
27
|
+
end
|
20
28
|
end
|
21
29
|
|
22
30
|
# Equal to <tt>self[1]</tt>.
|
@@ -48,6 +56,8 @@ class Array
|
|
48
56
|
end
|
49
57
|
|
50
58
|
# Equal to <tt>self[41]</tt>. Also known as accessing "the reddit".
|
59
|
+
#
|
60
|
+
# (1..42).to_a.forty_two # => 42
|
51
61
|
def forty_two
|
52
62
|
self[41]
|
53
63
|
end
|
@@ -82,23 +82,8 @@ class Array
|
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
|
-
#
|
86
|
-
# <tt
|
87
|
-
#
|
88
|
-
# class Blog < ActiveRecord::Base
|
89
|
-
# def to_s
|
90
|
-
# title
|
91
|
-
# end
|
92
|
-
# end
|
93
|
-
#
|
94
|
-
# Blog.all.map(&:title) #=> ["First Post", "Second Post", "Third post"]
|
95
|
-
#
|
96
|
-
# <tt>to_formatted_s</tt> shows us:
|
97
|
-
#
|
98
|
-
# Blog.all.to_formatted_s # => "First PostSecond PostThird Post"
|
99
|
-
#
|
100
|
-
# Adding in the <tt>:db</tt> argument as the format yields a comma separated
|
101
|
-
# id list:
|
85
|
+
# Extends <tt>Array#to_s</tt> to convert a collection of elements into a
|
86
|
+
# comma separated id list if <tt>:db</tt> argument is given as the format.
|
102
87
|
#
|
103
88
|
# Blog.all.to_formatted_s(:db) # => "1,2,3"
|
104
89
|
def to_formatted_s(format = :default)
|
@@ -18,6 +18,11 @@ class Array
|
|
18
18
|
# ["3", "4"]
|
19
19
|
# ["5"]
|
20
20
|
def in_groups_of(number, fill_with = nil)
|
21
|
+
if number.to_i <= 0
|
22
|
+
raise ArgumentError,
|
23
|
+
"Group size must be a positive integer, was #{number.inspect}"
|
24
|
+
end
|
25
|
+
|
21
26
|
if fill_with == false
|
22
27
|
collection = self
|
23
28
|
else
|
@@ -25,15 +30,13 @@ class Array
|
|
25
30
|
# subtracting from number gives how many to add;
|
26
31
|
# modulo number ensures we don't add group of just fill.
|
27
32
|
padding = (number - size % number) % number
|
28
|
-
collection = dup.concat(
|
33
|
+
collection = dup.concat(Array.new(padding, fill_with))
|
29
34
|
end
|
30
35
|
|
31
36
|
if block_given?
|
32
37
|
collection.each_slice(number) { |slice| yield(slice) }
|
33
38
|
else
|
34
|
-
|
35
|
-
collection.each_slice(number) { |group| groups << group }
|
36
|
-
groups
|
39
|
+
collection.each_slice(number).to_a
|
37
40
|
end
|
38
41
|
end
|
39
42
|
|
@@ -55,7 +58,7 @@ class Array
|
|
55
58
|
# ["4", "5"]
|
56
59
|
# ["6", "7"]
|
57
60
|
def in_groups(number, fill_with = nil)
|
58
|
-
# size
|
61
|
+
# size.div number gives minor group size;
|
59
62
|
# size % number gives how many objects need extra accommodation;
|
60
63
|
# each group hold either division or division + 1 items.
|
61
64
|
division = size.div number
|
@@ -85,14 +88,28 @@ class Array
|
|
85
88
|
#
|
86
89
|
# [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
|
87
90
|
# (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
|
88
|
-
def split(value = nil
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
91
|
+
def split(value = nil)
|
92
|
+
if block_given?
|
93
|
+
inject([[]]) do |results, element|
|
94
|
+
if yield(element)
|
95
|
+
results << []
|
96
|
+
else
|
97
|
+
results.last << element
|
98
|
+
end
|
95
99
|
|
100
|
+
results
|
101
|
+
end
|
102
|
+
else
|
103
|
+
results, arr = [[]], self.dup
|
104
|
+
until arr.empty?
|
105
|
+
if (idx = arr.index(value))
|
106
|
+
results.last.concat(arr.shift(idx))
|
107
|
+
arr.shift
|
108
|
+
results << []
|
109
|
+
else
|
110
|
+
results.last.concat(arr.shift(arr.size))
|
111
|
+
end
|
112
|
+
end
|
96
113
|
results
|
97
114
|
end
|
98
115
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class Array
|
2
|
-
# The human way of thinking about adding stuff to the end of a list is with append
|
2
|
+
# The human way of thinking about adding stuff to the end of a list is with append.
|
3
3
|
alias_method :append, :<<
|
4
4
|
|
5
|
-
# The human way of thinking about adding stuff to the beginning of a list is with prepend
|
5
|
+
# The human way of thinking about adding stuff to the beginning of a list is with prepend.
|
6
6
|
alias_method :prepend, :unshift
|
7
7
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'active_support/core_ext/array/wrap'
|
2
2
|
require 'active_support/core_ext/array/access'
|
3
|
-
require 'active_support/core_ext/array/uniq_by'
|
4
3
|
require 'active_support/core_ext/array/conversions'
|
5
4
|
require 'active_support/core_ext/array/extract_options'
|
6
5
|
require 'active_support/core_ext/array/grouping'
|
@@ -1,22 +1,7 @@
|
|
1
1
|
require 'bigdecimal'
|
2
2
|
require 'bigdecimal/util'
|
3
|
-
require 'yaml'
|
4
3
|
|
5
4
|
class BigDecimal
|
6
|
-
YAML_MAPPING = { 'Infinity' => '.Inf', '-Infinity' => '-.Inf', 'NaN' => '.NaN' }
|
7
|
-
|
8
|
-
def encode_with(coder)
|
9
|
-
string = to_s
|
10
|
-
coder.represent_scalar(nil, YAML_MAPPING[string] || string)
|
11
|
-
end
|
12
|
-
|
13
|
-
# Backport this method if it doesn't exist
|
14
|
-
unless method_defined?(:to_d)
|
15
|
-
def to_d
|
16
|
-
self
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
5
|
DEFAULT_STRING_FORMAT = 'F'
|
21
6
|
def to_formatted_s(*args)
|
22
7
|
if args[0].is_a?(Symbol)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'active_support/deprecation'
|
2
|
+
|
3
|
+
ActiveSupport::Deprecation.warn 'core_ext/big_decimal/yaml_conversions is deprecated and will be removed in the future.'
|
4
|
+
|
5
|
+
require 'bigdecimal'
|
6
|
+
require 'yaml'
|
7
|
+
require 'active_support/core_ext/big_decimal/conversions'
|
8
|
+
|
9
|
+
class BigDecimal
|
10
|
+
YAML_MAPPING = { 'Infinity' => '.Inf', '-Infinity' => '-.Inf', 'NaN' => '.NaN' }
|
11
|
+
|
12
|
+
def encode_with(coder)
|
13
|
+
string = to_s
|
14
|
+
coder.represent_scalar(nil, YAML_MAPPING[string] || string)
|
15
|
+
end
|
16
|
+
end
|
@@ -70,8 +70,7 @@ class Class
|
|
70
70
|
# To opt out of both instance methods, pass <tt>instance_accessor: false</tt>.
|
71
71
|
def class_attribute(*attrs)
|
72
72
|
options = attrs.extract_options!
|
73
|
-
|
74
|
-
instance_reader = instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
|
73
|
+
instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
|
75
74
|
instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)
|
76
75
|
instance_predicate = options.fetch(:instance_predicate, true)
|
77
76
|
|
@@ -1,170 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
#
|
4
|
-
|
5
|
-
class Class
|
6
|
-
# Defines a class attribute if it's not defined and creates a reader method that
|
7
|
-
# returns the attribute value.
|
8
|
-
#
|
9
|
-
# class Person
|
10
|
-
# cattr_reader :hair_colors
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# Person.class_variable_set("@@hair_colors", [:brown, :black])
|
14
|
-
# Person.hair_colors # => [:brown, :black]
|
15
|
-
# Person.new.hair_colors # => [:brown, :black]
|
16
|
-
#
|
17
|
-
# The attribute name must be a valid method name in Ruby.
|
18
|
-
#
|
19
|
-
# class Person
|
20
|
-
# cattr_reader :"1_Badname "
|
21
|
-
# end
|
22
|
-
# # => NameError: invalid attribute name
|
23
|
-
#
|
24
|
-
# If you want to opt out the instance reader method, you can pass <tt>instance_reader: false</tt>
|
25
|
-
# or <tt>instance_accessor: false</tt>.
|
26
|
-
#
|
27
|
-
# class Person
|
28
|
-
# cattr_reader :hair_colors, instance_reader: false
|
29
|
-
# end
|
30
|
-
#
|
31
|
-
# Person.new.hair_colors # => NoMethodError
|
32
|
-
def cattr_reader(*syms)
|
33
|
-
options = syms.extract_options!
|
34
|
-
syms.each do |sym|
|
35
|
-
raise NameError.new("invalid class attribute name: #{sym}") unless sym =~ /^[_A-Za-z]\w*$/
|
36
|
-
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
37
|
-
unless defined? @@#{sym}
|
38
|
-
@@#{sym} = nil
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.#{sym}
|
42
|
-
@@#{sym}
|
43
|
-
end
|
44
|
-
EOS
|
45
|
-
|
46
|
-
unless options[:instance_reader] == false || options[:instance_accessor] == false
|
47
|
-
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
48
|
-
def #{sym}
|
49
|
-
@@#{sym}
|
50
|
-
end
|
51
|
-
EOS
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
# Defines a class attribute if it's not defined and creates a writer method to allow
|
57
|
-
# assignment to the attribute.
|
58
|
-
#
|
59
|
-
# class Person
|
60
|
-
# cattr_writer :hair_colors
|
61
|
-
# end
|
62
|
-
#
|
63
|
-
# Person.hair_colors = [:brown, :black]
|
64
|
-
# Person.class_variable_get("@@hair_colors") # => [:brown, :black]
|
65
|
-
# Person.new.hair_colors = [:blonde, :red]
|
66
|
-
# Person.class_variable_get("@@hair_colors") # => [:blonde, :red]
|
67
|
-
#
|
68
|
-
# The attribute name must be a valid method name in Ruby.
|
69
|
-
#
|
70
|
-
# class Person
|
71
|
-
# cattr_writer :"1_Badname "
|
72
|
-
# end
|
73
|
-
# # => NameError: invalid attribute name
|
74
|
-
#
|
75
|
-
# If you want to opt out the instance writer method, pass <tt>instance_writer: false</tt>
|
76
|
-
# or <tt>instance_accessor: false</tt>.
|
77
|
-
#
|
78
|
-
# class Person
|
79
|
-
# cattr_writer :hair_colors, instance_writer: false
|
80
|
-
# end
|
81
|
-
#
|
82
|
-
# Person.new.hair_colors = [:blonde, :red] # => NoMethodError
|
83
|
-
#
|
84
|
-
# Also, you can pass a block to set up the attribute with a default value.
|
85
|
-
#
|
86
|
-
# class Person
|
87
|
-
# cattr_writer :hair_colors do
|
88
|
-
# [:brown, :black, :blonde, :red]
|
89
|
-
# end
|
90
|
-
# end
|
91
|
-
#
|
92
|
-
# Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
|
93
|
-
def cattr_writer(*syms)
|
94
|
-
options = syms.extract_options!
|
95
|
-
syms.each do |sym|
|
96
|
-
raise NameError.new("invalid class attribute name: #{sym}") unless sym =~ /^[_A-Za-z]\w*$/
|
97
|
-
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
98
|
-
unless defined? @@#{sym}
|
99
|
-
@@#{sym} = nil
|
100
|
-
end
|
101
|
-
|
102
|
-
def self.#{sym}=(obj)
|
103
|
-
@@#{sym} = obj
|
104
|
-
end
|
105
|
-
EOS
|
106
|
-
|
107
|
-
unless options[:instance_writer] == false || options[:instance_accessor] == false
|
108
|
-
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
109
|
-
def #{sym}=(obj)
|
110
|
-
@@#{sym} = obj
|
111
|
-
end
|
112
|
-
EOS
|
113
|
-
end
|
114
|
-
send("#{sym}=", yield) if block_given?
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
# Defines both class and instance accessors for class attributes.
|
119
|
-
#
|
120
|
-
# class Person
|
121
|
-
# cattr_accessor :hair_colors
|
122
|
-
# end
|
123
|
-
#
|
124
|
-
# Person.hair_colors = [:brown, :black, :blonde, :red]
|
125
|
-
# Person.hair_colors # => [:brown, :black, :blonde, :red]
|
126
|
-
# Person.new.hair_colors # => [:brown, :black, :blonde, :red]
|
127
|
-
#
|
128
|
-
# If a subclass changes the value then that would also change the value for
|
129
|
-
# parent class. Similarly if parent class changes the value then that would
|
130
|
-
# change the value of subclasses too.
|
131
|
-
#
|
132
|
-
# class Male < Person
|
133
|
-
# end
|
134
|
-
#
|
135
|
-
# Male.hair_colors << :blue
|
136
|
-
# Person.hair_colors # => [:brown, :black, :blonde, :red, :blue]
|
137
|
-
#
|
138
|
-
# To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
|
139
|
-
# To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
|
140
|
-
#
|
141
|
-
# class Person
|
142
|
-
# cattr_accessor :hair_colors, instance_writer: false, instance_reader: false
|
143
|
-
# end
|
144
|
-
#
|
145
|
-
# Person.new.hair_colors = [:brown] # => NoMethodError
|
146
|
-
# Person.new.hair_colors # => NoMethodError
|
147
|
-
#
|
148
|
-
# Or pass <tt>instance_accessor: false</tt>, to opt out both instance methods.
|
149
|
-
#
|
150
|
-
# class Person
|
151
|
-
# cattr_accessor :hair_colors, instance_accessor: false
|
152
|
-
# end
|
153
|
-
#
|
154
|
-
# Person.new.hair_colors = [:brown] # => NoMethodError
|
155
|
-
# Person.new.hair_colors # => NoMethodError
|
156
|
-
#
|
157
|
-
# Also you can pass a block to set up the attribute with a default value.
|
158
|
-
#
|
159
|
-
# class Person
|
160
|
-
# cattr_accessor :hair_colors do
|
161
|
-
# [:brown, :black, :blonde, :red]
|
162
|
-
# end
|
163
|
-
# end
|
164
|
-
#
|
165
|
-
# Person.class_variable_get("@@hair_colors") #=> [:brown, :black, :blonde, :red]
|
166
|
-
def cattr_accessor(*syms, &blk)
|
167
|
-
cattr_reader(*syms)
|
168
|
-
cattr_writer(*syms, &blk)
|
169
|
-
end
|
170
|
-
end
|
1
|
+
# cattr_* became mattr_* aliases in 7dfbd91b0780fbd6a1dd9bfbc176e10894871d2d,
|
2
|
+
# but we keep this around for libraries that directly require it knowing they
|
3
|
+
# want cattr_*. No need to deprecate.
|
4
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
@@ -1,25 +1,30 @@
|
|
1
1
|
require 'active_support/core_ext/kernel/singleton_class'
|
2
2
|
require 'active_support/core_ext/module/remove_method'
|
3
|
+
require 'active_support/core_ext/module/deprecation'
|
4
|
+
|
3
5
|
|
4
6
|
class Class
|
5
7
|
def superclass_delegating_accessor(name, options = {})
|
6
8
|
# Create private _name and _name= methods that can still be used if the public
|
7
|
-
# methods are overridden.
|
8
|
-
_superclass_delegating_accessor("_#{name}")
|
9
|
+
# methods are overridden.
|
10
|
+
_superclass_delegating_accessor("_#{name}", options)
|
9
11
|
|
10
|
-
# Generate the public methods name, name=, and name
|
12
|
+
# Generate the public methods name, name=, and name?.
|
11
13
|
# These methods dispatch to the private _name, and _name= methods, making them
|
12
|
-
# overridable
|
14
|
+
# overridable.
|
13
15
|
singleton_class.send(:define_method, name) { send("_#{name}") }
|
14
16
|
singleton_class.send(:define_method, "#{name}?") { !!send("_#{name}") }
|
15
17
|
singleton_class.send(:define_method, "#{name}=") { |value| send("_#{name}=", value) }
|
16
18
|
|
17
|
-
# If an instance_reader is needed, generate methods
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
# If an instance_reader is needed, generate public instance methods name and name?.
|
20
|
+
if options[:instance_reader] != false
|
21
|
+
define_method(name) { send("_#{name}") }
|
22
|
+
define_method("#{name}?") { !!send("#{name}") }
|
23
|
+
end
|
21
24
|
end
|
22
25
|
|
26
|
+
deprecate superclass_delegating_accessor: :class_attribute
|
27
|
+
|
23
28
|
private
|
24
29
|
# Take the object being set and store it in a method. This gives us automatic
|
25
30
|
# inheritance behavior, without having to store the object in an instance
|
@@ -69,6 +69,16 @@ class Date
|
|
69
69
|
alias :at_midnight :beginning_of_day
|
70
70
|
alias :at_beginning_of_day :beginning_of_day
|
71
71
|
|
72
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the middle of the day (12:00)
|
73
|
+
def middle_of_day
|
74
|
+
in_time_zone.middle_of_day
|
75
|
+
end
|
76
|
+
alias :midday :middle_of_day
|
77
|
+
alias :noon :middle_of_day
|
78
|
+
alias :at_midday :middle_of_day
|
79
|
+
alias :at_noon :middle_of_day
|
80
|
+
alias :at_middle_of_day :middle_of_day
|
81
|
+
|
72
82
|
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
|
73
83
|
def end_of_day
|
74
84
|
in_time_zone.end_of_day
|
@@ -13,7 +13,8 @@ class Date
|
|
13
13
|
day_format = ActiveSupport::Inflector.ordinalize(date.day)
|
14
14
|
date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007"
|
15
15
|
},
|
16
|
-
:rfc822 => '%e %b %Y'
|
16
|
+
:rfc822 => '%e %b %Y',
|
17
|
+
:iso8601 => lambda { |date| date.iso8601 }
|
17
18
|
}
|
18
19
|
|
19
20
|
# Ruby 1.9 has Date#to_time which converts to localtime only.
|
@@ -34,9 +35,11 @@ class Date
|
|
34
35
|
# date.to_s(:db) # => "2007-11-10"
|
35
36
|
#
|
36
37
|
# date.to_formatted_s(:short) # => "10 Nov"
|
38
|
+
# date.to_formatted_s(:number) # => "20071110"
|
37
39
|
# date.to_formatted_s(:long) # => "November 10, 2007"
|
38
40
|
# date.to_formatted_s(:long_ordinal) # => "November 10th, 2007"
|
39
41
|
# date.to_formatted_s(:rfc822) # => "10 Nov 2007"
|
42
|
+
# date.to_formatted_s(:iso8601) # => "2007-11-10"
|
40
43
|
#
|
41
44
|
# == Adding your own date formats to to_formatted_s
|
42
45
|
# You can add your own formats to the Date::DATE_FORMATS hash.
|
@@ -80,6 +83,11 @@ class Date
|
|
80
83
|
::Time.send(form, year, month, day)
|
81
84
|
end
|
82
85
|
|
86
|
+
# Returns a string which represents the time in used time zone as DateTime
|
87
|
+
# defined by XML Schema:
|
88
|
+
#
|
89
|
+
# date = Date.new(2015, 05, 23) # => Sat, 23 May 2015
|
90
|
+
# date.xmlschema # => "2015-05-23T00:00:00+04:00"
|
83
91
|
def xmlschema
|
84
92
|
in_time_zone.xmlschema
|
85
93
|
end
|
@@ -1,37 +1,6 @@
|
|
1
1
|
require 'date'
|
2
|
-
require 'active_support/core_ext/
|
2
|
+
require 'active_support/core_ext/date_and_time/zones'
|
3
3
|
|
4
4
|
class Date
|
5
|
-
|
6
|
-
#
|
7
|
-
# Converts Date to a TimeWithZone in the current zone if <tt>Time.zone</tt> or
|
8
|
-
# <tt>Time.zone_default</tt> is set, otherwise converts Date to a Time via
|
9
|
-
# Date#to_time.
|
10
|
-
def to_time_in_current_zone
|
11
|
-
ActiveSupport::Deprecation.warn 'Date#to_time_in_current_zone is deprecated. Use Date#in_time_zone instead', caller
|
12
|
-
|
13
|
-
if ::Time.zone
|
14
|
-
::Time.zone.local(year, month, day)
|
15
|
-
else
|
16
|
-
to_time
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# Converts Date to a TimeWithZone in the current zone if Time.zone or Time.zone_default
|
21
|
-
# is set, otherwise converts Date to a Time via Date#to_time
|
22
|
-
#
|
23
|
-
# Time.zone = 'Hawaii' # => 'Hawaii'
|
24
|
-
# Date.new(2000).in_time_zone # => Sat, 01 Jan 2000 00:00:00 HST -10:00
|
25
|
-
#
|
26
|
-
# You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
|
27
|
-
# and the conversion will be based on that zone instead of <tt>Time.zone</tt>.
|
28
|
-
#
|
29
|
-
# Date.new(2000).in_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00
|
30
|
-
def in_time_zone(zone = ::Time.zone)
|
31
|
-
if zone
|
32
|
-
::Time.find_zone!(zone).local(year, month, day)
|
33
|
-
else
|
34
|
-
to_time
|
35
|
-
end
|
36
|
-
end
|
5
|
+
include DateAndTime::Zones
|
37
6
|
end
|