Class: Aef::Weekling::Year

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/aef/weekling/year.rb

Overview

Immutable object representing a calendar year (according to ISO 8601).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ Year #initialize(date) ⇒ Year

Returns a new instance of Year.

Overloads:

  • #initialize(index) ⇒ Year

    Initialize by number.

    Parameters:

    • index (Integer)

      the year’s number

  • #initialize(date) ⇒ Year

    Initialize by a date-like object.

    Parameters:

    • date (Date, DateTime, Time)

      a date-like object



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

#indexInteger (readonly)

Returns the number of the year.

Returns:

  • (Integer)

    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

Note:

Looks for patterns like this: 2011

Parses the first year out of a string.

Parameters:

  • string (String)

    a string containing a year representation

Returns:

Raises:

  • (ArgumentError)

    if pattern cannot be found



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

.todayAef::Weekling::Year Also known as: now

Initializes the current year.

Returns:



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.

Examples:

3 year after 2000

Aef::Weekling::Year.new(2000) + 3
# => #<Aef::Weekling::Year: 2003>

Parameters:

  • other (Integer)

    number of years to add

Returns:



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.

Examples:

3 before 2000

Aef::Weekling::Year.new(2000) - 3
# => #<Aef::Weekling::Year: 1997>

Parameters:

  • other (Integer)

    number of years to subtract

Returns:



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.

Parameters:

Returns:

  • (-1, 0, 1)

    -1 if other is greater, 0 if other is equal and 1 if other is lesser than self



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.

Parameters:

Returns:

  • (true, false)

    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.

Parameters:

Returns:

  • (true, false)

    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.

Returns:

  • (true, false)

    true if the year is even



208
209
210
# File 'lib/aef/weekling/year.rb', line 208

def even?
  @index.even?
end

#hashsee Integer#hash

Returns identity hash for hash table usage.

Returns:

  • (see Integer#hash)

    identity hash for hash table usage



131
132
133
# File 'lib/aef/weekling/year.rb', line 131

def hash
  @index.hash
end

#inspectObject

Represents a week as String for debugging.

Examples:

Output of the year 2012

Aef::Weekling::Year.new(2012).inspect
# => "<#Aef::Weekling::Year: 2012>"


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.

Returns:

  • (true, false)

    true is the year is a leap year



215
216
217
# File 'lib/aef/weekling/year.rb', line 215

def leap?
  Date.leap?(to_i)
end

#nextAef::Weekling::Year Also known as: succ

Finds the following year.

Examples:

The year after some other year

some_year = Aef::Weekling::Year.new(2012)
some_year.next
# => #<Aef::Weekling::Year: 2013>

Returns:



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.

Returns:

  • (true, false)

    true if the year is odd



201
202
203
# File 'lib/aef/weekling/year.rb', line 201

def odd?
  @index.odd?
end

#previousAef::Weekling::Year Also known as: pred

Finds the previous year.

Examples:

The year before some other year

some_year = Aef::Weekling::Year.new(2012)
some_year.previous
# => #<Aef::Weekling::Year: 2011>

Returns:



168
169
170
# File 'lib/aef/weekling/year.rb', line 168

def previous
  self.class.new(@index - 1)
end

#to_iInteger

Represents the year as Integer.

Returns:

  • (Integer)

    the year’s number



86
87
88
# File 'lib/aef/weekling/year.rb', line 86

def to_i
  @index
end

#to_sString

Represents the year as String in ISO 8601 format.

Examples:

Output of the year 2012

Aef::Weekling::Year.new(2012).to_s
# => "2012"

Returns:

  • (String)

    a character representation of the year



97
98
99
# File 'lib/aef/weekling/year.rb', line 97

def to_s
  @index.to_s
end

#to_yearAef::Weekling::Year

Returns self reference.

Returns:



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.

Parameters:

  • index (Integer)

    the week’s index

Returns:



238
239
240
# File 'lib/aef/weekling/year.rb', line 238

def week(index)
  Week.new(self, index.to_i)
end

#week_countInteger

Calculates the amount of weeks in year.

Examples:

Acquire the amount of weeks for 2015

Aef::Weekling::Year.new(2015).week_count
# => 53

Returns:

  • (Integer)

    the amount of weeks



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

#weeksRange<Aef::Weekling::Week>

Returns a range of all weeks in year.

Returns:



245
246
247
# File 'lib/aef/weekling/year.rb', line 245

def weeks
  week(1)..week(week_count)
end