Class: CalendarPeriod
- Inherits:
-
Object
- Object
- CalendarPeriod
- Includes:
- ActionView::Helpers::DateHelper
- Defined in:
- lib/calendar_period.rb
Instance Attribute Summary collapse
Class Method Summary collapse
- .between(from = nil, to = nil) ⇒ Object
- .default ⇒ Object
- .from(from, duration = nil) ⇒ Object
- .to(to, duration = nil) ⇒ Object
Instance Method Summary collapse
-
#+(s) ⇒ Object
to shift the period forward one month.
-
#-(s) ⇒ Object
to shift the period back one month.
-
#<<(s) ⇒ Object
to extend the period by one month.
- #bounded? ⇒ Boolean
- #default? ⇒ Boolean
- #describe_finish(date_format = nil) ⇒ Object
- #describe_start(date_format = nil) ⇒ Object
- #description ⇒ Object
- #duration ⇒ Object
- #duration=(s) ⇒ Object
- #inspect ⇒ Object
- #monthname ⇒ Object
-
#pad! ⇒ Object
to expand the period to full calendar months @period.pad!.
-
#to_s ⇒ Object
descriptions.
- #unbounded? ⇒ Boolean
Instance Attribute Details
#finish ⇒ Object
36 37 38 |
# File 'lib/calendar_period.rb', line 36 def finish @finish.to_datetime if @finish end |
#start ⇒ Object
32 33 34 |
# File 'lib/calendar_period.rb', line 32 def start @start.to_datetime if @start end |
Class Method Details
.between(from = nil, to = nil) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/calendar_period.rb', line 6 def self.between(from=nil,to=nil) raise StandardError, "CalendarPeriod.between requires either start or finish datetime" unless from || to period = self.new period.start = from period.finish = to period end |
.default ⇒ Object
24 25 26 |
# File 'lib/calendar_period.rb', line 24 def self.default between(Time.now, nil) end |
.from(from, duration = nil) ⇒ Object
14 15 16 17 |
# File 'lib/calendar_period.rb', line 14 def self.from(from, duration=nil) to = from + duration if duration between(from, to) end |
.to(to, duration = nil) ⇒ Object
19 20 21 22 |
# File 'lib/calendar_period.rb', line 19 def self.to(to, duration=nil) from = to - duration if duration between(from, to) end |
Instance Method Details
#+(s) ⇒ Object
to shift the period forward one month
132 133 134 135 |
# File 'lib/calendar_period.rb', line 132 def +(s) start = start + s if start finish = finish + s if finish end |
#-(s) ⇒ Object
to shift the period back one month
140 141 142 143 |
# File 'lib/calendar_period.rb', line 140 def -(s) start = start - s if start finish = finish - s if finish end |
#<<(s) ⇒ Object
to extend the period by one month
148 149 150 151 152 153 154 |
# File 'lib/calendar_period.rb', line 148 def <<(s) if bounded? finish += s else duration = s end end |
#bounded? ⇒ Boolean
56 57 58 |
# File 'lib/calendar_period.rb', line 56 def bounded? start && finish end |
#default? ⇒ Boolean
28 29 30 |
# File 'lib/calendar_period.rb', line 28 def default? finish.nil? && (Time.now - start).to_i.abs < 1.minute end |
#describe_finish(date_format = nil) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/calendar_period.rb', line 94 def describe_finish(date_format=nil) if finish unless date_format date_format = :calendar_period_describe date_format = :calendar_period_describe_with_year unless finish.year == Time.now.year end I18n.l finish, :format => date_format end end |
#describe_start(date_format = nil) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/calendar_period.rb', line 84 def describe_start(date_format=nil) if start unless date_format date_format = :calendar_period_describe date_format = :calendar_period_describe_with_year unless start.year == Time.now.year end I18n.l start, :format => date_format end end |
#description ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/calendar_period.rb', line 104 def description return I18n.t 'calendar_period.description_day', :day => I18n.l(start, :format => "%d"), :monthname => I18n.l(start ? start : finish, :format => "%B"), :year => I18n.l(start, :format => "%Y") if is_day? return I18n.l start, :format => :calendar_period_description_week if is_week? return I18n.l start, :format => :calendar_period_description_month if is_month? return I18n.l start, :format => :calendar_period_description_year if is_year? return I18n.t 'calendar_period.onwards', :describe_start => describe_start unless finish return I18n.t 'calendar_period.until', :describe_finish => describe_finish unless start I18n.t 'calendar_period.between', :describe_finish => describe_finish, :describe_start => describe_start end |
#duration ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/calendar_period.rb', line 40 def duration if bounded? finish - start else 'indefinite' end end |
#duration=(s) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/calendar_period.rb', line 48 def duration=(s) if start finish = start + s elsif finish start = finish - s end end |
#inspect ⇒ Object
72 73 74 75 76 |
# File 'lib/calendar_period.rb', line 72 def inspect I18n.t 'calendar_period.inspect', :describe_start => describe_start(:calendar_period_describe_detailed), :describe_finish => describe_finish(:calendar_period_describe_detailed) end |
#monthname ⇒ Object
117 118 119 |
# File 'lib/calendar_period.rb', line 117 def monthname I18n.l(start ? start : finish, :format => "%B") end |
#pad! ⇒ Object
to expand the period to full calendar months @period.pad!
124 125 126 127 |
# File 'lib/calendar_period.rb', line 124 def pad! start = start.beginning_of_month if start finish = finish.end_of_month if finish end |
#to_s ⇒ Object
descriptions
66 67 68 69 70 |
# File 'lib/calendar_period.rb', line 66 def to_s I18n.t 'calendar_period.to_s', :distance_of_time_in_words => distance_of_time_in_words(start, finish), :start => (I18n.l start, :format => :default) end |
#unbounded? ⇒ Boolean
60 61 62 |
# File 'lib/calendar_period.rb', line 60 def unbounded? !bounded? end |