Module: Period
- Defined in:
- lib/period.rb
Overview
This module is intended to hide the complexity of ActivePeriod And permit the user to write less and doing more
Constant Summary collapse
- LAST_OR_NEXT_REGEX =
/^(:?last|next)_(\d+)_(day|week|month|quarter|year)s?(_from_now)?$/
- LAST_AND_NEXT_REGEX =
/^last_(\d+)_(day|week|month|quarter|year)s?_to_next_(\d+)_(day|week|month|quarter|year)s?$/
Class Method Summary collapse
-
.[](range) ⇒ Object
Shorthand to Period.new.
-
.bounded(range) ⇒ Object
Shorthand ActivePeriod::FreePeriod.new(range, allow_beginless: false, allow_endless: false).
-
.env_time ⇒ Object
env_time provide a Fallback if the project dont specify any Time.zone.
-
.method_missing(method_name, *arguments, &block) ⇒ Object
Experimenta l non-documented feature Inpired form ActiveRecord dynamic find_by_x like User.find_by_name Example: Period.last_3_weeks_from_now == Period.mew(2.weeks.ago.beginning_of_week..Time.now.end_of_week).
- .missing_last_and_next(method_name) ⇒ Object
- .missing_last_or_next(method_name) ⇒ Object
-
.new(range, allow_beginless: true, allow_endless: true) ⇒ Object
Shorthand to ActivePeriod::FreePeriod.new.
- .respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Class Method Details
.[](range) ⇒ Object
Shorthand to Period.new
16 17 18 |
# File 'lib/period.rb', line 16 def self.[](range) Period.new(range) end |
.bounded(range) ⇒ Object
Shorthand ActivePeriod::FreePeriod.new(range, allow_beginless: false, allow_endless: false)
11 12 13 |
# File 'lib/period.rb', line 11 def self.bounded(range) ActivePeriod::FreePeriod.new(range, allow_beginless: false, allow_endless: false) end |
.env_time ⇒ Object
env_time provide a Fallback if the project dont specify any Time.zone
21 22 23 |
# File 'lib/period.rb', line 21 def self.env_time (Time.zone || Time) end |
.method_missing(method_name, *arguments, &block) ⇒ Object
Experimenta l non-documented feature Inpired form ActiveRecord dynamic find_by_x like User.find_by_name Example: Period.last_3_weeks_from_now == Period.mew(2.weeks.ago.beginning_of_week..Time.now.end_of_week)
59 60 61 62 63 64 65 66 67 |
# File 'lib/period.rb', line 59 def method_missing(method_name, *arguments, &block) if method_name.match? LAST_OR_NEXT_REGEX missing_last_or_next(method_name) elsif method_name.match? LAST_AND_NEXT_REGEX missing_last_and_next(method_name) else super end end |
.missing_last_and_next(method_name) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/period.rb', line 88 def missing_last_and_next(method_name) last_count, last_klass, next_count, next_klass = method_name.to_s.scan(LAST_AND_NEXT_REGEX).flatten last_klass = last_klass.singularize next_klass = next_klass.singularize from = last_count.to_i.send(last_klass).ago.send("beginning_of_#{last_klass}") to = next_count.to_i.send(next_klass).from_now.send("end_of_#{next_klass}") self.new(from..to) end |
.missing_last_or_next(method_name) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/period.rb', line 69 def missing_last_or_next(method_name) last_next, count, klass, from_now = method_name.to_s.scan(LAST_OR_NEXT_REGEX).flatten klass = klass.singularize case last_next when 'last' from = count.to_i.send(klass).ago.send("beginning_of_#{klass}") to = env_time.now to -= 1.send(klass) unless from_now to = to.send("end_of_#{klass}") when 'next' from = env_time.now from += 1.send(klass) unless from_now from = from.send("beginning_of_#{klass}") to = count.to_i.send(klass).from_now.send("end_of_#{klass}") end self.new(from..to) end |
.new(range, allow_beginless: true, allow_endless: true) ⇒ Object
Shorthand to ActivePeriod::FreePeriod.new
6 7 8 |
# File 'lib/period.rb', line 6 def self.new(range, allow_beginless: true, allow_endless: true) ActivePeriod::FreePeriod.new(range, allow_beginless: allow_beginless, allow_endless: allow_endless) end |
.respond_to_missing?(method_name, include_private = false) ⇒ Boolean
99 100 101 102 103 |
# File 'lib/period.rb', line 99 def respond_to_missing?(method_name, include_private = false) method_name.match?(/(last|next)_\d+_(day|week|month|quarter|year)s?(_from_now)?/) || method_name.match?(/(last)_\d+_(day|week|month|quarter|year)s?_to_next_\d+_(day|week|month|quarter|year)s?/ ) || super end |