Class: MonthWeeks::Month
- Inherits:
-
Object
- Object
- MonthWeeks::Month
- Defined in:
- lib/month_weeks/month.rb
Instance Attribute Summary collapse
-
#first_day_of_week ⇒ Object
readonly
Returns the value of attribute first_day_of_week.
Instance Method Summary collapse
- #days ⇒ Object
- #first ⇒ Object
-
#initialize(*args) ⇒ Month
constructor
A new instance of Month.
- #last ⇒ Object
- #last_day_of_week ⇒ Object
- #month ⇒ Object
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
- #to_s ⇒ Object
- #weeks ⇒ Object
- #year ⇒ Object
Constructor Details
#initialize(*args) ⇒ Month
Returns a new instance of Month.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/month_weeks/month.rb', line 8 def initialize(*args) = args.last.kind_of?(Hash) ? args.pop : {} if args.length == 2 year, month = args @init_date = Date.new(year, month) elsif args.length == 1 date = args.first @init_date = Date.parse(date) else raise ArgumentError.new("wrong number of arguments (0 for 1..3)") end @first_day_of_week = [:first_day_of_week] || 1 end |
Instance Attribute Details
#first_day_of_week ⇒ Object (readonly)
Returns the value of attribute first_day_of_week.
5 6 7 |
# File 'lib/month_weeks/month.rb', line 5 def first_day_of_week @first_day_of_week end |
Instance Method Details
#days ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/month_weeks/month.rb', line 56 def days case month when 1, 3, 5, 7, 8, 10, 12 31 when 4, 6, 9, 11 30 when 2 Date.leap?(year) ? 29 : 28 end end |
#first ⇒ Object
44 45 46 |
# File 'lib/month_weeks/month.rb', line 44 def first day_to_date(1) end |
#last ⇒ Object
48 49 50 |
# File 'lib/month_weeks/month.rb', line 48 def last day_to_date(days) end |
#last_day_of_week ⇒ Object
52 53 54 |
# File 'lib/month_weeks/month.rb', line 52 def last_day_of_week (first_day_of_week + 6) % 7 end |
#month ⇒ Object
28 29 30 |
# File 'lib/month_weeks/month.rb', line 28 def month init_date.month end |
#to_h ⇒ Object
67 68 69 70 71 |
# File 'lib/month_weeks/month.rb', line 67 def to_h { :year => year, :month => month, :first => first, :last => last, :weeks => weeks } end |
#to_json(*args) ⇒ Object
73 74 75 |
# File 'lib/month_weeks/month.rb', line 73 def to_json(*args) self.to_h.to_json(*args) end |
#to_s ⇒ Object
77 78 79 |
# File 'lib/month_weeks/month.rb', line 77 def to_s sprintf("%d-%02d", year, month) end |
#weeks ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/month_weeks/month.rb', line 32 def weeks current = first weeks = [] while (current <= last) do start_date = current end_date = day_to_date(end_of_week(start_date)) weeks << Week.new(start_date, end_date) current = end_date.next end weeks end |
#year ⇒ Object
24 25 26 |
# File 'lib/month_weeks/month.rb', line 24 def year init_date.year end |