lib/fat_period/period.rb
# Return an array of Periods wholly-contained within self in chunks of size,
# defaulting to monthly chunks. Partial chunks at the beginning and end of
# self are not included unless `partial_first` or `partial_last`,
# respectively, are set true. The last chunk can be made to extend beyond the
# end of self to make it a whole chunk if `round_up_last` is set true, in
# which case, partial_last is ignored.
#
# @example
#   Period.parse('2015').chunks(size: :month) #=>
#    [Period(2015-01-01..2015-01-31),
#     Period(2015-02-01..2015-02-28),
#     Period(2015-03-01..2015-03-31),
#     Period(2015-04-01..2015-04-30),
#     Period(2015-05-01..2015-05-31),
#     Period(2015-06-01..2015-06-30),
#     Period(2015-07-01..2015-07-31),
#     Period(2015-08-01..2015-08-31),
#     Period(2015-09-01..2015-09-30),
#     Period(2015-10-01..2015-10-31),
#     Period(2015-11-01..2015-11-30),
#     Period(2015-12-01..2015-12-31)
#    ]
#
#   Period.parse('2015').chunks(size: :week) #=>
#    [Period(2015-01-05..2015-01-11), # Note that first week starts after Jan 1.
#     Period(2015-01-12..2015-01-18),
#     Period(2015-01-19..2015-01-25),
#     Period(2015-01-26..2015-02-01),
#     ...
#     Period(2015-12-07..2015-12-13),
#     Period(2015-12-14..2015-12-20),
#     Period(2015-12-21..2015-12-27)] # Note that last week ends before Dec 31
#
#   Period.parse('2015').chunks(size: :week, partial_first: true, partial_last: true) #=>
#    [Period(2015-01-01..2015-01-04), # Note the partial week starting Jan 1
#     Period(2015-01-05..2015-01-11),
#     Period(2015-01-12..2015-01-18),
#     Period(2015-01-19..2015-01-25),
#     Period(2015-01-26..2015-02-01),
#     ...
#     Period(2015-12-07..2015-12-13),
#     Period(2015-12-14..2015-12-20),
#     Period(2015-12-21..2015-12-27)
#     Period(2015-12-28..2015-12-31) # Note partial week ending Dec 31
#     ]
#
#   Period.parse('2015').chunks(size: :week, partial_first: true, round_up_last: true) #=>
#    [Period(2015-01-01..2015-01-04), # Note the partial week starting Jan 1
#     Period(2015-01-05..2015-01-11),
#     Period(2015-01-12..2015-01-18),
#     Period(2015-01-19..2015-01-25),
#     Period(2015-01-26..2015-02-01),
#     ...
#     Period(2015-12-07..2015-12-13),
#     Period(2015-12-14..2015-12-20),
#     Period(2015-12-21..2015-12-27)
#     Period(2015-12-28..2016-01-03) # Note full week extending beyond self
#     ]
#
# @raise ArgumentError if size of chunks is larger than self or if an invalid
#   chunk size.
# @param size [Symbol] a chunk symbol, :year, :half. :quarter, etc.
# @param partial_first [Boolean] allow a period less than a full :size period
#   as the first period in the returned array.
# @param partial_last [Boolean] allow a period less than a full :size period
#   as the last period in the returned array.
# @param round_up_last [Boolean] allow the last period in the returned array
#   to extend beyond the end of self.
# @return [Array<Period>] periods that subdivide self into chunks of size, `size`
Return an array of Periods wholly-contained within self in chunks of size,
defaulting to monthly chunks. Partial chunks at the beginning and end of
self are not included unless `partial_first` or `partial_last`,
respectively, are set true. The last chunk can be made to extend beyond the
end of self to make it a whole chunk if `round_up_last` is set true, in
which case, partial_last is ignored.
Return an array of Periods wholly - contained within self in chunks of size defaulting to monthly chunks . Partial chunks at the beginning and end of self are not included unless ` partial_first ` or ` partial_last ` respectively are set true . The last chunk can be made to extend beyond the end of self to make it a whole chunk if ` round_up_last ` is set true in which case partial_last is ignored .
def chunks(size: :month, partial_first: false, partial_last: false,
             round_up_last: false)
    chunk_size = size.to_sym
    raise ArgumentError, "unknown chunk size '#{chunk_size}'" unless CHUNKS.include?(chunk_size)
    containing_period = Period.chunk_containing(first, chunk_size)
    return [dup] if self == containing_period
    result = []
    if proper_subset_of?(containing_period)
      result =
        if partial_first || partial_last
          if round_up_last
            [containing_period]
          else
            [dup]
          end
        else
          []
        end
      return result
    end
    chunk_start = first.dup
    chunk_end = chunk_start.end_of_chunk(chunk_size)
    if chunk_start.beginning_of_chunk?(chunk_size) || partial_first
      result << Period.new(chunk_start, chunk_end)
    end
    chunk_start = chunk_end + 1.day
    chunk_end = chunk_start.end_of_chunk(chunk_size)
    while chunk_end <= last
      result << Period.new(chunk_start, chunk_end)
      chunk_start = chunk_end + 1.day
      chunk_end = chunk_start.end_of_chunk(chunk_size)
    end
    if chunk_start < last
      if round_up_last
        result << Period.new(chunk_start, chunk_end)
      elsif partial_last
        result << Period.new(chunk_start, last)
      else
        result
      end
    end
    if partial_last && !partial_first && result.empty?
      result << Period.new(first, last)
    end
    result
  end
