Module: HoneyFormat::Helpers

Defined in:
lib/honey_format/helpers/helpers.rb

Class Method Summary collapse

Class Method Details

.count_occurences(array) ⇒ Hash

Returns hash with key => occurrences_count

Parameters:

  • the (Array<Object>)

    array to count occurrences in

Returns:

  • (Hash)

    key => occurrences_count



29
30
31
32
33
# File 'lib/honey_format/helpers/helpers.rb', line 29

def self.count_occurences(array)
  occurrences = Hash.new(0)
  array.each { |column| occurrences[column] += 1 }
  occurrences
end

.duplicated_items(array) ⇒ Array<Object>

Returns array with duplicated objects

Parameters:

  • the (Array<Object>)

    array to find duplicates in

Returns:

  • (Array<Object>)

    array of duplicated objects



38
39
40
# File 'lib/honey_format/helpers/helpers.rb', line 38

def self.duplicated_items(array)
  array.select { |col| array.count(col) > 1 }.uniq
end

.key_count_to_deduplicated_array(data) ⇒ Array<Symbol>

Converts a Hash with key => count to a deduplicated array.

Examples:

Helpers.key_count_to_deduplicated_array({ a: 2, b: 1, c: 0})
# => [:a, :a1, :b]

Parameters:

  • data (Hash)

    with key => count

Returns:

  • (Array<Symbol>)

    an array of symbols



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/honey_format/helpers/helpers.rb', line 11

def self.key_count_to_deduplicated_array(data)
  array = []
  count_occurences(data).each do |key, value|
    next array << key if value == 1

    values = Array.new(value) { |i| i }.map do |index|
      next key if index.zero?

      :"#{key}#{index}"
    end
    array.concat(values)
  end
  array
end