Sha256: 7a231538104722faff388a4d574dbc8462aaf33d141459f0676a9814b8e45b0b

Contents?: true

Size: 937 Bytes

Versions: 22

Compression:

Stored size: 937 Bytes

Contents

# frozen_string_literal: true

String.class_eval do
  # Return the quarter start date.
  #
  # @return [Date]
  #
  def quarter_start
    quarter_date.beginning_of_quarter
  end

  # Return the quarter end date.
  #
  # @return [Date]
  #
  def quarter_end
    quarter_date.end_of_quarter
  end

  private

    # Parses a string for a quarter date. Expects the string to follow: 'qQyy' where
    # q = 1/2/3/4 and yy to be the last two digits of the year.
    #
    # @raise [RubyRailsExtensions::Errors::InvalidFormatError] if `self` is not qQyy
    #
    # @return [Date]
    #
    def quarter_date
      unless match?(/\A[1-4]q\d\d\z/i)
        raise(RubyRailsExtensions::Errors::InvalidFormatError, "Unexpected format (given #{self}, expected qQyy)")
      end

      values = scan(/\d/)
      quarter = values.shift.to_i
      year = "20#{values.join('')}".to_i
      month = quarter * 3

      Date.new(year, month, 1)
    end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
ruby-rails-extensions-2.1.2 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.2.pre.pre.pre.1 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.1 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.14 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.13 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.12 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.11 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.10 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.9 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.8 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.7 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.6 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.5 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.4 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.3 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.2 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.1.0.pre.rc.1 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.0.1 lib/ruby-rails-extensions/extensions/quarter_dates.rb
ruby-rails-extensions-2.0.1.pre.rc.1 lib/ruby-rails-extensions/extensions/quarter_dates.rb