activesupport 4.0.13 → 4.1.0.beta1
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 +4 -4
- data/CHANGELOG.md +283 -508
- data/README.rdoc +1 -1
- data/lib/active_support.rb +7 -1
- data/lib/active_support/backtrace_cleaner.rb +5 -5
- data/lib/active_support/benchmarkable.rb +0 -10
- data/lib/active_support/cache.rb +62 -26
- data/lib/active_support/cache/file_store.rb +27 -22
- data/lib/active_support/cache/mem_cache_store.rb +2 -2
- data/lib/active_support/cache/memory_store.rb +1 -0
- data/lib/active_support/cache/strategy/local_cache.rb +3 -0
- data/lib/active_support/callbacks.rb +416 -245
- data/lib/active_support/concern.rb +13 -5
- data/lib/active_support/core_ext.rb +0 -1
- data/lib/active_support/core_ext/array.rb +0 -1
- data/lib/active_support/core_ext/array/access.rb +2 -0
- data/lib/active_support/core_ext/array/conversions.rb +2 -17
- data/lib/active_support/core_ext/array/grouping.rb +24 -12
- data/lib/active_support/core_ext/array/prepend_and_append.rb +2 -2
- data/lib/active_support/core_ext/class.rb +0 -1
- data/lib/active_support/core_ext/class/attribute.rb +1 -2
- data/lib/active_support/core_ext/class/attribute_accessors.rb +5 -169
- data/lib/active_support/core_ext/date/calculations.rb +10 -0
- data/lib/active_support/core_ext/date/conversions.rb +5 -6
- data/lib/active_support/core_ext/date/zones.rb +2 -33
- data/lib/active_support/core_ext/date_and_time/calculations.rb +30 -11
- data/lib/active_support/core_ext/date_and_time/zones.rb +41 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +12 -25
- data/lib/active_support/core_ext/date_time/conversions.rb +2 -0
- data/lib/active_support/core_ext/date_time/zones.rb +3 -21
- data/lib/active_support/core_ext/hash.rb +0 -1
- data/lib/active_support/core_ext/hash/conversions.rb +6 -3
- data/lib/active_support/core_ext/hash/deep_merge.rb +11 -22
- data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -0
- data/lib/active_support/core_ext/hash/keys.rb +27 -47
- data/lib/active_support/core_ext/kernel/reporting.rb +2 -6
- data/lib/active_support/core_ext/module.rb +1 -0
- 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 +14 -4
- 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 +11 -0
- data/lib/active_support/core_ext/numeric/time.rb +8 -0
- data/lib/active_support/core_ext/object.rb +1 -1
- data/lib/active_support/core_ext/object/blank.rb +1 -1
- data/lib/active_support/core_ext/object/deep_dup.rb +6 -6
- data/lib/active_support/core_ext/object/inclusion.rb +4 -15
- data/lib/active_support/core_ext/object/json.rb +197 -0
- data/lib/active_support/core_ext/object/to_json.rb +4 -26
- data/lib/active_support/core_ext/object/to_param.rb +58 -1
- data/lib/active_support/core_ext/object/to_query.rb +7 -56
- data/lib/active_support/core_ext/object/try.rb +1 -1
- data/lib/active_support/core_ext/range/each.rb +2 -1
- data/lib/active_support/core_ext/string/access.rb +31 -31
- data/lib/active_support/core_ext/string/conversions.rb +9 -8
- data/lib/active_support/core_ext/string/exclude.rb +3 -3
- data/lib/active_support/core_ext/string/filters.rb +14 -4
- data/lib/active_support/core_ext/string/inflections.rb +11 -9
- data/lib/active_support/core_ext/string/output_safety.rb +65 -24
- data/lib/active_support/core_ext/string/zones.rb +1 -0
- data/lib/active_support/core_ext/thread.rb +4 -4
- data/lib/active_support/core_ext/time/calculations.rb +10 -57
- data/lib/active_support/core_ext/time/conversions.rb +3 -1
- data/lib/active_support/core_ext/time/zones.rb +2 -21
- data/lib/active_support/dependencies.rb +29 -13
- data/lib/active_support/deprecation.rb +4 -4
- data/lib/active_support/deprecation/behaviors.rb +3 -3
- data/lib/active_support/duration.rb +5 -7
- data/lib/active_support/file_update_checker.rb +1 -1
- data/lib/active_support/hash_with_indifferent_access.rb +4 -9
- data/lib/active_support/i18n.rb +4 -4
- data/lib/active_support/i18n_railtie.rb +2 -6
- data/lib/active_support/inflections.rb +0 -1
- data/lib/active_support/inflector/inflections.rb +17 -17
- data/lib/active_support/inflector/methods.rb +34 -17
- data/lib/active_support/json/decoding.rb +14 -21
- data/lib/active_support/json/encoding.rb +113 -285
- data/lib/active_support/key_generator.rb +1 -1
- 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 +1 -1
- data/lib/active_support/message_encryptor.rb +3 -3
- data/lib/active_support/message_verifier.rb +6 -1
- data/lib/active_support/multibyte/chars.rb +1 -2
- data/lib/active_support/multibyte/unicode.rb +27 -39
- data/lib/active_support/notifications.rb +3 -3
- data/lib/active_support/notifications/instrumenter.rb +2 -1
- data/lib/active_support/number_helper.rb +20 -311
- 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 +21 -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 +62 -0
- data/lib/active_support/option_merger.rb +1 -1
- data/lib/active_support/ordered_hash.rb +0 -8
- data/lib/active_support/ordered_options.rb +8 -0
- data/lib/active_support/per_thread_registry.rb +9 -8
- data/lib/active_support/subscriber.rb +26 -3
- data/lib/active_support/test_case.rb +9 -10
- data/lib/active_support/testing/assertions.rb +0 -30
- data/lib/active_support/testing/autorun.rb +2 -2
- data/lib/active_support/testing/declarative.rb +18 -8
- data/lib/active_support/testing/isolation.rb +13 -65
- data/lib/active_support/testing/setup_and_teardown.rb +17 -2
- data/lib/active_support/testing/tagged_logging.rb +1 -1
- data/lib/active_support/testing/time_helpers.rb +55 -0
- data/lib/active_support/time_with_zone.rb +4 -4
- data/lib/active_support/values/time_zone.rb +18 -15
- data/lib/active_support/version.rb +1 -1
- data/lib/active_support/xml_mini.rb +2 -4
- metadata +71 -61
- 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/proc.rb +0 -17
- data/lib/active_support/core_ext/string/encoding.rb +0 -8
- data/lib/active_support/json/variable.rb +0 -18
- data/lib/active_support/testing/pending.rb +0 -14
@@ -98,25 +98,33 @@ module ActiveSupport
|
|
98
98
|
# include Bar # works, Bar takes care now 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
|
@@ -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'
|
@@ -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)
|
@@ -25,15 +25,13 @@ class Array
|
|
25
25
|
# subtracting from number gives how many to add;
|
26
26
|
# modulo number ensures we don't add group of just fill.
|
27
27
|
padding = (number - size % number) % number
|
28
|
-
collection = dup.concat(
|
28
|
+
collection = dup.concat(Array.new(padding, fill_with))
|
29
29
|
end
|
30
30
|
|
31
31
|
if block_given?
|
32
32
|
collection.each_slice(number) { |slice| yield(slice) }
|
33
33
|
else
|
34
|
-
|
35
|
-
collection.each_slice(number) { |group| groups << group }
|
36
|
-
groups
|
34
|
+
collection.each_slice(number).to_a
|
37
35
|
end
|
38
36
|
end
|
39
37
|
|
@@ -55,7 +53,7 @@ class Array
|
|
55
53
|
# ["4", "5"]
|
56
54
|
# ["6", "7"]
|
57
55
|
def in_groups(number, fill_with = nil)
|
58
|
-
# size
|
56
|
+
# size.div number gives minor group size;
|
59
57
|
# size % number gives how many objects need extra accommodation;
|
60
58
|
# each group hold either division or division + 1 items.
|
61
59
|
division = size.div number
|
@@ -85,14 +83,28 @@ class Array
|
|
85
83
|
#
|
86
84
|
# [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
|
87
85
|
# (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
|
-
|
86
|
+
def split(value = nil)
|
87
|
+
if block_given?
|
88
|
+
inject([[]]) do |results, element|
|
89
|
+
if yield(element)
|
90
|
+
results << []
|
91
|
+
else
|
92
|
+
results.last << element
|
93
|
+
end
|
95
94
|
|
95
|
+
results
|
96
|
+
end
|
97
|
+
else
|
98
|
+
results, arr = [[]], self.dup
|
99
|
+
until arr.empty?
|
100
|
+
if (idx = arr.index(value))
|
101
|
+
results.last.concat(arr.shift(idx))
|
102
|
+
arr.shift
|
103
|
+
results << []
|
104
|
+
else
|
105
|
+
results.last.concat(arr.shift(arr.size))
|
106
|
+
end
|
107
|
+
end
|
96
108
|
results
|
97
109
|
end
|
98
110
|
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
|
@@ -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,6 @@
|
|
1
|
-
require 'active_support/
|
1
|
+
require 'active_support/deprecation'
|
2
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
4
|
+
ActiveSupport::Deprecation.warn(
|
5
|
+
"The cattr_* method definitions have been moved into active_support/core_ext/module/attribute_accessors. Please require that instead."
|
6
|
+
)
|
@@ -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
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'date'
|
2
2
|
require 'active_support/inflector/methods'
|
3
3
|
require 'active_support/core_ext/date/zones'
|
4
|
-
require 'active_support/core_ext/module/remove_method'
|
5
4
|
|
6
5
|
class Date
|
7
6
|
DATE_FORMATS = {
|
@@ -13,16 +12,15 @@ class Date
|
|
13
12
|
day_format = ActiveSupport::Inflector.ordinalize(date.day)
|
14
13
|
date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007"
|
15
14
|
},
|
16
|
-
:rfc822 => '%e %b %Y'
|
15
|
+
:rfc822 => '%e %b %Y',
|
16
|
+
:iso8601 => lambda { |date| date.iso8601 }
|
17
17
|
}
|
18
18
|
|
19
19
|
# Ruby 1.9 has Date#to_time which converts to localtime only.
|
20
20
|
remove_method :to_time
|
21
21
|
|
22
|
-
# Ruby 1.9 has Date#xmlschema which converts to a string without the time
|
23
|
-
|
24
|
-
# need to use remove_possible_method here
|
25
|
-
remove_possible_method :xmlschema
|
22
|
+
# Ruby 1.9 has Date#xmlschema which converts to a string without the time component.
|
23
|
+
remove_method :xmlschema
|
26
24
|
|
27
25
|
# Convert to a formatted string. See DATE_FORMATS for predefined formats.
|
28
26
|
#
|
@@ -37,6 +35,7 @@ class Date
|
|
37
35
|
# date.to_formatted_s(:long) # => "November 10, 2007"
|
38
36
|
# date.to_formatted_s(:long_ordinal) # => "November 10th, 2007"
|
39
37
|
# date.to_formatted_s(:rfc822) # => "10 Nov 2007"
|
38
|
+
# date.to_formatted_s(:iso8601) # => "2007-11-10"
|
40
39
|
#
|
41
40
|
# == Adding your own date formats to to_formatted_s
|
42
41
|
# You can add your own formats to the Date::DATE_FORMATS hash.
|
@@ -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
|