class AIRAC::Cycle
AIRAC
cycle date calculations
@example
cycle = AIRAC::Cycle.new('2018-01-01') cycle.date # => #<Date: 2017-12-07 ((2458095j,0s,0n),+0s,2299161j)> cycle.id # => 1713 (cycle + 5).id # => 1804 (cycle - 5).id # => 1708 (cycle - 100).id # => ArgumentError
Constants
Attributes
date[R]
@return [Date] AIRAC
effective on date
id[R]
@return [Integer] AIRAC
cycle ID
Public Class Methods
new(raw_cycle=nil)
click to toggle source
@param raw_cycle [Date, String, nil] either a four digit AIRAC
cycle ID
or any date within the AIRAC cycle
# File lib/airac/cycle.rb 29 def initialize(raw_cycle=nil) 30 if raw_cycle.to_s.match?(/\A\d{4}\z/) 31 @id = raw_cycle.to_s 32 @date = date_for_id(@id) 33 else 34 raw_cycle = raw_cycle ? Date.parse(raw_cycle.to_s) : Date.today 35 fail(ArgumentError, "cannot calculate dates before #{ROOT_DATE}") if raw_cycle < ROOT_DATE 36 @date = date_for_date_within(raw_cycle) 37 @id = id_for(@date) 38 end 39 end
Public Instance Methods
+(cycles)
click to toggle source
@param days [Numerical] add this many days @return [AIRAC::Cycle] new object with offset applied
# File lib/airac/cycle.rb 55 def +(cycles) 56 AIRAC::Cycle.new(@date + cycles * DAYS_PER_CYCLE) 57 end
-(cycles)
click to toggle source
@param cycles [Numerical] subtract this many cycles @return [AIRAC::Cycle] new object with offset applied
# File lib/airac/cycle.rb 61 def -(cycles) 62 self + -cycles 63 end
<=>(other)
click to toggle source
@see Object#<=> @return [Integer]
# File lib/airac/cycle.rb 67 def <=>(other) 68 id <=> other.id 69 end
==(other)
click to toggle source
@see Object#== @return [Boolean]
# File lib/airac/cycle.rb 73 def ==(other) 74 self.class === other && (self <=> other).zero? 75 end
Also aliased as: eql?
hash()
click to toggle source
@see Object#hash @return [Integer]
# File lib/airac/cycle.rb 80 def hash 81 to_s.hash 82 end
inspect()
click to toggle source
@return [String]
# File lib/airac/cycle.rb 42 def inspect 43 %Q(#<#{self.class} #{id} #{date}>) 44 end
to_s(template=nil)
click to toggle source
@param template [String, nil] strftime template with %i for AIRAC
cycle
(default: '%i %Y-%m-%d')
@return [String]
# File lib/airac/cycle.rb 49 def to_s(template=nil) 50 date.strftime((template || '%i %Y-%m-%d').sub(/%i/, id)) 51 end
Private Instance Methods
date_for_date_within(date_within)
click to toggle source
Find the AIRAC
date for given date within the cycle
# File lib/airac/cycle.rb 87 def date_for_date_within(date_within) 88 ROOT_DATE + (date_within - ROOT_DATE).to_i / DAYS_PER_CYCLE * DAYS_PER_CYCLE 89 end
date_for_id(id)
click to toggle source
Find the AIRAC
date for given cycle ID
# File lib/airac/cycle.rb 92 def date_for_id(id) 93 year = (Date.today.year.to_s[0,2] + id[0,2]).to_i 94 preceding_cycle = self.class.new(Date.parse("#{year}-01-01") - 1) 95 (preceding_cycle + id[2,2].to_i).date 96 end