Class: EDTF::Season
- Inherits:
-
Object
- Object
- EDTF::Season
- 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
-
#qualifier ⇒ Object
Returns the value of attribute qualifier.
-
#season ⇒ Object
Returns the value of attribute season.
-
#year ⇒ Object
Returns the value of attribute year.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #===(other) ⇒ Object
-
#initialize(*arguments) ⇒ Season
constructor
A new instance of Season.
- #qualified? ⇒ Boolean
- #season? ⇒ Boolean
- #to_date ⇒ Object
-
#to_range ⇒ Object
def include?(other) case other when Date d = to_date other >= d && other <= d.months_since(3).end_of_month else false end end.
- #to_s ⇒ Object (also: #to_edtf)
Constructor Details
#initialize(*arguments) ⇒ Season
Returns a new instance of Season.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/edtf/season.rb', line 47 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].intern]] 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
#qualifier ⇒ Object
Returns the value of attribute qualifier.
29 30 31 |
# File 'lib/edtf/season.rb', line 29 def qualifier @qualifier end |
#season ⇒ Object
Returns the value of attribute season.
27 28 29 |
# File 'lib/edtf/season.rb', line 27 def season @season end |
#year ⇒ Object
Returns the value of attribute year.
27 28 29 |
# File 'lib/edtf/season.rb', line 27 def year @year end |
Class Method Details
.current ⇒ Object
22 23 24 |
# File 'lib/edtf/season.rb', line 22 def current Date.today.season end |
Instance Method Details
#<=>(other) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/edtf/season.rb', line 87 def <=>(other) case other when Date include?(other) ? 0 : to_date <=> other when Season [year, month, qualifier] <=> [other.year, other.month, other.qualifier] else nil end rescue nil end |
#===(other) ⇒ Object
100 101 102 103 104 |
# File 'lib/edtf/season.rb', line 100 def ===(other) (self <=> other) == 0 rescue false end |
#qualified? ⇒ Boolean
79 |
# File 'lib/edtf/season.rb', line 79 def qualified?; !!@qualifier; end |
#season? ⇒ Boolean
77 |
# File 'lib/edtf/season.rb', line 77 def season?; true; end |
#to_date ⇒ Object
106 107 108 |
# File 'lib/edtf/season.rb', line 106 def to_date Date.new(year, month) end |
#to_range ⇒ Object
def include?(other)
case other
when Date
d = to_date
other >= d && other <= d.months_since(3).end_of_month
else
false
end
end
120 121 122 123 |
# File 'lib/edtf/season.rb', line 120 def to_range d = to_date d .. d.months_since(3).end_of_month end |
#to_s ⇒ Object Also known as: to_edtf
81 82 83 |
# File 'lib/edtf/season.rb', line 81 def to_s '%04d-%2d%s' % [year, CODES[season], qualified? ? "^#{qualifier}" : ''] end |