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>
cycle.effective    # => 2017-12-07 00:00:00 UTC..2018-01-03 23:59:59 UTC
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



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

#dateDate (readonly)

Returns AIRAC effective on date.

Returns:

  • (Date)

    AIRAC effective on date



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

def date
  @date
end

#idInteger (readonly)

Returns AIRAC cycle ID.

Returns:

  • (Integer)

    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.

Parameters:

  • days (Numerical)

    add this many days

Returns:



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.

Parameters:

  • cycles (Numerical)

    subtract this many cycles

Returns:



70
71
72
# File 'lib/airac/cycle.rb', line 70

def -(cycles)
  self + -cycles
end

#<=>(other) ⇒ Integer

Returns:

  • (Integer)

See Also:

  • Object#<=>


76
77
78
# File 'lib/airac/cycle.rb', line 76

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

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

Returns:

  • (Boolean)

See Also:

  • Object#==


82
83
84
# File 'lib/airac/cycle.rb', line 82

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

#effectiveRange<Time>

Time range within which this cycle is effective.

Returns:

  • (Range<Time>)


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

#hashInteger

Returns:

  • (Integer)

See Also:

  • Object#hash


89
90
91
# File 'lib/airac/cycle.rb', line 89

def hash
  to_s.hash
end

#inspectString

Returns:

  • (String)


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

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)


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