Class: Timely::Season

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/timely/rails/season.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_season_for(dates = []) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/timely/rails/season.rb', line 70

def self.build_season_for(dates=[])
  season = Season.new
  date_groups = dates.map do |date|
    DateGroup.new(:start_date => date, :end_date => date)
  end
  season.date_groups = date_groups
  season
end

Instance Method Details

#boundary_endObject



52
53
54
# File 'lib/timely/rails/season.rb', line 52

def boundary_end
  date_groups.map(&:end_date).sort.last
end

#boundary_rangeObject



40
41
42
# File 'lib/timely/rails/season.rb', line 40

def boundary_range
  boundary_start..boundary_end
end

#boundary_startObject



48
49
50
# File 'lib/timely/rails/season.rb', line 48

def boundary_start
  date_groups.map(&:start_date).sort.first
end

#datesObject



34
35
36
37
38
# File 'lib/timely/rails/season.rb', line 34

def dates
  date_groups.map do |date_group|
    ((date_group.start_date)..(date_group.end_date)).to_a
  end.flatten
end

#deep_cloneObject



60
61
62
63
64
65
66
67
68
# File 'lib/timely/rails/season.rb', line 60

def deep_clone
  # Use clone until it is removed in AR 3.1, then dup is the same
  method = ActiveRecord::Base.instance_methods(false).include?(:clone) ? :clone : :dup
  cloned = self.send(method)
  date_groups.each do |dg|
    cloned.date_groups.build(dg.send(method).attributes)
  end
  cloned
end

#has_gaps?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/timely/rails/season.rb', line 26

def has_gaps?
  last_date = nil
  date_groups.each do |dg|
    return true if last_date && dg.start_date != last_date + 1
  end
  false
end

#includes_date?(date) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/timely/rails/season.rb', line 22

def includes_date?(date)
  date_groups.any?{|dg| dg.includes_date?(date)}
end

#past?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/timely/rails/season.rb', line 44

def past?
  boundary_end && boundary_end < ::Date.current
end

#string_of_date_groupsObject



85
86
87
# File 'lib/timely/rails/season.rb', line 85

def string_of_date_groups
  date_groups.map{|dg| "#{dg.start_date.to_s(:short)} - #{dg.end_date.to_s(:short)}"}.to_sentence
end

#to_sObject Also known as: audit_name



79
80
81
# File 'lib/timely/rails/season.rb', line 79

def to_s
  name.presence || Timely::DateRange.to_s(boundary_start, boundary_end)
end

#validate_dates_specifiedObject



17
18
19
20
# File 'lib/timely/rails/season.rb', line 17

def validate_dates_specified
  errors.add(:base, "No dates specified") if date_groups.blank?
  errors.empty?
end

#within_boundary?(date) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/timely/rails/season.rb', line 56

def within_boundary?(date)
  boundary_start && boundary_end && boundary_start <= date && boundary_end >= date
end