Class: ActivePeriod::StandardPeriod

Inherits:
Period show all
Defined in:
lib/active_period/standard_period.rb

Direct Known Subclasses

Day, Month, Quarter, Week, Year

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Period

#&, #==, #===, #beginless?, #boundless?, #calculated_begin, #calculated_end, #endless?, #i18n, #infinite?, #strftime, #to_s, #|

Methods included from HasMany::Holidays

#holidays

Methods included from Comparable

#<=>, #include?

Methods inherited from Range

#to_period

Constructor Details

#initialize(object) ⇒ StandardPeriod

Returns a new instance of StandardPeriod.



4
5
6
7
8
# File 'lib/active_period/standard_period.rb', line 4

def initialize(object)
  raise I18n.t(:base_class_id_abstract, scope: %i[active_period standard_period]) if self.class == StandardPeriod
  time = time_parse(object, I18n.t(:date_is_invalid, scope: %i[active_period standard_period]) )
  super(time.send("beginning_of_#{_period}")..time.send("end_of_#{_period}"))
end

Class Method Details

._periodString

Returns get the name of the standard period.

Returns:

  • (String)

    get the name of the standard period



24
25
26
# File 'lib/active_period/standard_period.rb', line 24

def self._period
  name.split('::').last.downcase
end

Instance Method Details

#+(duration) ⇒ self

Shift a period to the past acording to her ending point

Returns:

  • (self)

    A new period of the same kind



43
44
45
# File 'lib/active_period/standard_period.rb', line 43

def +(duration)
  self.class.new(to + duration)
end

#-(duration) ⇒ self

Shift a period to the past acording to her starting point

Returns:

  • (self)

    A new period of the same kind



37
38
39
# File 'lib/active_period/standard_period.rb', line 37

def -(duration)
  self.class.new(from - duration)
end

#_periodObject



19
20
21
# File 'lib/active_period/standard_period.rb', line 19

def _period
  self.class._period
end

#i18n_scopeObject



47
48
49
# File 'lib/active_period/standard_period.rb', line 47

def i18n_scope
  [:active_period, :standard_period, _period]
end

#nextObject Also known as: succ



10
11
12
# File 'lib/active_period/standard_period.rb', line 10

def next
  self.class.new(from.send("next_#{_period}"))
end

#prevObject



15
16
17
# File 'lib/active_period/standard_period.rb', line 15

def prev
  self.class.new(from.send("prev_#{_period}"))
end

#to_iActiveSupport::Duration

Don’t realy return an Integer. ActiveSupport::Duration is a better numeric representation a in time manipulation context

Returns:

  • (ActiveSupport::Duration)


31
32
33
# File 'lib/active_period/standard_period.rb', line 31

def to_i
  1.send(_period)
end