def chunks ( size : :month , partial_first : false , partial_last : false , round_up_last : false ) chunk_size = size . to_sym raise ArgumentError , "unknown chunk size '#{chunk_size}'" unless CHUNKS . include? ( chunk_size ) containing_period = Period . chunk_containing ( first , chunk_size ) return [ dup ] if self == containing_period result = [ ] if proper_subset_of? ( containing_period ) result = if partial_first || partial_last if round_up_last [ containing_period ] else [ dup ] end else [ ] end return result end chunk_start = first . dup chunk_end = chunk_start . end_of_chunk ( chunk_size ) if chunk_start . beginning_of_chunk? ( chunk_size ) || partial_first result << Period . new ( chunk_start , chunk_end ) end chunk_start = chunk_end + 1 . day chunk_end = chunk_start . end_of_chunk ( chunk_size ) while chunk_end <= last result << Period . new ( chunk_start , chunk_end ) chunk_start = chunk_end + 1 . day chunk_end = chunk_start . end_of_chunk ( chunk_size ) end if chunk_start < last if round_up_last result << Period . new ( chunk_start , chunk_end ) elsif partial_last result << Period . new ( chunk_start , last ) else result end end if partial_last && ! partial_first && result . empty? result << Period . new ( first , last ) end result end
Return an array of Periods wholly-contained within self in chunks of size,
defaulting to monthly chunks.
Return an array of Periods wholly - contained within self in chunks of size defaulting to monthly chunks .
# Period too small for even a single chunk and is wholly-contained by a # single chunk. # Keep the first chunk if it's whole or partials allowed # Add Whole chunks # Possibly append the final chunk to result # Catch the case where the period is too small to make a whole chunk and # partial_first is false, so it did not get included as the initial # partial chunk, yet a partial_last is allowed, so include the whole # period as a partial chunk.
periods that subdivide self into chunks of size, `size`
periods that subdivide self into chunks of size ` size `
Array<Period>
ArgumentError if size of chunks is larger than self or if an invalid chunk size.
ArgumentError if size of chunks is larger than self or if an invalid chunk size .
size
a chunk symbol, :year, :half. :quarter, etc.
a chunk symbol : year : half . : quarter etc .
partial_first
allow a period less than a full :size period as the first period in the returned array.
allow a period less than a full : size period as the first period in the returned array .
partial_last
allow a period less than a full :size period as the last period in the returned array.
allow a period less than a full : size period as the last period in the returned array .
round_up_last
allow the last period in the returned array to extend beyond the end of self.
allow the last period in the returned array to extend beyond the end of self .
example
None