Class: CalendarDate

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
ScheduleFu::Finder
Defined in:
app/models/calendar_date.rb

Constant Summary collapse

@@create_lock =
Mutex.new

Class Method Summary collapse

Methods included from ScheduleFu::Finder

conditions_for_date_finders

Methods included from ScheduleFu::Parser

#parse, #parse_date, #parse_dates

Class Method Details

.create_for_date(date) ⇒ Object



36
37
38
# File 'app/models/calendar_date.rb', line 36

def self.create_for_date(date)
  self.create_for_dates(date, date)
end

.create_for_dates(start_date = nil, end_date = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/calendar_date.rb', line 23

def self.create_for_dates(start_date = nil, end_date = nil)
  start_date ||= Date.today
  end_date ||= 5.years.since(start_date)
  range = start_date..end_date
  existing_dates = Set.new
  self.by_dates(range).each {|d| existing_dates << d.value }
  range.each do |date|
    begin
      self.create(:value => date) unless existing_dates.include?(date)
    rescue; end
  end
end

.find_by_value(value) ⇒ Object



19
20
21
# File 'app/models/calendar_date.rb', line 19

def self.find_by_value(value)
  find(:first, :conditions => { :value => value })
end

.get_and_create_dates(range) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/calendar_date.rb', line 42

def self.get_and_create_dates(range)
  range = range.first.to_date..range.last.to_date
  dates = self.by_dates(range)
  if dates.size < range.to_a.size
    CalendarDate.create_for_dates(range.first, range.last)
    Thread.new do
      start_date = 1.year.ago(range.first).to_date
      end_date = 1.year.since(range.last).to_date
      @@create_lock.synchronize do
        CalendarDate.create_for_dates(start_date, end_date)
      end
    end
    dates = self.by_dates(range)
  end
  dates
end