hebrew_date 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -105,7 +105,7 @@
105
105
  </div>
106
106
 
107
107
  <div id="footer">
108
- Generated on Thu Jan 16 19:44:44 2014 by
108
+ Generated on Sun Jan 26 12:16:13 2014 by
109
109
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
110
110
  0.8.7.3 (ruby-2.0.0).
111
111
  </div>
data/hebrew_date.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'hebrew_date'
3
3
  s.require_paths = %w(. lib)
4
- s.version = '1.0.2'
5
- s.date = '2014-01-21'
4
+ s.version = '1.0.3'
5
+ s.date = '2014-01-26'
6
6
  s.summary = 'Hebrew/Jewish dates, times, and holidays.'
7
7
  s.description = <<-EOF
8
8
  hebrew_date is a library designed to provide information about the Jewish
data/lib/hebrew_date.rb CHANGED
@@ -12,8 +12,9 @@ require_relative 'support/parshiot.rb'
12
12
  # simultaneously. Note that you may call any Date methods on this class
13
13
  # and it should respond accordingly.
14
14
  class HebrewDate < Delegator
15
- include HebrewDateSupport::Parshiot
16
- include HebrewDateSupport::Holidays
15
+ include HebrewDateSupport::ParshaMethods
16
+ include HebrewDateSupport::HolidayMethods
17
+ # @!parse extend HebrewDateSupport::HolidayMethods::ClassMethods
17
18
 
18
19
  # @private
19
20
  HEBREW_EPOCH = -1373429
@@ -78,7 +79,23 @@ class HebrewDate < Delegator
78
79
 
79
80
  # @private
80
81
  def inspect
81
- strftime('*Y *-m *-d')
82
+ strftime('*Y-*-m-*-d (%Y-%-m-%-d)')
83
+ end
84
+
85
+ # Add the dates specified (in days).
86
+ # @param [Fixnum] other_date the days to add.
87
+ # @return [HebrewDate]
88
+ def +(other_date)
89
+ date = self.to_date + other_date
90
+ HebrewDate.new(date.year, date.month, date.day)
91
+ end
92
+
93
+ # Subtract the dates specified (in days).
94
+ # @param [Fixnum] other_date the days to subtract.
95
+ # @return [HebrewDate]
96
+ def -(other_date)
97
+ date = self.to_date - other_date
98
+ HebrewDate.new(date.year, date.month, date.day)
82
99
  end
83
100
 
84
101
  # @private
@@ -167,7 +184,7 @@ class HebrewDate < Delegator
167
184
  end
168
185
 
169
186
  # Move forward one day.
170
- # @return [void]
187
+ # @return [HebrewDate] the same HebrewDate object again, for chaining.
171
188
  def forward
172
189
  # Change Gregorian date
173
190
  if @date == last_day_of_month
@@ -207,10 +224,12 @@ class HebrewDate < Delegator
207
224
 
208
225
  # increment the absolute date
209
226
  @abs_date += 1
227
+
228
+ self
210
229
  end
211
230
 
212
231
  # Move back one day.
213
- # @return [void]
232
+ # @return [HebrewDate] the same HebrewDate object again, for chaining.
214
233
  def back
215
234
  # Change Gregorian date
216
235
  if @date == 1
@@ -244,6 +263,8 @@ class HebrewDate < Delegator
244
263
 
245
264
  # Change the absolute date
246
265
  @abs_date -= 1
266
+
267
+ self
247
268
  end
248
269
 
249
270
  # Get the name of the current Hebrew month.
@@ -278,18 +299,19 @@ class HebrewDate < Delegator
278
299
  # @param format [String]
279
300
  # @return [String]
280
301
  def strftime(format)
281
- format.gsub!('*Y', @hebrew_year.to_s)
282
- .gsub!('*m', @hebrew_month.to_s.rjust(2, '0'))
283
- .gsub!('*_m', @hebrew_month.to_s.rjust(2, ' '))
284
- .gsub!('*-m', @hebrew_month.to_s)
285
- .gsub!('*B', hebrew_month_to_s)
286
- .gsub!('*^B', hebrew_month_to_s.upcase)
287
- .gsub!('*b', hebrew_month_to_s[0, 3])
288
- .gsub!('*^b', hebrew_month_to_s[0, 3].upcase)
289
- .gsub!('*h', hebrew_month_to_s[0, 3])
290
- .gsub!('*d', @hebrew_date.to_s.rjust(2, '0'))
291
- .gsub!('*-d', @hebrew_date.to_s)
292
- .gsub!('*e', @hebrew_date.to_s.rjust(2, ' '))
302
+ format = format.gsub('*Y', @hebrew_year.to_s)
303
+ .gsub('*m', @hebrew_month.to_s.rjust(2, '0'))
304
+ .gsub('*_m', @hebrew_month.to_s.rjust(2, ' '))
305
+ .gsub('*-m', @hebrew_month.to_s)
306
+ .gsub('*B', hebrew_month_to_s)
307
+ .gsub('*^B', hebrew_month_to_s.upcase)
308
+ .gsub('*b', hebrew_month_to_s[0, 3])
309
+ .gsub('*^b', hebrew_month_to_s[0, 3].upcase)
310
+ .gsub('*h', hebrew_month_to_s[0, 3])
311
+ .gsub('*d', @hebrew_date.to_s.rjust(2, '0'))
312
+ .gsub('*-d', @hebrew_date.to_s)
313
+ .gsub('*e', @hebrew_date.to_s.rjust(2, ' '))
314
+ super(format)
293
315
  end
