Skip to content
This repository
Browse code

AS core_ext refactoring

  • Loading branch information...
commit 1946d7b9229fabb52226f9ff82de72872c748d90 1 parent 242f4d1
Alexey Gaziev authored
2  activesupport/lib/active_support/core_ext/array/access.rb
@@ -16,7 +16,7 @@ def from(position)
16 16
   #   %w( a b c d ).to(10) # => %w( a b c d )
17 17
   #   %w().to(0)           # => %w()
18 18
   def to(position)
19  
-    self.first position + 1
  19
+    first position + 1
20 20
   end
21 21
 
22 22
   # Equal to <tt>self[1]</tt>.
2  activesupport/lib/active_support/core_ext/array/conversions.rb
@@ -46,7 +46,7 @@ def to_sentence(options = {})
46 46
   def to_formatted_s(format = :default)
47 47
     case format
48 48
       when :db
49  
-        if respond_to?(:empty?) && self.empty?
  49
+        if respond_to?(:empty?) && empty?
50 50
           "null"
51 51
         else
52 52
           collect { |element| element.id }.join(",")
2  activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
@@ -71,7 +71,7 @@ def #{sym}=(obj)
71 71
           end
72 72
         EOS
73 73
       end
74  
-      self.send("#{sym}=", yield) if block_given?
  74
+      send("#{sym}=", yield) if block_given?
75 75
     end
76 76
   end
77 77
 
52  activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -5,7 +5,15 @@
5 5
 require 'active_support/core_ext/time/zones'
6 6
 
7 7
 class Date
8  
-  DAYS_INTO_WEEK = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
  8
+  DAYS_INTO_WEEK = {
  9
+    :monday => 0,
  10
+    :tuesday => 1,
  11
+    :wednesday => 2,
  12
+    :thursday => 3,
  13
+    :friday => 4,
  14
+    :saturday => 5,
  15
+    :sunday => 6
  16
+  }
9 17
 
10 18
   class << self
11 19
     # Returns a new Date representing the date 1 day ago (i.e. yesterday's date).
@@ -31,7 +39,7 @@ def past?
31 39
 
32 40
   # Returns true if the Date object's date is today.
33 41
   def today?
34  
-    self.to_date == ::Date.current # we need the to_date because of DateTime
  42
+    to_date == ::Date.current # we need the to_date because of DateTime
35 43
   end
36 44
 
37 45
   # Returns true if the Date object's date lies in the future.
@@ -105,9 +113,9 @@ def advance(options)
105 113
   #   Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)
106 114
   def change(options)
107 115
     ::Date.new(
108  
-      options[:year]  || self.year,
109  
-      options[:month] || self.month,
110  
-      options[:day]   || self.day
  116
+      options.fetch(:year, year),
  117
+      options.fetch(:month, month),
  118
+      options.fetch(:day, day)
111 119
     )
112 120
   end
113 121
 
@@ -166,7 +174,7 @@ def monday
166 174
   def end_of_week(start_day = :monday)
167 175
     days_to_end = 6 - days_to_week_start(start_day)
168 176
     result = self + days_to_end.days
169  
-    self.acts_like?(:time) ? result.end_of_day : result
  177
+    acts_like?(:time) ? result.end_of_day : result
170 178
   end
171 179
   alias :at_end_of_week :end_of_week
172 180
 
@@ -180,7 +188,7 @@ def sunday
180 188
   # week. Default is +:monday+. +DateTime+ objects have their time set to 0:00.
181 189
   def prev_week(day = :monday)
182 190
     result = (self - 7).beginning_of_week + DAYS_INTO_WEEK[day]
183  
-    self.acts_like?(:time) ? result.change(:hour => 0) : result
  191
+    acts_like?(:time) ? result.change(:hour => 0) : result
184 192
   end
185 193
   alias :last_week :prev_week
186 194
 
@@ -193,43 +201,57 @@ def prev_week(day = :monday)
193 201
   # Returns a new Date/DateTime representing the start of the given day in next week (default is :monday).
194 202
   def next_week(day = :monday)
