Module: HoneyFormat::Helpers
- Defined in:
- lib/honey_format/helpers/helpers.rb
Class Method Summary collapse
-
.count_occurences(array) ⇒ Hash
Returns hash with key => occurrences_count.
-
.duplicated_items(array) ⇒ Array<Object>
Returns array with duplicated objects.
-
.key_count_to_deduplicated_array(data) ⇒ Array<Symbol>
Converts a Hash with key => count to a deduplicated array.
Class Method Details
.count_occurences(array) ⇒ Hash
Returns hash with 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
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.
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 |