Class: ActivePeriod::FreePeriod

Inherits:
Period show all
Includes:
HasMany::Days, HasMany::Months, HasMany::Quarters, HasMany::Weeks, HasMany::Years
Defined in:
lib/active_period/free_period.rb

Instance Method Summary collapse

Methods included from HasMany::Years

#years

Methods included from HasMany::Quarters

#quarters

Methods included from HasMany::Months

#months

Methods included from HasMany::Weeks

#weeks

Methods included from HasMany::Days

#days

Methods inherited from Period

#&, #==, #===, #beginless?, #boundless?, #calculated_begin, #calculated_end, #endless?, #infinite?, #initialize, #next, #prev, #|

Methods included from HasMany::Holidays

#holidays

Methods included from Comparable

#<=>, #include?

Methods inherited from Range

#to_period

Constructor Details

This class inherits a constructor from ActivePeriod::Period

Instance Method Details

#+(duration) ⇒ self

Shift a period to the future

Returns:

  • (self)

    A new period of the same kind



34
35
36
# File 'lib/active_period/free_period.rb', line 34

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

#-(duration) ⇒ self

Shift a period to the past

Returns:

  • (self)

    A new period of the same kind



27
28
29
# File 'lib/active_period/free_period.rb', line 27

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

#bounding_formatObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_period/free_period.rb', line 54

def bounding_format
  if boundless?
    :boundless_format
  elsif beginless?
    :beginless_format
  elsif endless?
    :endless_format
  else
    :default_format
  end
end

#endingObject



66
67
68
69
70
71
72
# File 'lib/active_period/free_period.rb', line 66

def ending
  if exclude_end?
    :excluded
  else
    :included
  end
end

#i18n {|from, to, exclude_end?| ... } ⇒ Object

If no block given, it’s an alias to to_s For a block {|from,to| … }

Yield Parameters:

  • from (DateTime|Nil)

    the start of the period

  • to (DateTime|Nil)

    the end of the period

  • exclude_end? (Boolean)

    is the ending of the period excluded



79
80
81
82
83
# File 'lib/active_period/free_period.rb', line 79

def i18n(&block)
  return yield(from, to, exclude_end?) if block.present?

  to_s
end

#strftime(format) ⇒ String

Returns Formated string.

Parameters:

  • format (String)

    A valid format for I18n.l

Returns:

  • (String)

    Formated string



40
41
42
# File 'lib/active_period/free_period.rb', line 40

def strftime(format)
  to_s(format: format)
end

#to_iActiveSupport::Duration

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

Returns:

  • (ActiveSupport::Duration)

    Number of day



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

def to_i
  return Float::INFINITY if infinite?
  days.count.days
end

#to_s(format: '%d %B %Y') ⇒ String

Returns Formated string.

Parameters:

  • format (String) (defaults to: '%d %B %Y')

    A valid format for I18n.l

Returns:

  • (String)

    Formated string



46
47
48
49
50
51
52
# File 'lib/active_period/free_period.rb', line 46

def to_s(format: '%d %B %Y')
  I18n.t(bounding_format,
         scope: %i[active_period free_period],
         from:  I18n.l(self.begin, format: format, default: nil),
         to:    I18n.l(self.end,   format: format, default: nil),
         ending: I18n.t(ending, scope: %i[active_period]))
end