195 203
     result = (self + 7).beginning_of_week + DAYS_INTO_WEEK[day]
196  
-    self.acts_like?(:time) ? result.change(:hour => 0) : result
  204
+    acts_like?(:time) ? result.change(:hour => 0) : result
197 205
   end
198 206
 
199 207
   # Returns a new ; DateTime objects will have time set to 0:00DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00)
200 208
   def beginning_of_month
201  
-    self.acts_like?(:time) ? change(:day => 1, :hour => 0) : change(:day => 1)
  209
+    acts_like?(:time) ? change(:day => 1, :hour => 0) : change(:day => 1)
202 210
   end
203 211
   alias :at_beginning_of_month :beginning_of_month
204 212
 
205 213
   # Returns a new Date/DateTime representing the end of the month (last day of the month; DateTime objects will have time set to 0:00)
206 214
   def end_of_month
207  
-    last_day = ::Time.days_in_month( self.month, self.year )
208  
-    self.acts_like?(:time) ? change(:day => last_day, :hour => 23, :min => 59, :sec => 59) : change(:day => last_day)
  215
+    last_day = ::Time.days_in_month(month, year)
  216
+    if acts_like?(:time)
  217
+      change(:day => last_day, :hour => 23, :min => 59, :sec => 59)
  218
+    else
  219
+      change(:day => last_day)
  220
+    end
209 221
   end
210 222
   alias :at_end_of_month :end_of_month
211 223
 
212 224
   # Returns a new Date/DateTime representing the start of the quarter (1st of january, april, july, october; DateTime objects will have time set to 0:00)
213 225
   def beginning_of_quarter
214  
-    beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
  226
+    first_quarter_month = [10, 7, 4, 1].detect { |m| m <= month }
  227
+    beginning_of_month.change(:month => first_quarter_month)
215 228
   end
216 229
   alias :at_beginning_of_quarter :beginning_of_quarter
217 230
 
218 231
   # Returns a new Date/DateTime representing the end of the quarter (last day of march, june, september, december; DateTime objects will have time set to 23:59:59)
219 232
   def end_of_quarter
220  
-    beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month
  233
+    last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month }
  234
+    beginning_of_month.change(:month => last_quarter_month).end_of_month
221 235
   end
222 236
   alias :at_end_of_quarter :end_of_quarter
223 237
 
224 238
   # Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00)
225 239
   def beginning_of_year
226  
-    self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0) : change(:month => 1, :day => 1)
  240
+    if acts_like?(:time)
  241
+      change(:month => 1, :day => 1, :hour => 0)
  242
+    else
  243
+      change(:month => 1, :day => 1)
  244
+    end
227 245
   end
228 246
   alias :at_beginning_of_year :beginning_of_year
229 247
 
230 248
   # Returns a new Time representing the end of the year (31st of december; DateTime objects will have time set to 23:59:59)
231 249
   def end_of_year
232  
-    self.acts_like?(:time) ? change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59) : change(:month => 12, :day => 31)
  250
+    if acts_like?(:time)
  251
+      change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59)
  252
+    else
  253
+      change(:month => 12, :day => 31)
  254
+    end
233 255
   end
234 256
   alias :at_end_of_year :end_of_year
235 257
 
5  activesupport/lib/active_support/core_ext/date/conversions.rb
@@ -9,7 +9,10 @@ class Date
9 9
     :long         => "%B %e, %Y",
10 10
     :db           => "%Y-%m-%d",
11 11
     :number       => "%Y%m%d",
12  
-    :long_ordinal => lambda { |date| date.strftime("%B #{ActiveSupport::Inflector.ordinalize(date.day)}, %Y") }, # => "April 25th, 2007"
  12
+    :long_ordinal => lambda { |date|
  13
+      day_format = ActiveSupport::Inflector.ordinalize(date.day)
  14
+      date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007"
  15
+    },
13 16
     :rfc822       => "%e %b %Y"
14 17
   }
15 18
 
33  activesupport/lib/active_support/core_ext/date_time/calculations.rb
@@ -4,8 +4,8 @@ class DateTime
4 4
   class << self
