Class: SeeAsVee::Producers::Hashes

Inherits:
Object
  • Object
show all
Defined in:
lib/see_as_vee/producers/hashes.rb

Constant Summary collapse

INTERNAL_PARAMS =
%i|ungroup delimiter|.freeze
NORMALIZER =
->(symbol, hash) { hash.map { |k, v| [k.public_send(symbol ? :to_sym : :to_s), v] }.to_h }.freeze
HUMANIZER =
lambda do |hash|
  hash.map do |k, v|
    [k.respond_to?(:humanize) ? k.humanize : k.to_s.sub(/\A_+/, '').tr('_', ' ').downcase, v]
  end.to_h
end.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **params) ⇒ Hashes

Returns a new instance of Hashes.



12
13
14
15
16
# File 'lib/see_as_vee/producers/hashes.rb', line 12

def initialize *args, **params
  @hashes = []
  @params = params
  add(*args)
end

Class Method Details

.csv(*args, **params) ⇒ Object



87
88
89
90
91
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
# 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) ⇒ Object

NB this method is NOT idempotent!



100
101
102
103
104
105
106
107
# 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) ⇒ Object



93
94
95
96
97
# 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

Instance Method Details

#add(*args) ⇒ Object



18
19
20
21
22
# 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

#humanizeObject



33
34
35
# File 'lib/see_as_vee/producers/hashes.rb', line 33

def humanize
  @hashes.map(&HUMANIZER)
end

#humanize!Object



37
38
39
40
# File 'lib/see_as_vee/producers/hashes.rb', line 37

def humanize!
  @hashes.map!(&HUMANIZER)
  self
end

#joinObject



42
43
44
45
46
# 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

#keysObject



48
49
50
# File 'lib/see_as_vee/producers/hashes.rb', line 48

def keys
  @keys ||= @hashes.map(&:keys).reduce(&:|)
end

#normalize(symbol = true) ⇒ Object

to_s otherwise



24
25
26
# 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) ⇒ Object

to_s otherwise



28
29
30
31
# 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_sheetObject



52
53
54
# File 'lib/see_as_vee/producers/hashes.rb', line 52

def to_sheet
  SeeAsVee::Sheet.new(humanize!.join.map(&:values).unshift(keys))
end