class Squib::DataFrame

Public Class Methods

new(hash = {}, def_columns = true) click to toggle source
# File lib/squib/import/data_frame.rb, line 10
def initialize(hash = {}, def_columns = true)
  @hash = hash
  columns.each { |col| def_column(col) } if def_columns
end

Public Instance Methods

[](i) click to toggle source
# File lib/squib/import/data_frame.rb, line 19
def [](i)
  @hash[i]
end
[]=(col, v) click to toggle source
# File lib/squib/import/data_frame.rb, line 23
def []=(col, v)
  @hash[col] = v
  def_column(col)
  return v
end
col?(col) click to toggle source
# File lib/squib/import/data_frame.rb, line 37
def col?(col)
  @hash.key? col
end
columns() click to toggle source
# File lib/squib/import/data_frame.rb, line 29
def columns
  @hash.keys
end
each(&block) click to toggle source
# File lib/squib/import/data_frame.rb, line 15
def each(&block)
  @hash.each(&block)
end
ncolumns() click to toggle source
# File lib/squib/import/data_frame.rb, line 33
def ncolumns
  @hash.keys.size
end
nrows() click to toggle source
# File lib/squib/import/data_frame.rb, line 45
def nrows
  @hash.inject(0) { |max, (_n, col)| col.size > max ? col.size : max }
end
row(i) click to toggle source
# File lib/squib/import/data_frame.rb, line 41
def row(i)
  @hash.inject(Hash.new) { |ret, (name, arr)| ret[name] = arr[i]; ret }
end
to_h() click to toggle source
# File lib/squib/import/data_frame.rb, line 57
def to_h
  @hash
end
to_json() click to toggle source
# File lib/squib/import/data_frame.rb, line 49
def to_json
  @hash.to_json
end
to_pretty_json() click to toggle source
# File lib/squib/import/data_frame.rb, line 53
def to_pretty_json
  JSON.pretty_generate(@hash)
end
to_pretty_text() click to toggle source
# File lib/squib/import/data_frame.rb, line 61
def to_pretty_text
  max_col = columns.inject(0) { |max, c | c.length > max ? c.length : max }
  top    = " ╭#{'-' * 36}╮\n"
  bottom = " ╰#{'-' * 36}╯\n"
  str =  ''
  0.upto(nrows - 1) do | i |
    str += (' ' * max_col) + top
    row(i).each do |col, data|
      str += "#{col.rjust(max_col)} #{wrap_n_pad(data, max_col)}"
    end
    str += (' ' * max_col) + bottom
  end
  return str
end

Private Instance Methods

def_column(col) click to toggle source
# File lib/squib/import/data_frame.rb, line 98
def def_column(col)
  raise "Column #{col} - does not exist" unless @hash.key? col
  method_name = snake_case(col)
  return if self.class.method_defined?(method_name) #warn people? or skip?
  define_singleton_method method_name do
    @hash[col]
  end
end
snake_case(str) click to toggle source
# File lib/squib/import/data_frame.rb, line 78
def snake_case(str)
  str.to_s.
      strip.
      gsub(/\s+/,'_').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z]+)([A-Z])/,'\1_\2').
      downcase.
      to_sym
end
wrap_n_pad(str, max_col) click to toggle source
# File lib/squib/import/data_frame.rb, line 88
def wrap_n_pad(str, max_col)
  new_str = str.to_s + ' ' # handle nil & empty strings
  new_str = new_str.
              scan(/.{1,34}/). # break down
              map { |s| (' ' * max_col) + " | " + s.ljust(34) }.
              join(" |\n").
              lstrip      # initially no whitespace next to key
  return new_str + " |\n"
end