Class: Factbase::Flatten

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/flatten.rb

Overview

Make maps suitable for printing.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(maps, sorter = '_id') ⇒ Flatten

Constructor.



32
33
34
35
# File 'lib/factbase/flatten.rb', line 32

def initialize(maps, sorter = '_id')
  @maps = maps
  @sorter = sorter
end

Instance Method Details

#itArray<HashMap>

Improve the maps.

Returns:

  • (Array<HashMap>)

    The hashmaps, but improved



39
40
41
42
43
44
# File 'lib/factbase/flatten.rb', line 39

def it
  @maps
    .sort_by { |m| m[@sorter] || [] }
    .map { |m| m.sort.to_h }
    .map { |m| m.transform_values { |v| v.size == 1 ? v[0] : v } }
end