Class: WillFilter::Calendar
- Inherits:
-
Object
- Object
- WillFilter::Calendar
- Defined in:
- lib/will_filter/calendar.rb
Constant Summary collapse
- MONTHS =
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
- DAYS =
['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
Class Method Summary collapse
- .days ⇒ Object
- .hour_options ⇒ Object
- .minute_options ⇒ Object
- .month_options ⇒ Object
- .prepand_zero(num) ⇒ Object
- .second_options ⇒ Object
- .year_options ⇒ Object
Instance Method Summary collapse
- #days_in_month ⇒ Object
- #end_date ⇒ Object
- #hour ⇒ Object
-
#initialize(selected_date = nil, start_date = nil, show_time = false, mode = 'month') ⇒ Calendar
constructor
A new instance of Calendar.
- #minute ⇒ Object
- #mode ⇒ Object
- #month ⇒ Object
- #move(delta) ⇒ Object
- #next_start_date ⇒ Object
- #previous_start_date ⇒ Object
- #second ⇒ Object
- #selected_date ⇒ Object
- #show_time? ⇒ Boolean
- #start_date ⇒ Object
- #title ⇒ Object
- #year ⇒ Object
Constructor Details
#initialize(selected_date = nil, start_date = nil, show_time = false, mode = 'month') ⇒ Calendar
Returns a new instance of Calendar.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/will_filter/calendar.rb', line 39 def initialize(selected_date = nil, start_date = nil, show_time = false, mode = 'month') if selected_date.blank? @selected_date = Time.now else begin @selected_date = Time.parse(selected_date) rescue @selected_date = Time.now end end if start_date.is_a?(Date) @start_date = start_date else @start_date = start_date.blank? ? Date.new(@selected_date.year, @selected_date.month, 1) : Date.parse(start_date) end @show_time = show_time @mode = mode end |
Class Method Details
.days ⇒ Object
144 145 146 |
# File 'lib/will_filter/calendar.rb', line 144 def self.days DAYS end |
.hour_options ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/will_filter/calendar.rb', line 148 def self. ||= begin ho = [] 0.upto(23) do |i| ho << [prepand_zero(i), i] end ho end end |
.minute_options ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/will_filter/calendar.rb', line 158 def self. ||= begin mo = [] 0.upto(59) do |i| mo << [prepand_zero(i), i] end mo end end |
.month_options ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/will_filter/calendar.rb', line 134 def self. ||= begin mo = [] MONTHS.each_with_index do |m, i| mo << [m, i+1] end mo end end |
.prepand_zero(num) ⇒ Object
172 173 174 |
# File 'lib/will_filter/calendar.rb', line 172 def self.prepand_zero(num) (num < 10 ? "0#{num}" : "#{num}") end |
.second_options ⇒ Object
168 169 170 |
# File 'lib/will_filter/calendar.rb', line 168 def self. ||= end |
.year_options ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/will_filter/calendar.rb', line 124 def self. ||= begin yo = [] (Date.today.year - 100).upto(Date.today.year + 30) do |year| yo << [year, year] end yo end end |
Instance Method Details
#days_in_month ⇒ Object
96 97 98 |
# File 'lib/will_filter/calendar.rb', line 96 def days_in_month @days_in_month ||= (Date.new(year, 12, 31).to_date<<(12 - month)).day end |
#end_date ⇒ Object
92 93 94 |
# File 'lib/will_filter/calendar.rb', line 92 def end_date @end_date ||= Date.new(start_date.year, start_date.month, days_in_month) end |
#hour ⇒ Object
76 77 78 |
# File 'lib/will_filter/calendar.rb', line 76 def hour selected_date.hour end |
#minute ⇒ Object
80 81 82 |
# File 'lib/will_filter/calendar.rb', line 80 def minute selected_date.min end |
#mode ⇒ Object
60 61 62 |
# File 'lib/will_filter/calendar.rb', line 60 def mode @mode ||= 'month' end |
#month ⇒ Object
68 69 70 |
# File 'lib/will_filter/calendar.rb', line 68 def month start_date.month end |
#move(delta) ⇒ Object
104 105 106 107 |
# File 'lib/will_filter/calendar.rb', line 104 def move(delta) return self if delta.blank? or delta == 0 WillFilter::Calendar.new(selected_date, start_date + delta, show_time?, mode) end |
#next_start_date ⇒ Object
113 114 115 116 |
# File 'lib/will_filter/calendar.rb', line 113 def next_start_date return start_date + 1.year if mode == 'annual' start_date + 1.month end |
#previous_start_date ⇒ Object
118 119 120 121 |
# File 'lib/will_filter/calendar.rb', line 118 def previous_start_date return start_date - 1.year if mode == 'annual' start_date - 1.month end |
#second ⇒ Object
84 85 86 |
# File 'lib/will_filter/calendar.rb', line 84 def second selected_date.sec end |
#selected_date ⇒ Object
64 65 66 |
# File 'lib/will_filter/calendar.rb', line 64 def selected_date @selected_date ||= Time.now end |
#show_time? ⇒ Boolean
100 101 102 |
# File 'lib/will_filter/calendar.rb', line 100 def show_time? @show_time end |
#start_date ⇒ Object
88 89 90 |
# File 'lib/will_filter/calendar.rb', line 88 def start_date @start_date ||= Date.new(Date.today.year, Date.today.month, 1) end |
#title ⇒ Object
109 110 111 |
# File 'lib/will_filter/calendar.rb', line 109 def title "#{MONTHS[month-1]}, #{year}" end |
#year ⇒ Object
72 73 74 |
# File 'lib/will_filter/calendar.rb', line 72 def year start_date.year end |