Module: Parliament::Utils::Helpers::GroupingHelper
- Included in:
- BusinessItemGroupingHelper, RoleGroupingHelper
- Defined in:
- lib/parliament/utils/helpers/grouping_helper.rb
Instance Method Summary collapse
-
#create_sorted_array(data_hash) ⇒ Array
Places unknown and ungrouped Grom::Nodes into sorted_array without calling any further methods on them For Grom::Nodes that need to be grouped, calls create_grouped_objects method and places result into sorted_array.
-
#group(data, *grouping_methods) ⇒ Array
Groups objects by category where possible.
-
#group_data(data, grouping_methods) ⇒ Hash
Where grouping methods can be called (e.g. #category and #id), this method groups by the result of calling each of those methods in succession on each Grom::Node If a grouping cannot be found, adds ‘UNKNOWN’ as key of the hash.
Instance Method Details
#create_sorted_array(data_hash) ⇒ Array
Places unknown and ungrouped Grom::Nodes into sorted_array without calling any further methods on them For Grom::Nodes that need to be grouped, calls create_grouped_objects method and places result into sorted_array
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/parliament/utils/helpers/grouping_helper.rb', line 34 def create_sorted_array(data_hash) sorted_array = [] data_hash.each do |key, value| # Identify Grom::Nodes that don't need to be grouped (either UNKNOWN or singular values) sorted_array << if key == 'UNKNOWN' || value.length == 1 value else create_grouped_objects(data_hash, key) end end sorted_array.flatten end |
#group(data, *grouping_methods) ⇒ Array
Groups objects by category where possible
10 11 12 13 |
# File 'lib/parliament/utils/helpers/grouping_helper.rb', line 10 def group(data, *grouping_methods) grouped_data = group_data(data, grouping_methods) create_sorted_array(grouped_data) end |
#group_data(data, grouping_methods) ⇒ Hash
Where grouping methods can be called (e.g. #category and #id), this method groups by the result of calling each of those methods in succession on each Grom::Node If a grouping cannot be found, adds ‘UNKNOWN’ as key of the hash
e.g. { 12345678 => […], 23456789 => […], “UNKNOWN” => […] }
23 24 25 26 27 |
# File 'lib/parliament/utils/helpers/grouping_helper.rb', line 23 def group_data(data, grouping_methods) data.group_by do |data_point| grouping_methods.inject(data_point) { |point, method| point.try(method) } || 'UNKNOWN' end end |