Class: CalendarHelper::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/table_builder/calendar_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Calendar

Returns a new instance of Calendar.



55
56
57
58
59
60
61
62
63
64
# File 'lib/table_builder/calendar_helper.rb', line 55

def initialize options = {}
  @year          = options[:year]  || Time.now.year
  @month         = options[:month] || Time.now.month
  @first_weekday = (options[:first_weekday] || 0) % 7
  @last_weekday  = (@first_weekday + 6) % 7
  @first         = Date.civil @year, @month, 1
  @last          = Date.civil @year, @month, -1
  @first_day     = @first - (@first.wday - @first_weekday + 7) % 7
  @last_day      = @last + (@last_weekday - @last.wday + 7) % 7
end

Instance Attribute Details

#first_dayObject (readonly)

Returns the value of attribute first_day.



54
55
56
# File 'lib/table_builder/calendar_helper.rb', line 54

def first_day
  @first_day
end

#first_weekdayObject (readonly)

Returns the value of attribute first_weekday.



54
55
56
# File 'lib/table_builder/calendar_helper.rb', line 54

def first_weekday
  @first_weekday
end

#last_dayObject (readonly)

Returns the value of attribute last_day.



54
55
56
# File 'lib/table_builder/calendar_helper.rb', line 54

def last_day
  @last_day
end

#last_weekdayObject (readonly)

Returns the value of attribute last_weekday.



54
55
56
# File 'lib/table_builder/calendar_helper.rb', line 54

def last_weekday
  @last_weekday
end

#monthObject (readonly)

Returns the value of attribute month.



54
55
56
# File 'lib/table_builder/calendar_helper.rb', line 54

def month
  @month
end

Instance Method Details

#daysObject



82
83
84
# File 'lib/table_builder/calendar_helper.rb', line 82

def days
  @days ||= each_day
end

#each_day(&block) ⇒ Object



66
67
68
# File 'lib/table_builder/calendar_helper.rb', line 66

def each_day &block
  (first_day..last_day).map &block
end

#objects_for_days(objects) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/table_builder/calendar_helper.rb', line 70

def objects_for_days objects
  grouped = {}
  objects.each do |obj|
    [*yield(obj)].each do |val|
      key = val.strftime("%Y-%m-%d")
      grouped.has_key?(key) ? grouped[key] << obj : grouped[key] = [obj]
    end
  end
        
  each_day { |day| [day, grouped[day.strftime("%Y-%m-%d")] || []] }
end