class ActiveCharts::Helpers::CollectionParser

Attributes

collection[R]
columns[R]
label_column[R]
rows[R]

Public Class Methods

new(resource_collection, columns, label_column) click to toggle source
# File lib/active_charts/helpers/collection_parser.rb, line 6
def initialize(resource_collection, columns, label_column)
  resource = resource_collection.first.class

  @label_column = label_column || auto_label_column(resource)
  @columns = valid_columns(resource, columns)
  @rows = resource_collection.pluck(@label_column)
  @collection = resource_collection.pluck(*@columns)
end

Public Instance Methods

series_labels() click to toggle source
# File lib/active_charts/helpers/collection_parser.rb, line 17
def series_labels
  columns.map(&:to_s).map(&:titleize)
end
xy_collection() click to toggle source
# File lib/active_charts/helpers/collection_parser.rb, line 29
def xy_collection
  collection.map do |row|
    x_val = row.first

    row[1..-1].map do |y_val|
      [x_val, y_val]
    end
  end
end
xy_series_labels() click to toggle source
# File lib/active_charts/helpers/collection_parser.rb, line 21
def xy_series_labels
  x_label = series_labels.first

  series_labels[1..-1].map do |y_label|
    "#{x_label} vs. #{y_label}"
  end
end

Private Instance Methods

auto_label_column(resource) click to toggle source
# File lib/active_charts/helpers/collection_parser.rb, line 49
def auto_label_column(resource)
  attribute_names = resource.new.attribute_names

  %w[name title id].each do |attribute_name|
    return attribute_name.to_sym if attribute_names.include?(attribute_name)
  end

  attribute_names.first.to_sym
end
valid_columns(resource, columns) click to toggle source
# File lib/active_charts/helpers/collection_parser.rb, line 41
def valid_columns(resource, columns)
  attribute_names = resource.new.attribute_names.map(&:to_sym) 

  return attribute_names if columns.eql?([])

  attribute_names & columns
end