Class: Aef::Weekling::Year
- Inherits:
-
Object
- Object
- Aef::Weekling::Year
- Includes:
- Comparable
- Defined in:
- lib/aef/weekling/year.rb
Overview
Immutable object representing a calendar year (according to ISO 8601).
Instance Attribute Summary collapse
-
#index ⇒ Integer
readonly
The number of the year.
Class Method Summary collapse
-
.parse(string) ⇒ Aef::Weekling::Year
Parses the first year out of a string.
-
.today ⇒ Aef::Weekling::Year
(also: now)
Initializes the current year.
Instance Method Summary collapse
-
#+(other) ⇒ Aef::Weekling::Year
Adds years to the year.
-
#-(other) ⇒ Aef::Weekling::Year
Subtracts years from the year.
-
#<=>(other) ⇒ -1, ...
Compares the year with another to determine its relative position.
-
#==(other) ⇒ true, false
True if other has the same index.
-
#eql?(other) ⇒ true, false
True if other has the same index and is of the same or descending class.
-
#even? ⇒ true, false
States if the year’s index is even.
-
#hash ⇒ see Integer#hash
Identity hash for hash table usage.
-
#initialize(index_or_date) ⇒ Year
constructor
A new instance of Year.
-
#inspect ⇒ Object
Represents a week as String for debugging.
-
#leap? ⇒ true, false
States if the year is a leap year.
-
#next ⇒ Aef::Weekling::Year
(also: #succ)
Finds the following year.
-
#odd? ⇒ true, false
States if the year’s index is odd.
-
#previous ⇒ Aef::Weekling::Year
(also: #pred)
Finds the previous year.
-
#to_i ⇒ Integer
Represents the year as Integer.
-
#to_s ⇒ String
Represents the year as String in ISO 8601 format.
-
#to_year ⇒ Aef::Weekling::Year
Self reference.
-
#week(index) ⇒ Aef::Weekling::Week
Finds a week in the year.
-
#week_count ⇒ Integer
Calculates the amount of weeks in year.
-
#weeks ⇒ Range<Aef::Weekling::Week>
Returns a range of all weeks in year.
Constructor Details
#initialize(index) ⇒ Year #initialize(date) ⇒ Year
Returns a new instance of Year.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/aef/weekling/year.rb', line 72 def initialize(index_or_date) if index_or_date.respond_to?(:to_date) date = index_or_date.to_date @index = date.year elsif index_or_date.respond_to?(:to_i) @index = index_or_date.to_i else raise ArgumentError, 'A single argument must either respond to #to_date or to #to_i' end end |
Instance Attribute Details
#index ⇒ Integer (readonly)
Returns the number of the year.
63 64 65 |
# File 'lib/aef/weekling/year.rb', line 63 def index @index end |
Class Method Details
.parse(string) ⇒ Aef::Weekling::Year
Looks for patterns like this: 2011
Parses the first year out of a string.
52 53 54 55 56 57 58 59 |
# File 'lib/aef/weekling/year.rb', line 52 def parse(string) if sub_matches = PARSE_PATTERN.match(string.to_s) original, year = *sub_matches new(year.to_i) else raise ArgumentError, 'No year found for parsing' end end |
.today ⇒ Aef::Weekling::Year Also known as: now
Initializes the current year.
37 38 39 40 41 |
# File 'lib/aef/weekling/year.rb', line 37 def today today = Date.today new(today.year) end |
Instance Method Details
#+(other) ⇒ Aef::Weekling::Year
Adds years to the year.
182 183 184 |
# File 'lib/aef/weekling/year.rb', line 182 def +(other) self.class.new(@index + other.to_i) end |
#-(other) ⇒ Aef::Weekling::Year
Subtracts years from the year.
194 195 196 |
# File 'lib/aef/weekling/year.rb', line 194 def -(other) self.class.new(@index - other.to_i) end |
#<=>(other) ⇒ -1, ...
Compares the year with another to determine its relative position.
140 141 142 143 144 |
# File 'lib/aef/weekling/year.rb', line 140 def <=>(other) other_year = other.to_year self.index <=> other_year.index end |
#==(other) ⇒ true, false
Returns true if other has the same index.
117 118 119 120 121 |
# File 'lib/aef/weekling/year.rb', line 117 def ==(other) other_year = self.class.new(other) self.index == other_year.index end |
#eql?(other) ⇒ true, false
Returns true if other has the same index and is of the same or descending class.
126 127 128 |
# File 'lib/aef/weekling/year.rb', line 126 def eql?(other) self == other and other.is_a?(self.class) end |
#even? ⇒ true, false
States if the year’s index is even.
208 209 210 |
# File 'lib/aef/weekling/year.rb', line 208 def even? @index.even? end |
#hash ⇒ see Integer#hash
Returns identity hash for hash table usage.
131 132 133 |
# File 'lib/aef/weekling/year.rb', line 131 def hash @index.hash end |
#inspect ⇒ Object
Represents a week as String for debugging.
106 107 108 |
# File 'lib/aef/weekling/year.rb', line 106 def inspect "#<#{self.class.name}: #{to_s}>" end |
#leap? ⇒ true, false
States if the year is a leap year.
215 216 217 |
# File 'lib/aef/weekling/year.rb', line 215 def leap? Date.leap?(to_i) end |
#next ⇒ Aef::Weekling::Year Also known as: succ
Finds the following year.
154 155 156 |
# File 'lib/aef/weekling/year.rb', line 154 def next self.class.new(@index + 1) end |
#odd? ⇒ true, false
States if the year’s index is odd.
201 202 203 |
# File 'lib/aef/weekling/year.rb', line 201 def odd? @index.odd? end |
#previous ⇒ Aef::Weekling::Year Also known as: pred
Finds the previous year.
168 169 170 |
# File 'lib/aef/weekling/year.rb', line 168 def previous self.class.new(@index - 1) end |
#to_i ⇒ Integer
Represents the year as Integer.
86 87 88 |
# File 'lib/aef/weekling/year.rb', line 86 def to_i @index end |
#to_s ⇒ String
Represents the year as String in ISO 8601 format.
97 98 99 |
# File 'lib/aef/weekling/year.rb', line 97 def to_s @index.to_s end |
#to_year ⇒ Aef::Weekling::Year
Returns self reference.
111 112 113 |
# File 'lib/aef/weekling/year.rb', line 111 def to_year self end |
#week(index) ⇒ Aef::Weekling::Week
Finds a week in the year.
238 239 240 |
# File 'lib/aef/weekling/year.rb', line 238 def week(index) Week.new(self, index.to_i) end |
#week_count ⇒ Integer
Calculates the amount of weeks in year.
226 227 228 229 230 231 232 |
# File 'lib/aef/weekling/year.rb', line 226 def week_count date = Date.new(index, 12, 31) date = date - 7 if date.cweek == 1 date.cweek end |
#weeks ⇒ Range<Aef::Weekling::Week>
Returns a range of all weeks in year.
245 246 247 |
# File 'lib/aef/weekling/year.rb', line 245 def weeks week(1)..week(week_count) end |