Class: AIRAC::Cycle

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/airac/cycle.rb

Overview

AIRAC cycle date calculations

Examples:

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

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

Instance Method Summary collapse

Constructor Details

#initialize(raw_cycle = nil) ⇒ Cycle

Returns a new instance of Cycle.

Parameters:

  • raw_cycle (Date, String, nil) (defaults to: nil)

    either a four digit AIRAC cycle ID or any date within the AIRAC 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

#dateDate (readonly)

Returns AIRAC effective on date.

Returns:

  • (Date)

    AIRAC effective on date



22
23
24
# File 'lib/airac/cycle.rb', line 22

def date
  @date
end

#idInteger (readonly)

Returns AIRAC cycle ID.

Returns:

  • (Integer)

    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.

Parameters:

  • days (Numerical)

    add this many days

Returns:



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.

Parameters:

  • cycles (Numerical)

    subtract this many cycles

Returns:



61
62
63
# File 'lib/airac/cycle.rb', line 61

def -(cycles)
  self + -cycles
end

#<=>(other) ⇒ Integer

Returns:

  • (Integer)

See Also:

  • Object#<=>


67
68
69
# File 'lib/airac/cycle.rb', line 67

def <=>(other)
  id <=> other.id
end

#==(other) ⇒ Boolean Also known as: eql?

Returns:

  • (Boolean)

See Also:

  • Object#==


73
74
75
# File 'lib/airac/cycle.rb', line 73

def ==(other)
  self.class === other  && (self <=> other).zero?
end

#hashInteger

Returns:

  • (Integer)

See Also:

  • Object#hash


80
81
82
# File 'lib/airac/cycle.rb', line 80

def hash
  to_s.hash
end

#inspectString

Returns:

  • (String)


42
43
44
# File 'lib/airac/cycle.rb', line 42

def inspect
  %Q(#<#{self.class} #{id} #{date}>)
end

#to_s(template = nil) ⇒ String

Parameters:

  • template (String, nil) (defaults to: nil)

    strftime template with %i for AIRAC cycle (default: ‘%i %Y-%m-%d’)

Returns:

  • (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