Class: Charting::Period
- Inherits:
-
Object
- Object
- Charting::Period
- Defined in:
- lib/ar_to_chart/charting/period.rb
Class Method Summary collapse
-
.clear ⇒ Object
Clear the saved instance which we should do at the start of each Rails request.
-
.filter(controller) ⇒ Object
Called when we’re configured as a before_filter which we need to ensure our thread data is ours and no left over from somebody else.
-
.method_missing(method, *args) ⇒ Object
Arrange for instance methods to be called as if class methods.
Instance Method Summary collapse
- #beginning_of_epoch ⇒ Object
- #date_from_param(date, default) ⇒ Object
-
#first_day_of_last_month ⇒ Object
Last month.
-
#first_day_of_last_week ⇒ Object
Last week.
-
#first_day_of_last_year ⇒ Object
Last year.
-
#first_day_of_this_month ⇒ Object
This month.
-
#first_day_of_this_week ⇒ Object
This week.
-
#first_day_of_this_year ⇒ Object
This year.
- #from_param(period, default) ⇒ Object
- #from_params(params = {}) ⇒ Object
- #last_day_of_last_month ⇒ Object
- #last_day_of_last_week ⇒ Object
- #last_day_of_last_year ⇒ Object
- #last_day_of_this_month ⇒ Object
- #last_day_of_this_week ⇒ Object
- #last_day_of_this_year ⇒ Object
- #range_from(params) ⇒ Object
-
#today ⇒ Object
Basic markers.
- #tomorrow ⇒ Object
- #yesterday ⇒ Object
Class Method Details
.clear ⇒ Object
Clear the saved instance which we should do at the start of each Rails request
141 142 143 |
# File 'lib/ar_to_chart/charting/period.rb', line 141 def self.clear Thread.current[:period2] = nil end |
.filter(controller) ⇒ Object
Called when we’re configured as a before_filter which we need to ensure our thread data is ours and no left over from somebody else
147 148 149 |
# File 'lib/ar_to_chart/charting/period.rb', line 147 def self.filter(controller) clear end |
.method_missing(method, *args) ⇒ Object
Arrange for instance methods to be called as if class methods. Make threadsafe.
152 153 154 155 |
# File 'lib/ar_to_chart/charting/period.rb', line 152 def self.method_missing(method, *args) Thread.current[:period2] = new unless Thread.current[:period2] self.instance_methods.include?(method.to_s) ? Thread.current[:period2].send(method, *args) : super end |
Instance Method Details
#beginning_of_epoch ⇒ Object
80 81 82 |
# File 'lib/ar_to_chart/charting/period.rb', line 80 def beginning_of_epoch today - 20.years end |
#date_from_param(date, default) ⇒ Object
24 25 26 27 28 |
# File 'lib/ar_to_chart/charting/period.rb', line 24 def date_from_param(date, default) return default unless date return date.to_date if date.is_a?(Date) || date.is_a?(Time) Time.parse(date) rescue default end |
#first_day_of_last_month ⇒ Object
Last month
112 113 114 115 |
# File 'lib/ar_to_chart/charting/period.rb', line 112 def first_day_of_last_month last_month = Date.today - 1.month Date.new(last_month.year, last_month.month, 1) end |
#first_day_of_last_week ⇒ Object
Last week
103 104 105 |
# File 'lib/ar_to_chart/charting/period.rb', line 103 def first_day_of_last_week first_day_of_this_week - 7.days end |
#first_day_of_last_year ⇒ Object
Last year
131 132 133 |
# File 'lib/ar_to_chart/charting/period.rb', line 131 def first_day_of_last_year Date.new(today.year - 1, 1, 1) end |
#first_day_of_this_month ⇒ Object
This month
94 95 96 |
# File 'lib/ar_to_chart/charting/period.rb', line 94 def first_day_of_this_month Date.new(today.year, today.month, 1) end |
#first_day_of_this_week ⇒ Object
This week
85 86 87 |
# File 'lib/ar_to_chart/charting/period.rb', line 85 def first_day_of_this_week today - today.wday.days end |
#first_day_of_this_year ⇒ Object
This year
122 123 124 |
# File 'lib/ar_to_chart/charting/period.rb', line 122 def first_day_of_this_year Date.new(today.year, 1, 1) end |
#from_param(period, default) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ar_to_chart/charting/period.rb', line 30 def from_param(period, default) return default.first, default.last unless period period_range = case period.to_sym when :today then today.beginning_of_day..today.end_of_day when :yesterday then yesterday.beginning_of_day..yesterday.end_of_day when :this_week then first_day_of_this_week.beginning_of_day..last_day_of_this_week.end_of_day when :this_month then first_day_of_this_month.beginning_of_day..last_day_of_this_month.end_of_day when :this_year then first_day_of_this_year.beginning_of_day..last_day_of_this_year.end_of_day when :last_week then first_day_of_last_week.beginning_of_day..last_day_of_last_week.end_of_day when :last_month then first_day_of_last_month.beginning_of_day..last_day_of_last_month.end_of_day when :last_year then first_day_of_last_year.beginning_of_day..last_day_of_last_year.end_of_day when :last_30_days then (today - 30.days).beginning_of_day..today.end_of_day when :last_12_months then (today - 12.months).beginning_of_day..today.end_of_day when :lifetime then beginning_of_epoch.beginning_of_day..today.end_of_day; else default end return period_range.first, period_range.last end |
#from_params(params = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ar_to_chart/charting/period.rb', line 12 def from_params(params = {}) default_from = (today - 30.days).beginning_of_day default_to = today.end_of_day if params[:period] from, to = from_param(params[:period], default_from..default_to) else to = date_from_param(params[:to], default_to) from = date_from_param(params[:from], default_from) end from..to end |
#last_day_of_last_month ⇒ Object
117 118 119 |
# File 'lib/ar_to_chart/charting/period.rb', line 117 def last_day_of_last_month first_day_of_last_month + 1.month - 1.day end |
#last_day_of_last_week ⇒ Object
107 108 109 |
# File 'lib/ar_to_chart/charting/period.rb', line 107 def last_day_of_last_week first_day_of_last_week + 7.days end |
#last_day_of_last_year ⇒ Object
135 136 137 |
# File 'lib/ar_to_chart/charting/period.rb', line 135 def last_day_of_last_year Date.new(first_day_of_last_year.year, 12, 31) end |
#last_day_of_this_month ⇒ Object
98 99 100 |
# File 'lib/ar_to_chart/charting/period.rb', line 98 def last_day_of_this_month first_day_of_this_month + 1.month - 1.day end |
#last_day_of_this_week ⇒ Object
89 90 91 |
# File 'lib/ar_to_chart/charting/period.rb', line 89 def last_day_of_this_week first_day_of_this_week + 7.days end |
#last_day_of_this_year ⇒ Object
126 127 128 |
# File 'lib/ar_to_chart/charting/period.rb', line 126 def last_day_of_this_year Date.new(today.year, 12, 31) end |
#range_from(params) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ar_to_chart/charting/period.rb', line 49 def range_from(params) period = self.from_params(params) case params[:time_group] when 'date' period.first.to_date..period.last.to_date when 'day_of_week' 0..6 when 'day_of_month' 1..31 when 'hour' 0..24 when 'month' 1..12 when 'year' period.first.to_date.year..period.last.to_date.year end end |
#today ⇒ Object
Basic markers
68 69 70 |
# File 'lib/ar_to_chart/charting/period.rb', line 68 def today Time.zone.now.to_date end |
#tomorrow ⇒ Object
76 77 78 |
# File 'lib/ar_to_chart/charting/period.rb', line 76 def tomorrow today + 1.day end |
#yesterday ⇒ Object
72 73 74 |
# File 'lib/ar_to_chart/charting/period.rb', line 72 def yesterday today - 1.day end |