Module: Rawbotz::Datapolate

Defined in:
lib/rawbotz/datapolate.rb

Class Method Summary collapse

Class Method Details

.create_data(sales, stock) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rawbotz/datapolate.rb', line 23

def self.create_data sales, stock
  # from [date, sales] and [date, stock] create realistic list
  from_date, to_date = date_minmax sales, stock
  return {} if from_date.nil? || to_date.nil?
  days = explode_days from_date, to_date

  stock_data = Hash.new(nil)
  stock.each{|s| stock_data[s.date.to_date] = s.qty}
  sales_data = Hash.new(nil)
  sales.each{|s| sales_data[s[0].to_date] = s.fetch(1)}

  data = {}
  days.each do |day|
    data[day] = {label: day.to_date,
                 stock: stock_data[day],
                 sales: sales_data[day]
    }
  end
  data
end

.date_minmax(sales, stock) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rawbotz/datapolate.rb', line 3

def self.date_minmax sales, stock
  #sales.each{|sale| sale[0] = Date.strptime(sale[0])}
  sales_first, sales_last = sales.minmax_by{|s| s[0]}
  stock_first, stock_last = stock.minmax_by{|s| s.date}
  first_dates = []
  first_dates << sales_first[0]   if !sales_first.nil?
  first_dates << stock_first.date.to_date if !stock_first.nil?

  last_dates = []
  last_dates << sales_last[0]   if !sales_last.nil?
  last_dates << stock_last.date.to_date if !stock_last.nil?

  [first_dates.min, last_dates.max]
end

.explode_days(min_date, max_date) ⇒ Object



18
19
20
21
# File 'lib/rawbotz/datapolate.rb', line 18

def self.explode_days min_date, max_date
  return [] if min_date.nil? || max_date.nil?
  (min_date.to_date..max_date.to_date).to_a
end