Class: Timely::Season

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

Instance Method Summary collapse

Instance Method Details

#boundary_endObject



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

def boundary_end
  date_groups.map(&:end_date).max
end

#boundary_rangeObject



28
29
30
# File 'lib/timely/rails/season.rb', line 28

def boundary_range
  boundary_start..boundary_end
end

#boundary_startObject



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

def boundary_start
  date_groups.map(&:start_date).min
end

#datesObject



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

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

#deep_cloneObject



48
49
50
51
52
53
54
55
56
# File 'lib/timely/rails/season.rb', line 48

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 = send(method)
  date_groups.each do |dg|
    cloned.date_groups.build(dg.send(method).attributes)
  end
  cloned
end

#includes_date?(date) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#past?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/timely/rails/season.rb', line 32

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

#string_of_date_groupsObject



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

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

#to_sObject Also known as: audit_name



58
59
60
# File 'lib/timely/rails/season.rb', line 58

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

#validate_dates_specifiedObject



13
14
15
16
# File 'lib/timely/rails/season.rb', line 13

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

#within_boundary?(date) ⇒ Boolean

Returns:

  • (Boolean)


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

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