lazier 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -134,7 +134,7 @@
134
134
 
135
135
  </div>
136
136
  </dt>
137
- <dd><pre class="code"><span class='int'>3</span></pre></dd>
137
+ <dd><pre class="code"><span class='int'>5</span></pre></dd>
138
138
 
139
139
  <dt id="PATCH-constant" class="">PATCH =
140
140
  <div class="docstring">
@@ -149,7 +149,7 @@
149
149
 
150
150
  </div>
151
151
  </dt>
152
- <dd><pre class="code"><span class='int'>1</span></pre></dd>
152
+ <dd><pre class="code"><span class='int'>0</span></pre></dd>
153
153
 
154
154
  <dt id="STRING-constant" class="">STRING =
155
155
  <div class="docstring">
@@ -180,7 +180,7 @@
180
180
  </div>
181
181
 
182
182
  <div id="footer">
183
- Generated on Tue Jan 29 20:37:33 2013 by
183
+ Generated on Thu Jan 31 20:15:55 2013 by
184
184
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
185
185
  0.8.3 (ruby-1.9.3).
186
186
  </div>
data/doc/_index.html CHANGED
@@ -312,7 +312,7 @@
312
312
  </div>
313
313
 
314
314
  <div id="footer">
315
- Generated on Tue Jan 29 20:37:32 2013 by
315
+ Generated on Thu Jan 31 20:15:55 2013 by
316
316
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
317
317
  0.8.3 (ruby-1.9.3).
318
318
  </div>
data/doc/file.README.html CHANGED
@@ -102,7 +102,7 @@ See documentation for more informations.</p>
102
102
  </div></div>
103
103
 
104
104
  <div id="footer">
105
- Generated on Tue Jan 29 20:37:32 2013 by
105
+ Generated on Thu Jan 31 20:15:55 2013 by
106
106
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
107
107
  0.8.3 (ruby-1.9.3).
108
108
  </div>
data/doc/index.html CHANGED
@@ -102,7 +102,7 @@ See documentation for more informations.</p>
102
102
  </div></div>
103
103
 
104
104
  <div id="footer">
105
- Generated on Tue Jan 29 20:37:32 2013 by
105
+ Generated on Thu Jan 31 20:15:55 2013 by
106
106
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
107
107
  0.8.3 (ruby-1.9.3).
108
108
  </div>
@@ -103,7 +103,7 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Tue Jan 29 20:37:32 2013 by
106
+ Generated on Thu Jan 31 20:15:55 2013 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
108
  0.8.3 (ruby-1.9.3).
109
109
  </div>
data/lib/lazier.rb CHANGED
@@ -89,16 +89,10 @@ module Lazier
89
89
  def self.load_datetime
90
90
  Lazier.load_object
91
91
 
92
- ::Time.class_eval do
93
- include ::Lazier::DateTime
94
- end
95
-
96
- ::Date.class_eval do
97
- include ::Lazier::DateTime
98
- end
99
-
100
- ::DateTime.class_eval do
101
- include ::Lazier::DateTime
92
+ [::Time, ::Date, ::DateTime].each do |c|
93
+ c.class_eval do
94
+ include ::Lazier::DateTime
95
+ end
102
96
  end
103
97
 
104
98
  ::ActiveSupport::TimeZone.class_eval do
@@ -125,9 +125,11 @@ module Lazier
125
125
  year = ::Date.today.year if !year.is_integer?
126
126
 
127
127
  # Compute using Anonymouse Gregorian Algorithm: http://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm
128
- a, b, c = easter_independent(year)
129
- x, e, i, k = easter_dependent(b, c)
130
- day, month = easter_summarize(easter_finalize(a, x, e, i, k))
128
+ data = easter_start(year)
129
+ data = easter_part_1(data)
130
+ data = easter_part_2(year, data)
131
+ data = easter_part_3(year, data)
132
+ day, month = easter_end(data)
131
133
 
132
134
  ::Date.civil(year, month, day)
133
135
  end
@@ -165,17 +167,16 @@ module Lazier
165
167
  # Part one of Easter calculation.
166
168
  #
167
169
  # @param year [Fixnum] The year to compute the date for.
168
- # @return [Array] Partial variables for calculus.
169
- def easter_independent(year)
170
+ # @return [Array] Partial variables for #easter_part_1.
171
+ def easter_start(year)
170
172
  [year % 19, (year / 100.0).floor, year % 100]
171
173
  end
172
174
 
173
175
  # Part two of Easter calculation.
