Module: Rubualizations::Tableable

Defined in:
lib/rubualizations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cols_defObject

Returns the value of attribute cols_def.



4
5
6
# File 'lib/rubualizations.rb', line 4

def cols_def
  @cols_def
end

Instance Method Details

#col_label(value) ⇒ Object



78
79
80
81
82
83
# File 'lib/rubualizations.rb', line 78

def col_label(value)
   values = value.split('_')
   values.each { |v| v.downcase! }
   values[0].capitalize!
   return values.join " "
end

#continuous_with_cols(hashes, cols) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubualizations.rb', line 40

def continuous_with_cols(hashes, cols)
   key = cols[0]
   cols.each do |c|
      cumul_col = c[/cumulation\((.*)\)/, 1]
      cumulation = 0
      hashes.map do |h| 
         h[key] = format_datetime_continuous(h[key]) if c.eql?(key) 
         unless cumul_col.nil?
            @cols_def[c] = self.cumul_col_def cumul_col
            h[c] = cumulation += h[cumul_col] 
         end
      end
   end
   return self.hashes_to_table(hashes, cols) 
end

#cumul_col_def(col) ⇒ Object



64
65
66
# File 'lib/rubualizations.rb', line 64

def cumul_col_def(col)
   {id: "cumulation(col)", label:"#{col_label(col)} Cumulation", type: "number"}
end

#discrete_with_cols(hashes, cols) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubualizations.rb', line 21

def discrete_with_cols(hashes, cols)
   key = cols[0]
   date_key, date_unit = key.split "__"
   prev_hash = nil
   hashes.delete_if do |h|
      h[key] = format_datetime_mmyy(h[date_key]) unless date_unit.nil?
      if !prev_hash.nil? and h[key].eql? prev_hash[key]
         prev_hash.merge!(h) do |k,o,n| 
            if k.eql?(key) or !n.respond_to?("+") then n 
            else n+o end
         end
      else
         prev_hash = h
         false
      end
   end
   return self.hashes_to_table(hashes, cols) 
end

#format_datetime_continuous(datetime) ⇒ Object



60
61
62
# File 'lib/rubualizations.rb', line 60

def format_datetime_continuous(datetime) 
   "Date(#{datetime.strftime('%Y')}, #{datetime.strftime('%-m').to_i - 1}, #{datetime.strftime('%-d')}, #{datetime.strftime('%k')}, #{datetime.strftime('%M')}, #{datetime.strftime('%S')})"
end

#format_datetime_mmyy(datetime) ⇒ Object



56
57
58
# File 'lib/rubualizations.rb', line 56

def format_datetime_mmyy(datetime)
   datetime.strftime "%m/%y"
end

#hashes_to_table(hashes, cols) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/rubualizations.rb', line 68

def hashes_to_table(hashes, cols)
   table = { cols: @cols_def.values_at(*cols), rows:[]}
   hashes.each do |hash|
      row = {}
      row[:c] = hash.values_at(*cols).map{|v| {v:v}}
      table[:rows] << row
   end
   return table
end

#tablize_hashes(hashes, params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rubualizations.rb', line 6

def tablize_hashes(hashes, params)
   if params[:cols].to_s == ""
      cols = @cols_def.keys
   else
      cols = params[:cols].delete(" ").split(",")
   end
   s_hashes = hashes.sort_by {|h| h[cols[0]]}
   if params[:type] == "discrete"
      return self.discrete_with_cols(s_hashes, cols)
   else
      return self.continuous_with_cols(s_hashes, cols)
   end
end