Class: MonthYear

Inherits:
Object
  • Object
show all
Defined in:
lib/month_year.rb,
lib/month_year/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, month) ⇒ MonthYear

Instantiate a new ‘MonthYear` object.

Example:

>> MonthYear.new(2016, 12)
=> #<MonthYear:0x00000001f15c10 @month=12, @year=2016>

Arguments:

year: (Integer)
month: (Integer)

Raises:

  • (ArgumentError)


47
48
49
50
51
# File 'lib/month_year.rb', line 47

def initialize(year, month)
  raise ArgumentError unless [year, month].all? {|v| Fixnum === v }
  raise ArgumentError unless (1..12).cover?(month)
  @year, @month = year, month
end

Instance Attribute Details

#monthObject (readonly)

Returns the value of attribute month.



5
6
7
# File 'lib/month_year.rb', line 5

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



5
6
7
# File 'lib/month_year.rb', line 5

def year
  @year
end

Class Method Details

.dump(month_year) ⇒ Object



31
32
33
34
# File 'lib/month_year.rb', line 31

def dump(month_year)
  raise_dump_error(month_year) unless month_year.is_a?(self)
  month_year.to_i
end

.from_date(date) ⇒ Object

Instantiate a ‘MonthYear` object from a `Date`-like object (a `Date` or `Time` instance, or in fact from any object responding to :year and :month).

Example:

>> MonthYear.from_date(Date.today)
=> #<MonthYear:0x00000001f15c10 @month=12, @year=2016>

Arguments:

date: (Date)

Raises:

  • (ArgumentError)


19
20
21
22
# File 'lib/month_year.rb', line 19

def from_date(date)
  raise ArgumentError unless [:year, :month].all? {|v| date.respond_to?(v) }
  self.new(date.year, date.month)
end

.load(month_year) ⇒ Object



24
25
26
27
28
29
# File 'lib/month_year.rb', line 24

def load(month_year)
  raise_load_error(month_year) unless month_year.is_a?(Integer)
  year = month_year / 100
  month = month_year % 100
  self.new(year, month)
end

Instance Method Details

#<=>(other) ⇒ Object



58
59
60
# File 'lib/month_year.rb', line 58

def <=>(other)
  (year <=> other.year).nonzero? || month <=> other.month
end

#==(other) ⇒ Object Also known as: eql?



53
54
55
# File 'lib/month_year.rb', line 53

def ==(other)
  other.class == self.class && (self <=> other) == 0
end

#hashObject



62
63
64
# File 'lib/month_year.rb', line 62

def hash
  to_i.hash
end

#succObject Also known as: next



66
67
68
69
70
71
72
# File 'lib/month_year.rb', line 66

def succ
  if month == 12
    self.class.new(year+1, 1)
  else
    self.class.new(year, month+1)
  end
end

#to_iObject

Return the numeric representation of this ‘MonthYear` instance.

Example:

>> MonthYear.new(2016, 12).to_i
=> 201612


81
82
83
# File 'lib/month_year.rb', line 81

def to_i
  year * 100 + month
end

#to_sObject

Return the string representation of this ‘MonthYear` instance.

Example:

>> MonthYear.new(2016, 12).to_s
=> "2016-12"


91
92
93
# File 'lib/month_year.rb', line 91

def to_s
  "#{year}-#{month}"
end