Class: AIRAC::Cycle
Overview
AIRAC cycle date calculations
Constant Summary collapse
- ROOT_DATE =
First AIRAC date following the last cycle length modification
Date.parse('2015-06-25').freeze
- DAYS_PER_CYCLE =
Length of one AIRAC cycle
28
Instance Attribute Summary collapse
-
#date ⇒ Date
readonly
AIRAC effective on date.
-
#id ⇒ Integer
readonly
AIRAC cycle ID.
Instance Method Summary collapse
-
#+(cycles) ⇒ AIRAC::Cycle
New object with offset applied.
-
#-(cycles) ⇒ AIRAC::Cycle
New object with offset applied.
- #<=>(other) ⇒ Integer
- #==(other) ⇒ Boolean (also: #eql?)
-
#effective ⇒ Range<Time>
Time range within which this cycle is effective.
- #hash ⇒ Integer
-
#initialize(raw_cycle = nil) ⇒ Cycle
constructor
A new instance of Cycle.
- #inspect ⇒ String
- #to_s(template = nil) ⇒ String
Constructor Details
#initialize(raw_cycle = nil) ⇒ Cycle
Returns a new instance of Cycle.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/airac/cycle.rb', line 30 def initialize(raw_cycle=nil) if raw_cycle.to_s.match?(/\A\d{4}\z/) @id = raw_cycle.to_s @date = date_for_id(@id) else raw_cycle = raw_cycle ? Date.parse(raw_cycle.to_s) : Date.today fail(ArgumentError, "cannot calculate dates before #{ROOT_DATE}") if raw_cycle < ROOT_DATE @date = date_for_date_within(raw_cycle) @id = id_for(@date) end end |
Instance Attribute Details
#date ⇒ Date (readonly)
Returns AIRAC effective on date.
23 24 25 |
# File 'lib/airac/cycle.rb', line 23 def date @date end |
#id ⇒ Integer (readonly)
Returns AIRAC cycle ID.
26 27 28 |
# File 'lib/airac/cycle.rb', line 26 def id @id end |
Instance Method Details
#+(cycles) ⇒ AIRAC::Cycle
Returns new object with offset applied.
64 65 66 |
# File 'lib/airac/cycle.rb', line 64 def +(cycles) AIRAC::Cycle.new(@date + cycles * DAYS_PER_CYCLE) end |
#-(cycles) ⇒ AIRAC::Cycle
Returns new object with offset applied.
70 71 72 |
# File 'lib/airac/cycle.rb', line 70 def -(cycles) self + -cycles end |
#<=>(other) ⇒ Integer
76 77 78 |
# File 'lib/airac/cycle.rb', line 76 def <=>(other) id <=> other.id end |
#==(other) ⇒ Boolean Also known as: eql?
82 83 84 |
# File 'lib/airac/cycle.rb', line 82 def ==(other) self.class === other && (self <=> other).zero? end |
#effective ⇒ Range<Time>
Time range within which this cycle is effective.
57 58 59 60 |
# File 'lib/airac/cycle.rb', line 57 def effective next_date = date + DAYS_PER_CYCLE (Time.utc(date.year, date.month, date.day)..(Time.utc(next_date.year, next_date.month, next_date.day) - 1)) end |
#hash ⇒ Integer
89 90 91 |
# File 'lib/airac/cycle.rb', line 89 def hash to_s.hash end |
#inspect ⇒ String
43 44 45 |
# File 'lib/airac/cycle.rb', line 43 def inspect %Q(#<#{self.class} #{id} #{date}>) end |
#to_s(template = nil) ⇒ String
50 51 52 |
# File 'lib/airac/cycle.rb', line 50 def to_s(template=nil) date.strftime((template || '%i %Y-%m-%d').sub(/%i/, id)) end |