class Rorschart::RorschartData

Attributes

cols[RW]
rows[RW]

Public Class Methods

new(raw_data) click to toggle source
# File lib/rorschart/data/rorschart_data.rb, line 7
def initialize(raw_data)

    data = convert_objects_to_array_of_hash(raw_data)

    case data
    when Array          
      case data.first     
      when Array
        cols = columns_from_array_row(data.first)
      when Hash
        cols = columns_from_hash_row(data)
        data = data.map{|row| row.values}
      end
    when Hash
      cols = columns_from_array_row(data.first)     
    end

    @rows = data.to_a
    @cols = cols
end

Public Instance Methods

sort_by_date!() click to toggle source
# File lib/rorschart/data/rorschart_data.rb, line 28
def sort_by_date!

  return if @cols.blank?
  
  if ['datetime', 'date'].include? @cols.first[:type]
    @rows = @rows.sort_by{ |c| c.first }
  end

end

Private Instance Methods

columns_from_array_row(array_row) click to toggle source
# File lib/rorschart/data/rorschart_data.rb, line 63
def columns_from_array_row(array_row)
  array_row.map { |c|
    {:type => type_to_string(c), :label => default_label_for_type(c)}
  }
end
columns_from_hash_row(data) click to toggle source
# File lib/rorschart/data/rorschart_data.rb, line 56
def columns_from_hash_row(data)
  hash_row = flatten_array_hash(data)
  hash_row.map { |c|
    {:type => type_to_string(c[1]), :label => c[0]}
  }
end
convert_objects_to_array_of_hash(data) click to toggle source
# File lib/rorschart/data/rorschart_data.rb, line 40
def convert_objects_to_array_of_hash(data)

  data = data.to_a if data.is_a? ActiveRecord::Relation
  data = [] << data if data.is_a? ActiveRecord::Base

  if data.is_a? Array and data.first.is_a? ActiveRecord::Base
    data = data.map{|m| model_to_hash_without_empty_primary_key(m)}     
  end

  return data
end
default_label_for_type(cel) click to toggle source
# File lib/rorschart/data/rorschart_data.rb, line 77
def default_label_for_type(cel)
  return 'Date' if (cel.is_a? DateTime) or (cel.is_a? Date)
  return 'Value'
end
flatten_array_hash(data) click to toggle source
# File lib/rorschart/data/rorschart_data.rb, line 82
def flatten_array_hash(data)
  data.inject({}){|row, hash| row.merge(hash)}
end
model_to_hash_without_empty_primary_key(object) click to toggle source
# File lib/rorschart/data/rorschart_data.rb, line 52
def model_to_hash_without_empty_primary_key(object)
  object.attributes.except(object.class.primary_key)
end
type_to_string(cel) click to toggle source
# File lib/rorschart/data/rorschart_data.rb, line 69
def type_to_string(cel)
  return 'number' if (cel.is_a? Integer) or (cel.is_a? Float) or cel.nil?
  return 'datetime' if (cel.is_a? DateTime) or (cel.is_a? Time) 
  return 'date' if cel.is_a? Date
  return 'boolean' if (!!cel == cel)
  return 'string'
end