Class: Timely::Season
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Timely::Season
- Defined in:
- lib/timely/rails/season.rb
Instance Method Summary collapse
- #boundary_end ⇒ Object
- #boundary_range ⇒ Object
- #boundary_start ⇒ Object
- #dates ⇒ Object
- #deep_clone ⇒ Object
- #includes_date?(date) ⇒ Boolean
- #past? ⇒ Boolean
- #string_of_date_groups ⇒ Object
- #to_s ⇒ Object (also: #audit_name)
- #validate_dates_specified ⇒ Object
- #within_boundary?(date) ⇒ Boolean
Instance Method Details
#boundary_end ⇒ Object
40 41 42 |
# File 'lib/timely/rails/season.rb', line 40 def boundary_end date_groups.map(&:end_date).max end |
#boundary_range ⇒ Object
28 29 30 |
# File 'lib/timely/rails/season.rb', line 28 def boundary_range boundary_start..boundary_end end |
#boundary_start ⇒ Object
36 37 38 |
# File 'lib/timely/rails/season.rb', line 36 def boundary_start date_groups.map(&:start_date).min end |
#dates ⇒ Object
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_clone ⇒ Object
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
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
32 33 34 |
# File 'lib/timely/rails/season.rb', line 32 def past? boundary_end && boundary_end < ::Date.current end |
#string_of_date_groups ⇒ Object
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_s ⇒ Object 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_specified ⇒ Object
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
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 |