294
316
 
295
317
  # Get the day of the week.
@@ -318,18 +340,33 @@ class HebrewDate < Delegator
318
340
  end
319
341
  end
320
342
 
321
- # Is this a Hebrew leap year?
322
- # @param year [Integer] Used internally.
323
- # @return [Boolean]
324
- def hebrew_leap_year?(year=nil)
325
- year ||= @hebrew_year
326
- (((7 * year) + 1).remainder(19)) < 7
343
+ class << self
344
+ # Is this a Hebrew leap year?
345
+ # @param year [Integer] the Hebrew year.
346
+ # @return [Boolean]
347
+ def hebrew_leap_year?(year)
348
+ (((7 * year) + 1).remainder(19)) < 7
349
+ end
350
+
351
+ # The last month in the Hebrew year (12 or 13).
352
+ # @param year [Integer] the Hebrew year.
353
+ # @return [Integer]
354
+ def last_month_of_hebrew_year(year)
355
+ hebrew_leap_year?(year) ? 13 : 12
356
+ end
357
+
327
358
  end
328
359
 
329
360
  # The last month in this Hebrew year (12 or 13).
330
361
  # @return [Integer]
331
362
  def last_month_of_hebrew_year
332
- hebrew_leap_year? ? 13 : 12
363
+ self.class.last_month_of_hebrew_year(@hebrew_year)
364
+ end
365
+
366
+ # Is this a Hebrew leap year?
367
+ # @return [Boolean]
368
+ def hebrew_leap_year?
369
+ self.class.hebrew_leap_year?(@hebrew_year)
333
370
  end
334
371
 
335
372
  # Last day of the current Hebrew month.
@@ -407,10 +444,10 @@ class HebrewDate < Delegator
407
444
  if (conjunction_parts >= 19440) || # If new moon is at or after midday,
408
445
  (((conjunction_day.remainder(7)) == 2) && # ...or is on a Tuesday...
409
446
  (conjunction_parts >= 9924) && # at 9 hours, 204 parts or later...
410
- !hebrew_leap_year?(year)) || # ...of a common year,
447
+ !HebrewDate.hebrew_leap_year?(year)) || # ...of a common year,
411
448
  (((conjunction_day.remainder(7)) == 1) && # ...or is on a Monday at...
412
449
  (conjunction_parts >= 16789) && # 15 hours, 589 parts or later...
413
- (hebrew_leap_year?(year - 1))) # at the end of a leap year
450
+ (HebrewDate.hebrew_leap_year?(year - 1))) # at the end of a leap year
414
451
  # Then postpone Rosh HaShanah one day
415
452
  alternative_day += 1
416
453
  end
@@ -1,5 +1,136 @@
1
1
  module HebrewDateSupport