5 5
     # *DEPRECATED*: Use +DateTime.civil_from_format+ directly.
6 6
     def local_offset
7  
-      ActiveSupport::Deprecation.warn 'DateTime.local_offset is deprecated. ' \
8  
-        'Use DateTime.civil_from_format directly.', caller
  7
+      ActiveSupport::Deprecation.warn 'DateTime.local_offset is deprecated. Use DateTime.civil_from_format directly.', caller
  8
+
9 9
       ::Time.local(2012).utc_offset.to_r / 86400
10 10
     end
11 11
 
@@ -35,14 +35,14 @@ def seconds_since_midnight
35 35
   # minute is passed, then sec is set to 0.
36 36
   def change(options)
37 37
     ::DateTime.civil(
38  
-      options[:year]  || year,
39  
-      options[:month] || month,
40  
-      options[:day]   || day,
41  
-      options[:hour]  || hour,
42  
-      options[:min]   || (options[:hour] ? 0 : min),
43  
-      options[:sec]   || ((options[:hour] || options[:min]) ? 0 : sec),
44  
-      options[:offset]  || offset,
45  
-      options[:start]  || start
  38
+      options.fetch(:year, year),
  39
+      options.fetch(:month, month),
  40
+      options.fetch(:day, day),
  41
+      options.fetch(:hour, hour),
  42
+      options.fetch(:min, options[:hour] ? 0 : min),
  43
+      options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec),
  44
+      options.fetch(:offset, offset),
  45
+      options.fetch(:start, start)
46 46
     )
47 47
   end
48 48
 
@@ -53,8 +53,16 @@ def change(options)
53 53
   def advance(options)
54 54
     d = to_date.advance(options)
55 55
     datetime_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
56  
-    seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
57  
-    seconds_to_advance == 0 ? datetime_advanced_by_date : datetime_advanced_by_date.since(seconds_to_advance)
  56
+    seconds_to_advance = \
  57
+      options.fetch(:seconds, 0) +
  58
+      options.fetch(:minutes, 0) * 60 +
  59
+      options.fetch(:hours, 0) * 3600
  60
+
  61
+    if seconds_to_advance.zero?
  62
+      datetime_advanced_by_date
  63
+    else
  64
+      datetime_advanced_by_date.since seconds_to_advance
  65
+    end
58 66
   end
59 67
 
60 68
   # Returns a new DateTime representing the time a number of seconds ago
@@ -108,4 +116,5 @@ def utc_offset
108 116
   def <=>(other)
109 117
     super other.to_datetime
110 118
   end
  119
+
111 120
 end
9  activesupport/lib/active_support/core_ext/enumerable.rb
@@ -62,8 +62,11 @@ class Range #:nodoc:
62 62
   # Optimize range sum to use arithmetic progression if a block is not given and
63 63
   # we have a range of numeric values.
64 64
   def sum(identity = 0)
65  
-    return super if block_given? || !(first.instance_of?(Integer) && last.instance_of?(Integer))
66  
-    actual_last = exclude_end? ? (last - 1) : last
67  
-    (actual_last - first + 1) * (actual_last + first) / 2
  65
+    if block_given? || !(first.instance_of?(Integer) && last.instance_of?(Integer))
  66
+      super
  67
+    else
  68
+      actual_last = exclude_end? ? (last - 1) : last
  69
+      (actual_last - first + 1) * (actual_last + first) / 2
  70
+    end
68 71
   end
69 72
 end
4  activesupport/lib/active_support/core_ext/logger.rb
@@ -56,8 +56,8 @@ def silence(temporary_level = Logger::ERROR)
56 56
   alias :old_datetime_format= :datetime_format=
57 57
   # Logging date-time format (string passed to +strftime+). Ignored if the formatter
58 58
   # does not respond to datetime_format=.
59  
-  def datetime_format=(datetime_format)
60  
-    formatter.datetime_format = datetime_format if formatter.respond_to?(:datetime_format=)
  59
+  def datetime_format=(format)
  60
+    formatter.datetime_format = format if formatter.respond_to?(:datetime_format=)
61 61
   end
