Class: WillFilter::Calendar

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

.daysObject



144
145
146
# File 'lib/will_filter/calendar.rb', line 144

def self.days
  DAYS
end

.hour_optionsObject



148
149
150
151
152
153
154
155
156
# File 'lib/will_filter/calendar.rb', line 148

def self.hour_options
  @hour_options ||= begin
    ho = []
    0.upto(23) do |i| 
      ho << [prepand_zero(i), i]
    end  
    ho
  end
end

.minute_optionsObject



158
159
160
161
162
163
164
165
166
# File 'lib/will_filter/calendar.rb', line 158

def self.minute_options
  @minute_options ||= begin
    mo = []
    0.upto(59) do |i| 
      mo << [prepand_zero(i), i] 
    end
    mo
  end      
end

.month_optionsObject



134
135
136
137
138
139
140
141
142
# File 'lib/will_filter/calendar.rb', line 134

def self.month_options
  @month_options ||= 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_optionsObject



168
169
170
# File 'lib/will_filter/calendar.rb', line 168

def self.second_options
  @second_options ||= minute_options
end

.year_optionsObject



124
125
126
127
128
129
130
131
132
# File 'lib/will_filter/calendar.rb', line 124

def self.year_options
  @year_options ||= 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_monthObject



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_dateObject



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

#hourObject



76
77
78
# File 'lib/will_filter/calendar.rb', line 76

def hour 
  selected_date.hour
end

#minuteObject



80
81
82
# File 'lib/will_filter/calendar.rb', line 80

def minute 
  selected_date.min
end

#modeObject



60
61
62
# File 'lib/will_filter/calendar.rb', line 60

def mode
  @mode ||= 'month'
end

#monthObject



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_dateObject



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_dateObject



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

#secondObject



84
85
86
# File 'lib/will_filter/calendar.rb', line 84

def second 
  selected_date.sec
end

#selected_dateObject



64
65
66
# File 'lib/will_filter/calendar.rb', line 64

def selected_date
  @selected_date ||= Time.now
end

#show_time?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/will_filter/calendar.rb', line 100

def show_time?
  @show_time
end

#start_dateObject



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

#titleObject



109
110
111
# File 'lib/will_filter/calendar.rb', line 109

def title
  "#{MONTHS[month-1]}, #{year}"
end

#yearObject



72
73
74
# File 'lib/will_filter/calendar.rb', line 72

def year 
  start_date.year
end