2
- module Holidays
2
+ module HolidayMethods
3
+ module ClassMethods
4
+
5
+ # A list of holidays which can be passed into the from_holiday method.
6
+ HOLIDAYS = [
7
+ :EREV_PESACH,
8
+ :PESACH,
9
+ :PESACH_2,
10
+ :PESACH_SHENI,
11
+ :YOM_HAATZMAUT,
12
+ :YOM_HAZIKARON,
13
+ :YOM_YERUSHALAYIM,
14
+ :LAG_BAOMER,
15
+ :EREV_SHAVUOT,
16
+ :SHAVUOT,
17
+ :TZOM_TAMMUZ,
18
+ :TISHA_BAV,
19
+ :TU_BAV,
20
+ :EREV_ROSH_HASHANA,
21
+ :ROSH_HASHANA,
22
+ :TZOM_GEDALIA,
23
+ :EREV_YOM_KIPPUR,
24
+ :YOM_KIPPUR,
25
+ :EREV_SUKKOT,
26
+ :SUKKOT,
27
+ :SHMINI_ATZERET,
28
+ :EREV_CHANUKAH,
29
+ :CHANUKAH,
30
+ :TZOM_TEVET,
31
+ :TU_BISHVAT,
32
+ :PURIM_KATAN,
33
+ :TAANIT_ESTHER,
34
+ :PURIM,
35
+ :SHUSHAN_PURIM
36
+ ]
37
+
38
+ # Given a holiday name, return a HebrewDate representing that holiday.
39
+ # @param holiday [Symbol] the name of the holiday. Possible values are
40
+ # in the HOLIDAYS array.
41
+ # @param year [Integer] if given, the Hebrew year to search. Defaults
42
+ # to the current year.
43
+ def from_holiday(holiday, year=nil)
44
+ year ||= HebrewDate.new.hebrew_year
45
+ case holiday
46
+ when :EREV_PESACH
47
+ HebrewDate.new_from_hebrew(year, 1, 14)
48
+ when :PESACH
49
+ HebrewDate.new_from_hebrew(year, 1, 15)
50
+ when :PESACH_2
51
+ HebrewDate.new_from_hebrew(year, 1, 21)
52
+ when :PESACH_SHENI
53
+ HebrewDate.new_from_hebrew(year, 2, 15)
54
+ when :YOM_HAZIKARON
55
+ HebrewDate.from_holiday(:YOM_HAATZMAUT).back
56
+ when :YOM_HAATZMAUT
57
+ date = HebrewDate.new_from_hebrew(year, 2, 5)
58
+ # Friday or Shabbat - move back to Thursday
59
+ # Monday - move forward to Tuesday
60
+ if date.day == 6
61
+ date.back
62
+ elsif date.day == 2
63
+ date.forward
64
+ elsif date.day == 7
65
+ date.set_hebrew_date(year, 2, 3)
66
+ end
67
+ when :LAG_BAOMER
68
+ HebrewDate.new_from_hebrew(year, 2, 18)
69
+ when :YOM_YERUSHALAYIM
70
+ HebrewDate.new_from_hebrew(year, 2, 28)
71
+ when :TZOM_TAMMUZ
72
+ date = HebrewDate.new_from_hebrew(year, 4, 17)
73
+ date.forward if date.day == 7
74
+ date
75
+ when :EREV_SHAVUOT
76
+ HebrewDate.new_from_hebrew(year, 3, 5)
77
+ when :SHAVUOT
78
+ HebrewDate.new_from_hebrew(year, 3, 6)
79
+ when :TISHA_BAV
80
+ date = HebrewDate.new_from_hebrew(year, 5, 9)
81
+ date.forward if date.day == 7
82
+ date
83
+ when :TU_BAV
84
+ HebrewDate.new_from_hebrew(year, 15, 9)
85
+ when :EREV_ROSH_HASHANA
86
+ HebrewDate.new_from_hebrew(year, 6, 29)
87
+ when :ROSH_HASHANA
88
+ HebrewDate.new_from_hebrew(year, 7, 1)
89
+ when :TZOM_GEDALIA
90
+ date = HebrewDate.new_from_hebrew(year, 7, 3)
91
+ date.forward if date.day == 7
92
+ date
93
+ when :EREV_YOM_KIPPUR
94
+ HebrewDate.new_from_hebrew(year, 7, 9)
95
+ when :YOM_KIPPUR
96
+ HebrewDate.new_from_hebrew(year, 7, 10)
97
+ when :EREV_SUKKOT
98
+ HebrewDate.new_from_hebrew(year, 7, 14)
99
+ when :SUKKOT
100
+ HebrewDate.new_from_hebrew(year, 7, 15)
101
+ when :SHMINI_ATZERET
102
+ HebrewDate.new_from_hebrew(year, 7, 22)
103
+ when :EREV_CHANUKAH
104
+ HebrewDate.new_from_hebrew(year, 9, 24)
105
+ when :CHANUKAH
106
+ HebrewDate.new_from_hebrew(year, 9, 25)
107
+ when :TZOM_TEVET
108
+ HebrewDate.new_from_hebrew(year, 10, 10)
109
+ when :TU_BISHVAT
110
+ HebrewDate.new_from_hebrew(year, 11, 15)
111
+ when :PURIM_KATAN
112
+ if HebrewDate.hebrew_leap_year?(year)
113
+ HebrewDate.new_from_hebrew(year, 12, 14)
114
+ end
115
+ when :TAANIT_ESTHER
116
+ date = HebrewDate.new_from_hebrew(year,
117
+ HebrewDate.last_month_of_hebrew_year(year), 13)
118
+ if date.day == 7
119
+ date.set_hebrew_date(year, date.hebrew_month, 11)
120
+ elsif date.day == 6
121
+ date.back
122
+ end
123
+ date
124
+ when :PURIM
125
+ HebrewDate.new_from_hebrew(year,
126
+ HebrewDate.last_month_of_hebrew_year(year), 14)
127
+ when :SHUSHAN_PURIM
128
+ HebrewDate.new_from_hebrew(year,
129
+ HebrewDate.last_month_of_hebrew_year(year), 15)
130
+ end
131
+ end
132
+ end
133
+
3
134
  # Returns a string of the Jewish holiday or fast day for the current day,
