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?)
- #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.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/airac/cycle.rb', line 29 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.
22 23 24 |
# File 'lib/airac/cycle.rb', line 22 def date @date end |
#id ⇒ Integer (readonly)
Returns AIRAC cycle ID.
25 26 27 |
# File 'lib/airac/cycle.rb', line 25 def id @id end |
Instance Method Details
#+(cycles) ⇒ AIRAC::Cycle
Returns new object with offset applied.
55 56 57 |
# File 'lib/airac/cycle.rb', line 55 def +(cycles) AIRAC::Cycle.new(@date + cycles * DAYS_PER_CYCLE) end |
#-(cycles) ⇒ AIRAC::Cycle
Returns new object with offset applied.
61 62 63 |
# File 'lib/airac/cycle.rb', line 61 def -(cycles) self + -cycles end |
#<=>(other) ⇒ Integer
67 68 69 |
# File 'lib/airac/cycle.rb', line 67 def <=>(other) id <=> other.id end |
#==(other) ⇒ Boolean Also known as: eql?
73 74 75 |
# File 'lib/airac/cycle.rb', line 73 def ==(other) self.class === other && (self <=> other).zero? end |
#hash ⇒ Integer
80 81 82 |
# File 'lib/airac/cycle.rb', line 80 def hash to_s.hash end |
#inspect ⇒ String
42 43 44 |
# File 'lib/airac/cycle.rb', line 42 def inspect %Q(#<#{self.class} #{id} #{date}>) end |
#to_s(template = nil) ⇒ String
49 50 51 |
# File 'lib/airac/cycle.rb', line 49 def to_s(template=nil) date.strftime((template || '%i %Y-%m-%d').sub(/%i/, id)) end |