62 62
 
63 63
   alias :old_datetime_format :datetime_format
4  activesupport/lib/active_support/core_ext/object/inclusion.rb
@@ -4,7 +4,7 @@ class Object
4 4
   #
5 5
   #   characters = ["Konata", "Kagami", "Tsukasa"]
6 6
   #   "Konata".in?(characters) # => true
7  
-  #   
  7
+  #
8 8
   #   character = "Konata"
9 9
   #   character.in?("Konata", "Kagami", "Tsukasa") # => true
10 10
   #
@@ -18,7 +18,7 @@ def in?(*args)
18 18
       if another_object.respond_to? :include?
19 19
         another_object.include? self
20 20
       else
21  
-        raise ArgumentError.new("The single parameter passed to #in? must respond to #include?")
  21
+        raise ArgumentError.new 'The single parameter passed to #in? must respond to #include?'
22 22
       end
23 23
     end
24 24
   end
32  activesupport/lib/active_support/core_ext/string/conversions.rb
@@ -4,21 +4,33 @@
4 4
 class String
5 5
   # Form can be either :utc (default) or :local.
6 6
   def to_time(form = :utc)
7  
-    return nil if self.blank?
8  
-    d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset).map { |arg| arg || 0 }
9  
-    d[6] *= 1000000
10  
-    ::Time.send("#{form}_time", *d[0..6]) - d[7]
  7
+    unless blank?
  8
+      date_values = ::Date._parse(self, false).
  9
+        values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset).
  10
+        map! { |arg| arg || 0 }
  11
+      date_values[6] *= 1000000
  12
+      offset = date_values.pop
  13
+
  14
+      ::Time.send("#{form}_time", *date_values) - offset
  15
+    end
11 16
   end
12 17
 
13 18
   def to_date
14  
-    return nil if self.blank?
15  
-    ::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
  19
+    unless blank?
  20
+      date_values = ::Date._parse(self, false).values_at(:year, :mon, :mday)
  21
+
  22
+      ::Date.new(*date_values)
  23
+    end
16 24
   end
17 25
 
18 26
   def to_datetime
19  
-    return nil if self.blank?
20  
-    d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :sec_fraction).map { |arg| arg || 0 }
21  
-    d[5] += d.pop
22  
-    ::DateTime.civil(*d)
  27
+    unless blank?
  28
+      date_values = ::Date._parse(self, false).
  29
+        values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :sec_fraction).
  30
+        map! { |arg| arg || 0 }
  31
+      date_values[5] += date_values.pop
  32
+
  33
+      ::DateTime.civil(*date_values)
  34
+    end
23 35
   end
24 36
 end
14  activesupport/lib/active_support/core_ext/string/filters.rb
@@ -35,13 +35,17 @@ def squish!
35 35
   #
36 36
   #   "And they found that many people were sleeping better.".truncate(25, :omission => "... (continued)")
37 37
   #   # => "And they f... (continued)"
38  
-  def truncate(length, options = {})
39  
-    return self.dup unless self.length > length
  38
+  def truncate(truncate_at, options = {})
  39
+    return dup unless length > truncate_at
40 40
 
41 41
     options[:omission] ||= "..."
