Module: DatePeriodParser

Defined in:
lib/date_period_parser.rb,
lib/date_period_parser/version.rb

Defined Under Namespace

Classes: Base

Constant Summary collapse

DEFAULT_OFFSET =
"+00:00".freeze
VERSION =
"0.2.4"

Class Method Summary collapse

Class Method Details

.parse(period, options = {}) ⇒ Array<DateTime, DateTime>?

Returns array of start and end DateTime of given period string.

Examples:

Basic useage

DatePeriodParser.parse("2014")
# => [
#   #<DateTime 2014-01-01T00:00:00.000+0000">,
#   #<DateTime 2014-12-31T23:59:59.999+0000">
# ]

with timezone offsets:

from,until = DatePeriodParser.parse("2014", offset: "+0700")
from    # => #<DateTime 2014-01-01T00:00:00.000+0700">
until   # => #<DateTime 2014-12-31T23:59:59.999+0700">

invalid periods

DatePeriodParser.parse("123213") # => nil
from,until = DatePeriodParser.parse("123213")
from    # => nil
until   # => nil

Parameters:

  • period (String)

    date period string.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :default (String) — default: nil

    use this default if `period` is nil

  • :offset (String) — default: "+0000"

    timezone offset, e.g. “+0700”

Returns:

  • (Array<DateTime, DateTime>)

    start and end DateTime

  • (nil)

    if period string is invalid


34
35
36
37
38
# File 'lib/date_period_parser.rb', line 34

def parse(period, options = {})
  parse!(period, options)
rescue ArgumentError => e
  nil
end

.parse!(period, options = {}) ⇒ Array<DateTime, DateTime>

Same as #parse but raises an ArgumentError if period string is invalid

Examples:

Basic useage

def my_method
  from,until = DatePeriodParser.parse!("FOOBAR")
rescue ArgumentError => e
   # do something
end

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

  • period (String)

    date period string.

Options Hash (options):

  • :default (String) — default: nil

    use this default if `period` is nil

  • :offset (String) — default: "+0000"

    timezone offset, e.g. “+0700”

Returns:

  • (Array<DateTime, DateTime>)

    start and end DateTime

Raises:

  • (ArgumentError)

    if period string is invalid

See Also:

  • #parse

56
57
58
59
# File 'lib/date_period_parser.rb', line 56

def parse!(period, options = {})
  period = options[:default] if period.nil? || period.empty?
  Base.new(period, options).parse
end

.range(period, options = {}) ⇒ Range<DateTime, DateTime>

Same as #parse but returns a range instead

Examples:

Basic useage

rng = DatePeriodParser.range("2014")
rng.member? DateTime.new(2014,8,6)

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

  • period (String)

    date period string.

Options Hash (options):

  • :default (String) — default: nil

    use this default if `period` is nil

  • :offset (String) — default: "+0000"

    timezone offset, e.g. “+0700”

Returns:

  • (Range<DateTime, DateTime>)

    start and end DateTime as range

Raises:

  • (ArgumentError)

    if period string is invalid

See Also:

  • #parse

74
75
76
77
78
# File 'lib/date_period_parser.rb', line 74

def range(period, options = {})
  range!(period, options)
rescue ArgumentError => e
  nil
end

.range!(period, options = {}) ⇒ Range<DateTime, DateTime>

Same as #range but raises an ArgumentError if period string is invalid

Examples:

Basic useage

def my_method
  rng = DatePeriodParser.range!("FOOBAR")
rescue ArgumentError => e
   # do something
end

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

  • period (String)

    date period string.

Options Hash (options):

  • :default (String) — default: nil

    use this default if `period` is nil

  • :offset (String) — default: "+0000"

    timezone offset, e.g. “+0700”

Returns:

  • (Range<DateTime, DateTime>)

    start and end DateTime as range

Raises:

  • (ArgumentError)

    if period string is invalid

See Also:

  • #parse

96
97
98
99
100
# File 'lib/date_period_parser.rb', line 96

def range!(period, options = {})
  period = options[:default] if period.nil? || period.empty?
  first,last = Base.new(period, options).parse
  first..last
end