174
- #
175
- # @param b [Fixnum] Variable from #easter_independent.
176
- # @param c [Fixnum] Variable from #easter_independent.
177
- # @return [Array] Partial variables for calculus.
178
- def easter_dependent(b, c)
176
+ # @param data [Fixnum] Partial variables from #easter_start.
177
+ # @return [Array] Partial variables for #easter_part_2.
178
+ def easter_part_1(data)
179
+ a, b, c = data
179
180
  [
180
181
  b - (b / 4.0).floor - ((b - ((b + 8) / 25.0).floor + 1) / 3.0).floor,
181
182
  b % 4,
@@ -186,25 +187,30 @@ module Lazier
186
187
 
187
188
  # Part three of Easter calculation.
188
189
  #
189
- # @param a [Fixnum] Variable from #easter_independent.
190
- # @param x [Fixnum] Variable from #easter_independent.
191
- # @param e [Fixnum] Variable from #easter_dependent.
192
- # @param i [Fixnum] Variable from #easter_dependent.
193
- # @param k [Fixnum] Variable from #easter_dependent.
194
- # @return [Array] Partial variables for calculus.
195
- def easter_finalize(a, x, e, i, k)
190
+ # @param data [Fixnum] Partial variables from #easter_part_1.
191
+ # @return [Array] Partial variables for #easter_part_3.
192
+ def easter_part_2(year, data)
193
+ a = year % 19
194
+ x, e, i, k = data
196
195
  h = ((19 * a) + x + 15) % 30
197
- l = (32 + (2 * e) + (2 * i) - h - k) % 7
196
+ [h, (32 + (2 * e) + (2 * i) - h - k) % 7]
197
+ end
198
198
 
199
+ # Part four of Easter calculation
200
+ # @param data [Arrays] Partial variables from #easter_part_2.
201
+ # @return [Array] Partial variables for #easter_end.
202
+ def easter_part_3(year, data)
203
+ a = year % 19
204
+ h, l = data
199
205
  [h, l, ((a + (11 * h) + (22 * l)) / 451.0).floor]
200
206
  end
201
207
 
202
208
  # Final part of Easter calculation.
203
209
  #
204
- # @param prev [Fixnum] Variable from #easter_finalize.
205
- # @return [Array] Day and month of Easter daye.
206
- def easter_summarize(prev)
207
- h, l, m = prev
210
+ # @param data [Fixnum] Variable from #easter_part_3.
211
+ # @return [Array] Day and month of Easter day.
212
+ def easter_end(data)
213
+ h, l, m = data
208
214
  [((h + l - (7 * m) + 114) % 31) + 1, ((h + l - (7 * m) + 114) / 31.0).floor]
209
215
  end
210
216
  end
@@ -244,10 +250,7 @@ module Lazier
244
250
  # @param format [String] A format or a custom format name to use for formatting.
245
251
  # @return [String] The formatted date.
246
252
  def lstrftime(format = nil)
247
- rv = nil
248
- names = ::Lazier.settings.date_names
249
- substitutions = {"%a" => names[:short_days][self.wday], "%A" => names[:long_days][self.wday], "%b" => names[:short_months][self.month - 1], "%B" => names[:long_months][self.month - 1]}
250
- self.strftime(::DateTime.custom_format(format).ensure_string.gsub(/(?<!%)(%[ab])/i) {|mo| substitutions[mo] })
253
+ self.strftime(::DateTime.custom_format(format).ensure_string.gsub(/(?<!%)(%[ab])/i) {|mo| localize_time_component(mo) })
251
254
  end
252
255
 
253
256
  # Formats a datetime in the current timezone.
@@ -266,6 +269,17 @@ module Lazier
266
269
  def local_lstrftime(format = nil)
267
270
  (self.respond_to?(:in_time_zone) ? self.in_time_zone : self).lstrftime(format)
268
271
  end
272
+
273
+ private
274
+ # Returns a component of the date in the current locale.
275
+ #
276
+ # @param component [String] The component to localize.
277
+ # @return [String] The localized component.
278
+ def localize_time_component(component)
279
+ type = {"%a" => :short_days, "%A" => :long_days, "%b" => :short_months, "%B" => :long_months}.fetch(component, "")
280
+ index = component =~ /%a/i ? self.wday : self.month - 1
281
+ ::Lazier.settings.date_names.fetch(type, [])[index]
282
+ end
269
283
  end
270
284
 
271
285
  # Extensions for timezone objects.
@@ -319,9 +333,7 @@ module Lazier
319
333
  dst_label ||= "(DST)"
320
334
 
321
335
  @zones_names ||= { "STANDARD" => ::ActiveSupport::TimeZone.all.collect(&:to_s) }
322
- @zones_names["DST[#{dst_label}]-STANDARD"] ||= ::ActiveSupport::TimeZone.all.collect { |zone|
323
- zone.aliases.collect { |zone_alias| [zone.to_str(zone_alias), (zone.uses_dst? && zone_alias !~ /(#{Regexp.quote(dst_label)})$/) ? zone.to_str_with_dst(dst_label, nil, zone_alias) : nil] }
324
- }.flatten.compact.uniq.sort { |a,b| ::ActiveSupport::TimeZone.compare(a, b) } # Sort by name
336
+ @zones_names["DST[#{dst_label}]-STANDARD"] ||= ::ActiveSupport::TimeZone.all.collect { |zone| fetch_aliases(zone, dst_label) }.flatten.compact.uniq.sort { |a,b| ::ActiveSupport::TimeZone.compare(a, b) } # Sort by name
325
337
 
326
338
  @zones_names["#{with_dst ? "DST[#{dst_label}]-" : ""}STANDARD"]
327
339
  end
@@ -381,20 +393,24 @@ module Lazier
381
393
  right = right.to_str if right.is_a?(::ActiveSupport::TimeZone)
382
394
  left.ensure_string.split(" ", 2)[1] <=> right.ensure_string.split(" ", 2)[1]
383
395
  end
396
+
397
+ private
398
+ # Returns a list of aliases for a given time zone.
399
+ #
400
+ # @param zone [ActiveSupport::TimeZone] The zone.
401
+ # @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
402
+ def fetch_aliases(zone, dst_label = "(DST)")
403
+ zone.aliases.collect { |zone_alias|
404
+ [zone.to_str(zone_alias), (zone.uses_dst? && zone_alias !~ /(#{Regexp.quote(dst_label)})$/) ? zone.to_str_with_dst(dst_label, nil, zone_alias) : nil]
405
+ }
406
+ end
384
407
  end
385
408
 
386
409
  # Returns a list of valid aliases (city names) for this timezone (basing on offset).
387
410
  # @return [Array] A list of aliases for this timezone
388
411
  def aliases
389
412
  reference = self.class::MAPPING.fetch(self.name, self.name).gsub("_", " ")
390
-
391
- @aliases ||= ([reference] + self.class::MAPPING.collect { |name, zone|
392
- if zone.gsub("_", " ") == reference then
393
- (["International Date Line West", "UTC"].include?(name) || name.include?("(US & Canada)")) ? name : reference.gsub(/\/.*/, "/#{name}")
394
- else
395
- nil
396
- end
397
- }).uniq.compact.sort
413
+ @aliases ||= ([reference] + self.class::MAPPING.collect { |name, zone| format_alias(name, zone, reference) }).uniq.compact.sort
398
414
  end
399
415
 
400
416
  # Returns the current offset for this timezone, taking care of Daylight Saving Time (DST).
@@ -543,5 +559,20 @@ module Lazier
543
559
  rv = self.to_str_with_dst(dst_label, year, name)
544
560
  rv ? ::ActiveSupport::TimeZone.parameterize_zone(rv) : nil
545
561
  end
562
+
563
+ private
564
+ # Formats a time zone alias.
565
+ #
566
+ # @param name [String] The zone name.
567
+ # @param zone [String] The zone.
568
+ # @param reference [String] The main name for the zone.
569
+ # @return [String|nil] The formatted alias.
570
+ def format_alias(name, zone, reference)
571
+ if zone.gsub("_", " ") == reference then
572
+ (["International Date Line West", "UTC"].include?(name) || name.include?("(US & Canada)")) ? name : reference.gsub(/\/.*/, "/#{name}")
573
+ else
574
+ nil
575
+ end
576
+ end
546
577
  end
547
578
  end
data/lib/lazier/i18n.rb CHANGED
@@ -5,6 +5,7 @@
5
5
  #
6
6
 
7
7
  module Lazier
8
+ # Provides an easy way to localized messages in a class.
8
9
  module I18n
9
10
  # Setup all I18n translations.
10
11
  #
@@ -13,7 +13,7 @@ module Lazier
13
13
  MAJOR = 2
14
14
 
15
15
  # The minor version.
16
- MINOR = 4
16
+ MINOR = 5
17
17
 
18
18
  # The patch version.
19
19
  PATCH = 0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazier
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-30 00:00:00.000000000 Z
12
+ date: 2013-02-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json