42  
-    length_with_room_for_omission = length - options[:omission].length
43  
-    stop = options[:separator] ?
44  
-      (rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission
  42
+    length_with_room_for_omission = truncate_at - options[:omission].length
  43
+    stop = \
  44
+      if options[:separator]
  45
+        rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
  46
+      else
  47
+        length_with_room_for_omission
  48
+      end
45 49
 
46 50
     self[0...stop] + options[:omission]
47 51
   end
21  activesupport/lib/active_support/core_ext/string/output_safety.rb
@@ -92,7 +92,10 @@ def html_safe?
92 92
 
93 93
 module ActiveSupport #:nodoc:
94 94
   class SafeBuffer < String
95  
-    UNSAFE_STRING_METHODS = ["capitalize", "chomp", "chop", "delete", "downcase", "gsub", "lstrip", "next", "reverse", "rstrip", "slice", "squeeze", "strip", "sub", "succ", "swapcase", "tr", "tr_s", "upcase", "prepend"].freeze
  95
+    UNSAFE_STRING_METHODS = %w(
  96
+      capitalize chomp chop delete downcase gsub lstrip next reverse rstrip
  97
+      slice squeeze strip sub succ swapcase tr tr_s upcase prepend
  98
+    )
96 99
 
97 100
     alias_method :original_concat, :concat
98 101
     private :original_concat
@@ -104,14 +107,16 @@ def initialize
104 107
     end
105 108
 
106 109
     def [](*args)
107  
-      return super if args.size < 2
108  
-
109  
-      if html_safe?
110  
-        new_safe_buffer = super
111  
-        new_safe_buffer.instance_eval { @html_safe = true }
112  
-        new_safe_buffer
  110
+      if args.size < 2
  111
+        super
113 112
       else
114  
-        to_str[*args]
  113
+        if html_safe?
  114
+          new_safe_buffer = super
  115
+          new_safe_buffer.instance_eval { @html_safe = true }
  116
+          new_safe_buffer
  117
+        else
  118
+          to_str[*args]
  119
+        end
115 120
       end
116 121
     end
117 122
 
95  activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -3,7 +3,15 @@
3 3
 
4 4
 class Time
5 5
   COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
6  
-  DAYS_INTO_WEEK = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
  6
+  DAYS_INTO_WEEK = {
  7
+    :monday    => 0,
  8
+    :tuesday   => 1,
  9
+    :wednesday => 2,
  10
+    :thursday  => 3,
  11
+    :friday    => 4,
  12
+    :saturday  => 5,
  13
+    :sunday    => 6
  14
+  }
7 15
 
8 16
   class << self
9 17
     # Overriding case equality method so that it returns true for ActiveSupport::TimeWithZone instances
@@ -23,8 +31,13 @@ def days_in_month(month, year = now.year)
23 31
     # otherwise returns a DateTime.
24 32
     def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
25 33
       time = ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
  34
+
26 35
       # This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138.
27  
-      time.year == year ? time : ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
  36
+      if time.year == year
  37
+        time
  38
+      else
  39
+        ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
  40
+      end
28 41
     rescue
29 42
       ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
30 43
     end
@@ -71,13 +84,13 @@ def seconds_since_midnight
71 84
   def change(options)
72 85
     ::Time.send(
73 86
       utc? ? :utc_time : :local_time,
74  
-      options[:year]  || year,
75  
-      options[:month] || month,
76  
-      options[:day]   || day,
77  
-      options[:hour]  || hour,
78  
-      options[:min]   || (options[:hour] ? 0 : min),
79  
-      options[:sec]   || ((options[:hour] || options[:min]) ? 0 : sec),
80  
-      options[:usec]  || ((options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
  87
+      options.fetch(:year, year),
  88
+      options.fetch(:month, month),
  89
+      options.fetch(:day, day),
  90
+      options.fetch(:hour, hour),
  91
+      options.fetch(:min, options[:hour] ? 0 : min),
  92
+      options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec),
  93
+      options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
81 94
     )
82 95
   end
83 96
 
@@ -88,18 +101,26 @@ def change(options)
88 101
   def advance(options)
89 102
     unless options[:weeks].nil?
90 103
       options[:weeks], partial_weeks = options[:weeks].divmod(1)
91  
-      options[:days] = (options[:days] || 0) + 7 * partial_weeks
  104
+      options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
92 105
     end
93 106
 
94 107
     unless options[:days].nil?
95 108
       options[:days], partial_days = options[:days].divmod(1)
96  
-      options[:hours] = (options[:hours] || 0) + 24 * partial_days
  109
+      options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
97 110
     end
98 111
 
99 112
     d = to_date.advance(options)
100 113
     time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
101  
-    seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
102  
-    seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance)
  114
+    seconds_to_advance = \
  115
+      options.fetch(:seconds, 0) +
  116
+      options.fetch(:minutes, 0) * 60 +
  117
+      options.fetch(:hours, 0) * 3600
  118
+
  119
+    if seconds_to_advance.zero?
  120
+      time_advanced_by_date
  121
+    else
  122
+      time_advanced_by_date.since(seconds_to_advance)
  123
+    end
103 124
   end
104 125
 
105 126
   # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
@@ -167,6 +188,7 @@ def days_to_week_start(start_day = :monday)
167 188
     start_day_number = DAYS_INTO_WEEK[start_day]
168 189
     current_day_number = wday != 0 ? wday - 1 : 6
169 190
     days_span = current_day_number - start_day_number
  191
+
170 192
     days_span >= 0 ? days_span : 7 + days_span
171 193
   end
172 194
 
@@ -198,13 +220,19 @@ def sunday
198 220
 
199 221
   # Returns a new Time representing the start of the given day in the previous week (default is :monday).
200 222
   def prev_week(day = :monday)
201  
-    ago(1.week).beginning_of_week.since(DAYS_INTO_WEEK[day].day).change(:hour => 0)
  223
+    ago(1.week).
  224
+      beginning_of_week.
  225
+      since(DAYS_INTO_WEEK[day].day).
  226
+      change(:hour => 0)
202 227
   end
203 228
   alias_method :last_week, :prev_week
204 229
 
205 230
   # Returns a new Time representing the start of the given day in next week (default is :monday).
206 231
   def next_week(day = :monday)
207  
-    since(1.week).beginning_of_week.since(DAYS_INTO_WEEK[day].day).change(:hour => 0)
  232
+    since(1.week).
  233
+      beginning_of_week.
  234
+      since(DAYS_INTO_WEEK[day].day).
  235
+      change(:hour => 0)
208 236
   end
209 237
 
210 238
   # Returns a new Time representing the start of the day (0:00)
@@ -218,7 +246,12 @@ def beginning_of_day
218 246
 
219 247
   # Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9)
220 248
   def end_of_day
221  
-    change(:hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
  249
+    change(
  250
+      :hour => 23,
  251
+      :min => 59,
  252
+      :sec => 59,
  253
+      :usec => 999999.999
  254
+    )
222 255
   end
223 256
 
224 257
   # Returns a new Time representing the start of the month (1st of the month, 0:00)
@@ -232,19 +265,27 @@ def beginning_of_month
232 265
   def end_of_month
233 266
     #self - ((self.mday-1).days + self.seconds_since_midnight)
234 267
     last_day = ::Time.days_in_month(month, year)
235  
-    change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
  268
+    change(
  269
+      :day  => last_day,
  270
+      :hour => 23,
  271
+      :min  => 59,
  272
+      :sec  => 59,
  273
+      :usec => 999999.999
  274
+    )
236 275
   end
237 276
   alias :at_end_of_month :end_of_month
238 277
 
239 278
   # Returns  a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)
240 279
   def beginning_of_quarter
241  
-    beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= month })
  280
