Sha256: 51ec865350710f8dadaf5ec633d1e01c9a52a55080bb92d002218ecdeb224e17

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

module Spree::Report::DateSlicer
  def self.slice_into(start_date, end_date, time_scale, klass)
    case time_scale
    when :hourly
      slice_hours_into(start_date, end_date, klass)
    when :daily
      slice_days_into(start_date, end_date, klass)
    when :monthly
      slice_months_into(start_date, end_date, klass)
    when :yearly
      slice_years_into(start_date, end_date, klass)
    end
  end

  def self.slice_hours_into(start_date, end_date, klass)
    current_date = start_date
    slices = []
    while current_date < end_date
      slices << (0..23).collect do |hour|
        obj = klass.new
        obj.date = current_date
        obj.hour = hour
        obj
      end
      current_date = current_date.next_day
    end
    slices.flatten
  end

  def self.slice_days_into(start_date, end_date, klass)
    current_date = start_date
    slices = []
    while current_date < end_date
      obj = klass.new
      obj.date = current_date
      slices << obj
      current_date = current_date.next_day
    end
    slices
  end

  def self.slice_months_into(start_date, end_date, klass)
    current_date = start_date
    slices = []
    while current_date < end_date
      obj = klass.new
      obj.date = current_date
      slices << obj
      current_date = current_date.end_of_month.next_day
    end
    slices
  end

  def self.slice_years_into(start_date, end_date, klass)
    (start_date.year..end_date.year).collect do |year|
      obj = klass.new
      obj.date = Date.new(year).end_of_year
      obj
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
spree_admin_insights-3.2.0 app/reports/spree/report/date_slicer.rb
solidus_admin_insights-2.1.1 app/reports/spree/report/date_slicer.rb
solidus_admin_insights-2.1.0 app/reports/spree/report/date_slicer.rb