Class: DateValues::YearMonth
- Inherits:
-
Data
- Object
- Data
- DateValues::YearMonth
- Includes:
- Comparable
- Defined in:
- lib/date_values/year_month.rb
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
Instance Method Summary collapse
- #+(n) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #advance(years: 0, months: 0) ⇒ Object
- #as_json ⇒ Object
- #change(year: self.year, month: self.month) ⇒ Object
- #days ⇒ Object
-
#initialize(year:, month:) ⇒ YearMonth
constructor
A new instance of YearMonth.
- #inspect ⇒ Object
- #strftime(format) ⇒ Object
- #succ ⇒ Object
- #to_date ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(year:, month:) ⇒ YearMonth
Returns a new instance of YearMonth.
9 10 11 12 13 |
# File 'lib/date_values/year_month.rb', line 9 def initialize(year:, month:) raise ArgumentError, "invalid month: #{month}" unless (1..12).include?(month) super end |
Instance Attribute Details
#month ⇒ Object (readonly)
Returns the value of attribute month
6 7 8 |
# File 'lib/date_values/year_month.rb', line 6 def month @month end |
#year ⇒ Object (readonly)
Returns the value of attribute year
6 7 8 |
# File 'lib/date_values/year_month.rb', line 6 def year @year end |
Class Method Details
.from(date) ⇒ Object
15 16 17 |
# File 'lib/date_values/year_month.rb', line 15 def self.from(date) new(date.year, date.month) end |
.parse(str) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/date_values/year_month.rb', line 19 def self.parse(str) case str when /\A(\d{4})[\/\-](\d{1,2})\z/ then new($1.to_i, $2.to_i) else raise ArgumentError, "invalid YearMonth: #{str}" end end |
Instance Method Details
#+(n) ⇒ Object
32 33 34 35 |
# File 'lib/date_values/year_month.rb', line 32 def +(n) total = (year * 12 + (month - 1)) + n self.class.new(total / 12, total % 12 + 1) end |
#-(other) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/date_values/year_month.rb', line 37 def -(other) case other when Integer self + (-other) when YearMonth (year * 12 + month) - (other.year * 12 + other.month) else raise TypeError, "#{other.class} can't be coerced into Integer or YearMonth" end end |
#<=>(other) ⇒ Object
26 27 28 29 30 |
# File 'lib/date_values/year_month.rb', line 26 def <=>(other) return nil unless other.is_a?(YearMonth) [year, month] <=> [other.year, other.month] end |
#advance(years: 0, months: 0) ⇒ Object
52 53 54 |
# File 'lib/date_values/year_month.rb', line 52 def advance(years: 0, months: 0) self + (years * 12 + months) end |
#as_json ⇒ Object
72 73 74 |
# File 'lib/date_values/year_month.rb', line 72 def as_json(*) to_s end |
#change(year: self.year, month: self.month) ⇒ Object
56 57 58 |
# File 'lib/date_values/year_month.rb', line 56 def change(year: self.year, month: self.month) self.class.new(year, month) end |
#days ⇒ Object
60 61 62 |
# File 'lib/date_values/year_month.rb', line 60 def days Date.new(year, month, -1).day end |
#inspect ⇒ Object
80 81 82 |
# File 'lib/date_values/year_month.rb', line 80 def inspect "#<DateValues::YearMonth #{self}>" end |
#strftime(format) ⇒ Object
64 65 66 |
# File 'lib/date_values/year_month.rb', line 64 def strftime(format) to_date.strftime(format) end |
#succ ⇒ Object
48 49 50 |
# File 'lib/date_values/year_month.rb', line 48 def succ self + 1 end |
#to_date ⇒ Object
68 69 70 |
# File 'lib/date_values/year_month.rb', line 68 def to_date Date.new(year, month, 1) end |
#to_s ⇒ Object
76 77 78 |
# File 'lib/date_values/year_month.rb', line 76 def to_s format('%04d-%02d', year, month) end |