+    first_quarter_month = [10, 7, 4, 1].detect { |m| m <= month }
  281
+    beginning_of_month.change(:month => first_quarter_month)
242 282
   end
243 283
   alias :at_beginning_of_quarter :beginning_of_quarter
244 284
 
245 285
   # Returns a new Time representing the end of the quarter (end of the last day of march, june, september, december)
246 286
   def end_of_quarter
247  
-    beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= month }).end_of_month
  287
+    last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month }
  288
+    beginning_of_month.change(:month => last_quarter_month).end_of_month
248 289
   end
249 290
   alias :at_end_of_quarter :end_of_quarter
250 291
 
@@ -256,7 +297,14 @@ def beginning_of_year
256 297
 
257 298
   # Returns a new Time representing the end of the year (end of the 31st of december)
258 299
   def end_of_year
259  
-    change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
  300
+    change(
  301
+      :month => 12,
  302
+      :day   => 31,
  303
+      :hour  => 23,
  304
+      :min   => 59,
  305
+      :sec   => 59,
  306
+      :usec  => 999999.999
  307
+    )
260 308
   end
261 309
   alias :at_end_of_year :end_of_year
262 310
 
@@ -329,7 +377,11 @@ def minus_with_coercion(other)
329 377
   # can be chronologically compared with a Time
