Module: RVGP::Utilities

Included in:
Base::Grid, Base::Grid, Base::Reconciler, Reconcilers::CsvReconciler
Defined in:
lib/rvgp/utilities.rb,
lib/rvgp/utilities/yaml.rb,
lib/rvgp/utilities/grid_query.rb
more...

Overview

This module contains helper methods used throughout RVGP. These are just common codepaths, that have little in common, save for their general utility.

Defined Under Namespace

Classes: GridQuery, Yaml

Instance Method Summary collapse

Instance Method Details

#months_through(date, ...) ⇒ Array<Date>

This returns each month in a series from the first date, to the last, in the provided array of dates

Parameters:

  • date (Array<Date>)

    A date, that will be used to calculate the range of months to construct a range from.

  • ... (Array<Date>)

    More dates. This method will automatically select the max and min from the sample provided.

Returns:

  • (Array<Date>)

    An array, containing a Date, set to the first of every month, in the provided range.

[View source]

14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rvgp/utilities.rb', line 14

def months_through(*args)
  dates = args.flatten.uniq.sort

  ret = []
  unless dates.empty?
    d = Date.new dates.first.year, dates.first.month, 1 # start_at
    while d <= Date.new(dates.last.year, dates.last.month, 1) # end_at
      ret << d
      d = d >> 1
    end
  end
  ret
end

#string_to_regex(str) ⇒ Regexp

Convert the provided string, into a Regexp. Note that the the ixm suffixes are supported, unlike ruby’s Regexp.new(str) method

Parameters:

  • str (String)

    A string, in the ‘standard’ regexp format: ‘/Running (?:to|at) the Park/i’

Returns:

  • (Regexp)

    The conversion to a useable regexp, for the provided string

[View source]

32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rvgp/utilities.rb', line 32

def string_to_regex(str)
  if %r{\A/(.*)/([imx]?[imx]?[imx]?)\Z}.match str
    Regexp.new(::Regexp.last_match(1), ::Regexp.last_match(2).chars.map do |c|
      case c
      when 'i' then Regexp::IGNORECASE
      when 'x' then Regexp::EXTENDED
      when 'm' then Regexp::MULTILINE
      end
    end.reduce(:|))
  end
end