Class: MonthYear
- Inherits:
-
Object
- Object
- MonthYear
- Defined in:
- lib/month_year.rb,
lib/month_year/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
- .dump(month_year) ⇒ Object
-
.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).
- .load(month_year) ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(year, month) ⇒ MonthYear
constructor
Instantiate a new ‘MonthYear` object.
- #succ ⇒ Object (also: #next)
-
#to_i ⇒ Object
Return the numeric representation of this ‘MonthYear` instance.
-
#to_s ⇒ Object
Return the string representation of this ‘MonthYear` instance.
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)
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
#month ⇒ Object (readonly)
Returns the value of attribute month.
5 6 7 |
# File 'lib/month_year.rb', line 5 def month @month end |
#year ⇒ Object (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)
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 |
#hash ⇒ Object
62 63 64 |
# File 'lib/month_year.rb', line 62 def hash to_i.hash end |
#succ ⇒ Object 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_i ⇒ Object
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_s ⇒ Object
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 |