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

Class Method Summary collapse

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_timeObject

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

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/period.rb', line 57

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(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

Returns:

  • (Boolean)


77
78
79
# File 'lib/period.rb', line 77

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