330 378
   def compare_with_coercion(other)
331 379
     # we're avoiding Time#to_datetime cause it's expensive
332  
-    other.is_a?(Time) ? compare_without_coercion(other.to_time) : to_datetime <=> other
  380
+    if other.is_a?(Time)
  381
+      compare_without_coercion(other.to_time)
  382
+    else
  383
+      to_datetime <=> other
  384
+    end
333 385
   end
334 386
   alias_method :compare_without_coercion, :<=>
335 387
   alias_method :<=>, :compare_with_coercion
@@ -343,4 +395,5 @@ def eql_with_coercion(other)
343 395
   end
344 396
   alias_method :eql_without_coercion, :eql?
345 397
   alias_method :eql?, :eql_with_coercion
  398
+
346 399
 end
10  activesupport/lib/active_support/core_ext/time/conversions.rb
@@ -8,8 +8,14 @@ class Time
8 8
     :time         => "%H:%M",
9 9
     :short        => "%d %b %H:%M",
10 10
     :long         => "%B %d, %Y %H:%M",
11  
-    :long_ordinal => lambda { |time| time.strftime("%B #{ActiveSupport::Inflector.ordinalize(time.day)}, %Y %H:%M") },
12  
-    :rfc822       => lambda { |time| time.strftime("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") }
  11
+    :long_ordinal => lambda { |time|
  12
+      day_format = ActiveSupport::Inflector.ordinalize(time.day)
  13
+      time.strftime("%B #{day_format}, %Y %H:%M")
  14
+    },
  15
+    :rfc822       => lambda { |time|
  16
+      offset_format = time.formatted_offset(false)
  17
+      time.strftime("%a, %d %b %Y %H:%M:%S #{offset_format}")
  18
+    }
13 19
   }
14 20
 
15 21
   # Converts to a formatted string. See DATE_FORMATS for builtin formats.
28  activesupport/lib/active_support/core_ext/time/zones.rb
@@ -51,13 +51,21 @@ def use_zone(time_zone)
51 51
 
52 52
     # Returns a TimeZone instance or nil, or raises an ArgumentError for invalid timezones.
53 53
     def find_zone!(time_zone)
54  
-      return time_zone if time_zone.nil? || time_zone.is_a?(ActiveSupport::TimeZone)
55  
-      # lookup timezone based on identifier (unless we've been passed a TZInfo::Timezone)
56  
-      unless time_zone.respond_to?(:period_for_local)
57  
-        time_zone = ActiveSupport::TimeZone[time_zone] || TZInfo::Timezone.get(time_zone)
  54
+      if !time_zone || time_zone.is_a?(ActiveSupport::TimeZone)
  55
+        time_zone
  56
+      else
  57
+        # lookup timezone based on identifier (unless we've been passed a TZInfo::Timezone)
  58
+        unless time_zone.respond_to?(:period_for_local)
  59
+          time_zone = ActiveSupport::TimeZone[time_zone] || TZInfo::Timezone.get(time_zone)
  60
+        end
  61
+
  62
+        # Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone
  63
+        if time_zone.is_a?(ActiveSupport::TimeZone)
  64
+          time_zone
  65
+        else
  66
+          ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone)
  67
+        end
58 68
       end
59  
-      # Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone
60  
-      time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone)
61 69
     rescue TZInfo::InvalidTimezoneIdentifier
62 70
       raise ArgumentError, "Invalid Timezone: #{time_zone}"
63 71
     end
@@ -80,8 +88,10 @@ def find_zone(time_zone)
80 88
   #
81 89
   #   Time.utc(2000).in_time_zone('Alaska')  # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
82 90
   def in_time_zone(zone = ::Time.zone)
83  
-    return self unless zone
84  
-
85  
-    ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.find_zone!(zone))
  91
+    if zone
  92
+      ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.find_zone!(zone))
  93
+    else
  94
+      self
  95
+    end
86 96
   end
87 97
 end

0 notes on commit 1946d7b

Please sign in to comment.
Something went wrong with that request. Please try again.