module Period

This module is intended to hide the complexity of ActivePeriod And permit the user to write less and doing more

Public Class Methods

bounded(range) click to toggle source

Shorthand ActivePeriod::FreePeriod.new(range, allow_beginless: false, allow_endless: false)

# File lib/period.rb, line 11
def self.bounded(range)
  ActivePeriod::FreePeriod.new(range, allow_beginless: false, allow_endless: false)
end
env_time() click to toggle source

env_time provide a Fallback if the project dont specify any Time.zone

# File lib/period.rb, line 16
def self.env_time
  (Time.zone || Time)
end
method_missing(method_name, *arguments, &block) click to toggle source

Experimental 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) Note : Maybe it should return a collection of StandardPeriod

Calls superclass method
# File lib/period.rb, line 52
def method_missing(method_name, *arguments, &block)
  super unless method_name.match?(/(last|next)_\d+_(day|week|month|quarter|year)s?(_from_now)?/)
  last_next, count, klass = method_name.to_s.split('_')
  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 method_name.match?(/from_now$/)
    to = to.send("end_of_#{klass}")
  when 'next'
    from = env_time.now
    from += 1.send(klass) unless method_name.match?(/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(*args) click to toggle source

Shorthand to ActivePeriod::FreePeriod.new

# File lib/period.rb, line 6
def self.new(*args)
  ActivePeriod::FreePeriod.new(*args)
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/period.rb, line 72
def respond_to_missing?(method_name, include_private = false)
  method_name.match?(/(last|next)_\d+_(day|week|month|quarter|year)s?(_from_now)?/) || super
end