lib/acb.rb in acb-0.1.1 vs lib/acb.rb in acb-0.1.2

- old
+ new

@@ -1,111 +1,39 @@ # frozen_string_literal: true require_relative 'acb/version' require 'active_support' -require 'forwardable' require 'csv' module Acb extend ActiveSupport::Concern - class Header - extend Forwardable - include Enumerable + autoload :Column, 'acb/column' + autoload :Columns, 'acb/columns' - attr_accessor :columns - - def_delegator :@columns, :each - - def initialize - @columns = [] - end - - def push(name, **options) - @columns.push(Column.new(name, **options)) - end - - def keys - @columns.map(&:name) - end - - def get_data(row) - @columns.map { |column| column.digest(row) } - end - end - - class Column - attr_reader :name - - def initialize(name, **options) - @name = name - @index = case options[:index] - when Proc - options[:index] - when String - options[:index].split('.') - else - [name] - end - @format = options[:format] - end - - def digest(row) - data = _digest(row) - @format && data ? format(data) : data - end - - def _digest(row) - if @index.is_a?(Array) - @index.reduce(row) do |acc, set| - acc.send(set) - end - else - @index.call(row) - end - end - - def format(data) - case data - when Date, Time - data.strftime(@format) - else - @format % data - end - end - end - class_methods do - def header - @header ||= Header.new + def columns + @columns ||= Columns.new end def add_column(name:, **options) - header.push(name, **options) + columns.push(name, **options) end end - def initialize(options = {}) - @options = options - end - def data @data = @data.preload(relations) if respond_to?(:relations) @data.find_each end - def header - self.class.header.keys - end - def get_data_from(row) - self.class.header.get_data(row) + self.class.columns.get_data(row) end - def content_string - header_content = header.join(',') + def content_string(**options) + header_content = self.class.columns.header.join(',') - CSV.generate(header_content, **@options) do |csv| + CSV.generate(header_content, **options) do |csv| data.each do |row| csv << get_data_from(row) end end end