4
135
  # or an empty string if there is no holiday for this day.
5
136
  # @param generic [Boolean] whether you want just a generic name
@@ -252,5 +383,9 @@ module HebrewDateSupport
252
383
  end
253
384
  end
254
385
 
386
+ def self.included(base)
387
+ base.extend(ClassMethods)
388
+ end
389
+
255
390
  end
256
391
  end
@@ -1,5 +1,5 @@
1
1
  module HebrewDateSupport
2
- module Parshiot
2
+ module ParshaMethods
3
3
  # Parsha names in Ashkenazi and Sephardi pronunciation.
4
4
  # @private
5
5
  PARSHA_NAMES = [
@@ -47,4 +47,49 @@ describe HebrewDate do
47
47
  HebrewDate.new(2014, 12, 17).holiday.should == 'Chanukah'
48
48
  HebrewDate.new(2014, 12, 25).holiday.should be_nil
49
49
  end
50
+
51
+ specify 'from holidays' do
52
+ HebrewDate.from_holiday(:ROSH_HASHANA, 5774).to_date.
53
+ should == Date.new(2013, 9, 5)
54
+ HebrewDate.from_holiday(:TZOM_GEDALIA, 5774).to_date.
55
+ should == Date.new(2013, 9, 8)
56
+ HebrewDate.from_holiday(:YOM_KIPPUR, 5774).to_date.
57
+ should == Date.new(2013, 9, 14)
58
+ HebrewDate.from_holiday(:SUKKOT, 5774).to_date.
59
+ should == Date.new(2013, 9, 19)
60
+ HebrewDate.from_holiday(:SHMINI_ATZERET, 5774).to_date.
61
+ should == Date.new(2013, 9, 26)
62
+ HebrewDate.from_holiday(:CHANUKAH, 5774).to_date.
63
+ should == Date.new(2013, 11, 28)
64
+ HebrewDate.from_holiday(:TZOM_TEVET, 5774).to_date.
65
+ should == Date.new(2013, 12, 13)
66
+ HebrewDate.from_holiday(:TU_BISHVAT, 5774).to_date.
67
+ should == Date.new(2014, 1, 16)
68
+ HebrewDate.from_holiday(:PURIM_KATAN, 5774).to_date.
69
+ should == Date.new(2014, 2, 14)
70
+ HebrewDate.from_holiday(:TAANIT_ESTHER, 5774).to_date.
71
+ should == Date.new(2014, 3, 13)
72
+ HebrewDate.from_holiday(:PURIM, 5774).to_date.
73
+ should == Date.new(2014, 3, 16)
74
+ HebrewDate.from_holiday(:EREV_PESACH, 5774).to_date.
75
+ should == Date.new(2014, 4, 14)
76
+ HebrewDate.from_holiday(:PESACH, 5774).to_date.
77
+ should == Date.new(2014, 4, 15)
78
+ HebrewDate.from_holiday(:PESACH_2, 5774).to_date.
79
+ should == Date.new(2014, 4, 21)
80
+ HebrewDate.from_holiday(:YOM_HAZIKARON, 5774).to_date.
81
+ should == Date.new(2014, 5, 5)
82
+ HebrewDate.from_holiday(:YOM_HAATZMAUT, 5774).to_date.
83
+ should == Date.new(2014, 5, 6)
84
+ HebrewDate.from_holiday(:LAG_BAOMER, 5774).to_date.
85
+ should == Date.new(2014, 5, 18)
86
+ HebrewDate.from_holiday(:YOM_YERUSHALAYIM, 5774).to_date.
87
+ should == Date.new(2014, 5, 28)
88
+ HebrewDate.from_holiday(:SHAVUOT, 5774).to_date.
89
+ should == Date.new(2014, 6, 4)
90
+ HebrewDate.from_holiday(:TZOM_TAMMUZ, 5774).to_date.
91
+ should == Date.new(2014, 7, 15)
92
+ HebrewDate.from_holiday(:TISHA_BAV, 5774).to_date.
93
+ should == Date.new(2014, 8, 5)
94
+ end
50
95
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hebrew_date
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Orner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-21 00:00:00.000000000 Z
11
+ date: 2014-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard