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)


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

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



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

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



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

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

#certain!Object



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

def certain!
	@uncertain = false
	self
end

#certain?Boolean

Returns:

  • (Boolean)


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

def certain?; !uncertain; end

#cover?(other) ⇒ Boolean

def next(n = 1) end

Returns:

  • (Boolean)


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

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

#eachObject



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

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

#edtfObject Also known as: to_s



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

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

#maxObject



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

def max
	to_date.months_since(2).end_of_month
end

#precise!Object



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

def precise!
	@approximate = false
end

#precise?Boolean

Returns:

  • (Boolean)


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

def precise?; !approximate; end

#qualified?Boolean

Returns:

  • (Boolean)


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

def qualified?; !!@qualifier; end

#season?Boolean

Returns:

  • (Boolean)


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

def season?; true; end

#succObject

Returns the next season.



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

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

#to_dateObject Also known as: min



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

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

#to_rangeObject

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



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

def to_range
			min .. max
end