Class: EDTF::Season

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable, Enumerable
Defined in:
lib/edtf/season.rb

Constant Summary collapse

SEASONS =
Hash[21, :spring, 22, :summer, 23, :autumn, 24, :winter].freeze
CODES =
Hash.new { |h,k| h.fetch(k.to_sym, nil) }.merge(
SEASONS.invert).merge({ :fall => 23 }).freeze
NORTHERN =
Hash[:spring, [3,4,5], :summer, [6,7,8], :autumn, [9,10,11], :winter, [12,1,2]].freeze
SOUTHERN =
Hash[:autumn, [3,4,5], :winter, [6,7,8], :spring, [9,10,11], :summer, [12,1,2]].freeze
NORTHERN_MONTHS =
Hash[*NORTHERN.map { |s,ms| ms.map { |m| [m,s] } }.flatten].freeze
SOUTHERN_MONTHS =
Hash[*SOUTHERN.map { |s,ms| ms.map { |m| [m,s] } }.flatten].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments) ⇒ Season

Returns a new instance of Season.

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/edtf/season.rb', line 49

def initialize(*arguments)
  arguments.flatten!
  raise ArgumentError, "wrong number of arguments (#{arguments.length} for 0..3)" if arguments.length > 3

  if arguments.length == 1
    case arguments[0]
    when Date
      @year, @season = arguments[0].year, NORTHERN_MONTHS[arguments[0]]
    when Symbol, String
      @year, @season = Date.today.year, SEASONS[CODES[arguments[0].to_sym]]
    else
      self.year = arguments[0]
      @season = NORTHERN_MONTHS[Date.today.month]
    end
  else
    self.year      = arguments[0] || Date.today.year
    self.season    = arguments[1] || NORTHERN_MONTHS[Date.today.month]
    self.qualifier = qualifier
  end
end

Instance Attribute Details

#approximateObject

Returns the value of attribute approximate.



29
30
31
# File 'lib/edtf/season.rb', line 29

def approximate
  @approximate
end

#qualifierObject

Returns the value of attribute qualifier.



29
30
31
# File 'lib/edtf/season.rb', line 29

def qualifier
  @qualifier
end

#seasonObject

Returns the value of attribute season.



27
28
29
# File 'lib/edtf/season.rb', line 27

def season
  @season
end

#uncertainObject

Returns the value of attribute uncertain.



29
30
31
# File 'lib/edtf/season.rb', line 29

def uncertain
  @uncertain
end

#yearObject

Returns the value of attribute year.



27
28
29
# File 'lib/edtf/season.rb', line 27

def year
  @year
end

Class Method Details

.currentObject



22
23
24
# File 'lib/edtf/season.rb', line 22

def current
  Date.today.season
end

Instance Method Details

#<=>(other) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/edtf/season.rb', line 139

def <=>(other)
  case other
  when Date
    cover?(other) ? 0 : to_date <=> other
  when Interval, Epoch
    [min, max] <=> [other.min, other.max]
  when Season
    [year, month, qualifier] <=> [other.year, other.month, other.qualifier]
  else
    nil
  end
rescue
  nil
end

#===(other) ⇒ Object



154
155
156
157
158
# File 'lib/edtf/season.rb', line 154

def ===(other)
  (self <=> other) == 0
rescue
  false
end

#certain!Object



84
85
86
87
# File 'lib/edtf/season.rb', line 84

def certain!
  @uncertain = false
  self
end

#certain?Boolean

Returns:

  • (Boolean)


81
# File 'lib/edtf/season.rb', line 81

def certain?; !uncertain; end

#cover?(other) ⇒ Boolean

def next(n = 1) end

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/edtf/season.rb', line 104

def cover?(other)
  return false unless other.respond_to?(:day_precision)
  other = other.day_precision
  min.day_precision! <= other && other <= max.day_precision!
end

#each(&block) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/edtf/season.rb', line 110

def each(&block)
  if block_given?
    to_range.each(&block)
    self
  else
    to_enum
  end
end

#edtfObject Also known as: to_s



132
133
134
# File 'lib/edtf/season.rb', line 132

def edtf
  '%04d-%2d%s' % [year, CODES[season], qualified? ? "^#{qualifier}" : '']
end

#maxObject



166
167
168
# File 'lib/edtf/season.rb', line 166

def max
  to_date.months_since(2).end_of_month
end

#precise!Object



89
90
91
# File 'lib/edtf/season.rb', line 89

def precise!
  @approximate = false
end

#precise?Boolean

Returns:

  • (Boolean)


82
# File 'lib/edtf/season.rb', line 82

def precise?; !approximate; end

#qualified?Boolean

Returns:

  • (Boolean)


130
# File 'lib/edtf/season.rb', line 130

def qualified?; !!@qualifier; end

#season?Boolean

Returns:

  • (Boolean)


128
# File 'lib/edtf/season.rb', line 128

def season?; true; end

#succObject

Returns the next season.



94
95
96
97
98
99
# File 'lib/edtf/season.rb', line 94

def succ
  s = dup
  s.season = next_season_code
  s.year = year + 1 if s.first?
  s
end

#to_dateObject Also known as: min



160
161
162
# File 'lib/edtf/season.rb', line 160

def to_date
  Date.new(year, month, 1)
end

#to_rangeObject

Returns a Range that covers the season (a three month period).



171
172
173
# File 'lib/edtf/season.rb', line 171

def to_range
  min .. max
end