class SeeAsVee::Producers::Hashes

Constants

HUMANIZER
INTERNAL_PARAMS
NORMALIZER

Public Class Methods

new(*args, **params) click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 12
def initialize *args, **params
  @hashes = []
  @params = params
  add(*args)
end

Private Class Methods

csv(*args, **params) click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 87
def csv *args, **params
  constructor_params, params = split_params(**params)
  result, = Hashes.new(*args, **constructor_params).to_sheet.produce csv: true, xlsx: false, **params
  result
end
join(*args, normalize: :human) click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 77
def join *args, normalize: :human
  Hashes.new(*args).join.tap do |result|
    case normalize
    when :humanize, :human, :pretty then result.map!(&HUMANIZER)
    when :string, :str, :to_s then result.map!(&NORMALIZER.curry[false])
    when :symbol, :sym, :to_sym then result.map!(&NORMALIZER.curry[true])
    end
  end
end
split_params(**params) click to toggle source

NB this method is NOT idempotent!

# File lib/see_as_vee/producers/hashes.rb, line 100
def split_params **params
  [
    INTERNAL_PARAMS.each_with_object({}) do |param, acc|
      acc[param] = params.delete(param)
    end.reject { |_, v| v.nil? },
    params
  ]
end
xlsx(*args, **params) click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 93
def xlsx *args, **params
  constructor_params, params = split_params(**params)
  _, result = Hashes.new(*args, **constructor_params).to_sheet.produce csv: false, xlsx: true, **params
  result
end

Public Instance Methods

add(*args) click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 18
def add *args
  @keys, @joined = [nil] * 2
  @hashes += degroup(args.flatten.select(&Hash.method(:===)), @params[:ungroup], @params[:delimiter] || ',')
  normalize!(false)
end
humanize() click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 33
def humanize
  @hashes.map(&HUMANIZER)
end
humanize!() click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 37
def humanize!
  @hashes.map!(&HUMANIZER)
  self
end
join() click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 42
def join
  return @joined if @joined

  @joined = @hashes.map { |hash| keys.zip([nil] * keys.size).to_h.merge hash }
end
keys() click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 48
def keys
  @keys ||= @hashes.map(&:keys).reduce(&:|)
end
normalize(symbol = true) click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 24
def normalize symbol = true # to_s otherwise
  @hashes.map(&NORMALIZER.curry[symbol])
end
normalize!(symbol = true) click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 28
def normalize! symbol = true # to_s otherwise
  @hashes.map!(&NORMALIZER.curry[symbol])
  self
end
to_sheet() click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 52
def to_sheet
  SeeAsVee::Sheet.new(humanize!.join.map(&:values).unshift(keys))
end

Private Instance Methods

degroup(hashes, columns, delimiter) click to toggle source
# File lib/see_as_vee/producers/hashes.rb, line 58
def degroup hashes, columns, delimiter
  return hashes if (columns = [*columns]).empty?

  hashes.tap do |hs|
    hs.each do |hash|
      columns.each do |column|
        case c = hash.delete(column)
        when Array then c
        when String then c.split(delimiter)
        else [c.inspect]
        end.each.with_index(1) do |value, idx|
          hash["#{column}_#{idx}"] = value
        end
      end
    end
  end
end