parliament-utils 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dc4a06941ea0dea090c59a94a451411e507b0286
4
- data.tar.gz: 175819e752aa2584803059792de65808d33d3437
3
+ metadata.gz: 57e8942631d85dd3ef9e374d3c69ef92162358ce
4
+ data.tar.gz: 3eebabcc83b7826f713b1d32a1cf765a29b8c25f
5
5
  SHA512:
6
- metadata.gz: f24e4ab9875fa9abe90779da5cbdab723e3097aba1b2bfeabdbd7ad41ecd62e87b566baf5528305d0041af71173f7b647fd96c8f93551941dd252bacac7fcedc
7
- data.tar.gz: e83122d7a8c75636140dfa6a94ff3aaa7ac909f48ee565f10b6735031fe80225c8d355ec8e1fbe7c409b8e1d74477fb8cd07a052c2436d84b2a7e030effc7234
6
+ metadata.gz: ec0fe535a22f52654d41d86518ad7f09fccfaf90cb0c0f37031372faf2c9c1921cc5189e87f22df9d3f8b3dae1237ed410235ce41b8c65032e113ff4d0eb1856
7
+ data.tar.gz: bae9c631bc25f9088b1f7f83e5857129b3df8f82c7a02d2e9f1fbed19adb6a20031875f403874eb28066a02140038c77831b6287d3d30fd265561986fa8c5eb9
@@ -0,0 +1,35 @@
1
+ module Parliament
2
+ module Utils
3
+ module Helpers
4
+ class BusinessItemGroupingHelper
5
+ extend Parliament::Utils::Helpers::GroupingHelper
6
+ # Finds Grom::Nodes that can be grouped and builds BusinessItemGroupingHelper::BusinessItemGroupedObject objects
7
+ #
8
+ # @return date [DateTime] Date of all Grom::Nodes
9
+ # @return nodes [Array] Array of Grom::Nodes
10
+ # @return type [String] Type of all grouped Grom::Nodes
11
+ class BusinessItemGroupedObject
12
+ attr_accessor :date, :nodes, :type
13
+ end
14
+
15
+ # Creates new BusinessItemGroupingHelper::BusinessItemGroupedObject, for each set of Grom::Nodes that have been grouped (nodes) and unknown
16
+ # Each instance of BusinessItemGroupingHelper::BusinessItemGroupedObject is assigned properties of laying_date, nodes and type
17
+ #
18
+ # @param data_hash [Hash] Keys identify grouping, with each value being an array of grouped, ungrouped and unknown Grom::Nodes
19
+ # @param key [String] Key identifies grouping of Grom::Nodes
20
+ # @return [Array] with instances of GroupingHelper::GroupedObject
21
+ def self.create_grouped_objects(data_hash, key)
22
+ grouped = []
23
+
24
+ grouped_object = BusinessItemGroupingHelper::BusinessItemGroupedObject.new
25
+ grouped_object.nodes = data_hash[key].sort_by(&:shortest_distance_of_procedure_steps)
26
+
27
+ # Set properties of the object
28
+ grouped_object.type = grouped_object.nodes.first.type
29
+ grouped_object.date = grouped_object.nodes.first.date
30
+ grouped << grouped_object
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,49 @@
1
+ module Parliament
2
+ module Utils
3
+ module Helpers
4
+ module GroupingHelper
5
+ # Groups objects by category where possible
6
+ #
7
+ # @param data [Array] Array of Grom::Nodes to be grouped
8
+ # @param grouping_methods [Symbol] One or more methods that should be called on Grom::Node to find suitable nodes for grouping
9
+ # @return [Array] Single array of grouped, ungrouped and unknown objects
10
+ def group(data, *grouping_methods)
11
+ grouped_data = group_data(data, grouping_methods)
12
+ create_sorted_array(grouped_data)
13
+ end
14
+
15
+ # 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
16
+ # If a grouping cannot be found, adds 'UNKNOWN' as key of the hash
17
+ #
18
+ # @param data [Array] Array of Grom::Nodes to be grouped
19
+ # @param grouping_methods [Array] One or more methods that should be called on Grom::Node to find suitable nodes for grouping
20
+ # @return [Hash] Keys identify grouping (grouped by category.property), with each value being an array of grouped, ungrouped and unknown Grom::Nodes
21
+ #
22
+ # e.g. { 12345678 => [...], 23456789 => [...], "UNKNOWN" => [...] }
23
+ def group_data(data, grouping_methods)
24
+ data.group_by do |data_point|
25
+ grouping_methods.inject(data_point) { |point, method| point.try(method) } || 'UNKNOWN'
26
+ end
27
+ end
28
+
29
+ # Places unknown and ungrouped Grom::Nodes into sorted_array without calling any further methods on them
30
+ # For Grom::Nodes that need to be grouped, calls create_grouped_objects method and places result into sorted_array
31
+ #
32
+ # @param data_hash [Hash] Keys identify grouping, with each value being an array of grouped, ungrouped and unknown Grom::Nodes
33
+ # @return [Array] Array of unknown and ungrouped Grom::Nodes, and sorted and grouped instances of GroupingHelper::GroupedObject
34
+ def create_sorted_array(data_hash)
35
+ sorted_array = []
36
+ data_hash.each do |key, value|
37
+ # Identify Grom::Nodes that don't need to be grouped (either UNKNOWN or singular values)
38
+ sorted_array << if key == 'UNKNOWN' || value.length == 1
39
+ value
40
+ else
41
+ create_grouped_objects(data_hash, key)
42
+ end
43
+ end
44
+ sorted_array.flatten
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,65 @@
1
+ module Parliament
2
+ module Utils
3
+ module Helpers
4
+ class RoleGroupingHelper
5
+ extend Parliament::Utils::Helpers::GroupingHelper
6
+ # Finds Grom::Nodes that can be grouped and builds GroupingHelper::GroupedObject objects
7
+ #
8
+ # @return start_date [DateTime] Earliest start date
9
+ # @return end_date [DateTime] Latest end date
10
+ # @return nodes [Array] Array of Grom::Nodes
11
+ # @return type [String] Type of all grouped Grom::Nodes
12
+ class RoleGroupedObject
13
+ attr_accessor :start_date, :end_date, :nodes, :type
14
+ end
15
+
16
+ # Creates new GroupingHelper::GroupedObject, for each set of Grom::Nodes that have been grouped (nodes)
17
+ # Each instance of GroupingHelper::GroupedObject is assigned properties of start_date, end_date, nodes and type
18
+ # Once object has been created, calls sort_grouped_nodes_by_date method to sort that object's nodes by date
19
+ #
20
+ # @param data_hash [Hash] Keys identify grouping, with each value being an array of grouped, ungrouped and unknown Grom::Nodes
21
+ # @param key [String] Key identifies grouping of Grom::Nodes
22
+ # @return [Array] with instances of GroupingHelper::GroupedObject
23
+ def self.create_grouped_objects(data_hash, key)
24
+ grouped = []
25
+
26
+ grouped_object = RoleGroupingHelper::RoleGroupedObject.new
27
+ grouped_object.nodes = data_hash[key]
28
+
29
+ start_date = nil
30
+ end_date = nil
31
+ current_nodes = []
32
+
33
+ grouped_object.nodes.each do |node|
34
+ # Find nodes that represents current roles
35
+ current_nodes << node if node.try(:end_date).nil? || node.end_date.nil?
36
+ # Find earliest start date and latest end date
37
+ start_date = node.start_date if start_date.nil? || node.start_date < start_date
38
+ end_date = node.end_date if current_nodes.empty? && (end_date.nil? || node.end_date > end_date)
39
+ end
40
+
41
+ # Set properties of the object
42
+ # TODO: Might need to adjust to allow for multiple roles roles and memberships of a committee
43
+ grouped_object.type = grouped_object.nodes.first.type
44
+ grouped_object.start_date = start_date
45
+ grouped_object.end_date = current_nodes.empty? ? end_date : nil
46
+
47
+ # Add sorted object to the array
48
+ sort_grouped_nodes_by_date(grouped_object, current_nodes)
49
+ grouped << grouped_object
50
+ end
51
+
52
+ # Sorts an array of grouped Grom::Nodes by end date, placing the most current Grom::Node at the start of the array
53
+ #
54
+ # @param grouped_object [Instance object] Instance of GroupingHelper::GroupedObject
55
+ # @param current_node [Grom::Node] Grom::Node with no end date (representing current)
56
+ # @return [Instance object] Returns the same instance, with nodes sorted by end date
57
+ def self.sort_grouped_nodes_by_date(grouped_object, current_nodes)
58
+ grouped_object.nodes -= current_nodes unless current_nodes.empty?
59
+ grouped_object.nodes.sort_by!(&:end_date).reverse!
60
+ grouped_object.nodes.unshift(current_nodes).flatten! unless current_nodes.empty?
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -7,6 +7,9 @@ require_relative './helpers/parliament_helper'
7
7
  require_relative './helpers/postcode_helper'
8
8
  require_relative './helpers/request_helper'
9
9
  require_relative './helpers/v_card_helper'
10
+ require_relative './helpers/grouping_helper'
11
+ require_relative './helpers/role_grouping_helper'
12
+ require_relative './helpers/business_item_grouping_helper'
10
13
 
11
14
  require 'parliament/grom/decorator'
12
15
 
@@ -1,5 +1,5 @@
1
1
  module Parliament
2
2
  module Utils
3
- VERSION = '0.9.0'.freeze
3
+ VERSION = '0.9.1'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parliament-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rebecca Appleyard
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-03 00:00:00.000000000 Z
11
+ date: 2018-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parliament-ruby
@@ -331,13 +331,16 @@ files:
331
331
  - lib/parliament/utils/config/initializers/wrap_parameters.rb
332
332
  - lib/parliament/utils/helpers.rb
333
333
  - lib/parliament/utils/helpers/application_helper.rb
334
+ - lib/parliament/utils/helpers/business_item_grouping_helper.rb
334
335
  - lib/parliament/utils/helpers/filter_helper.rb
335
336
  - lib/parliament/utils/helpers/flag_helper.rb
337
+ - lib/parliament/utils/helpers/grouping_helper.rb
336
338
  - lib/parliament/utils/helpers/houses_helper.rb
337
339
  - lib/parliament/utils/helpers/markdown_helper.rb
338
340
  - lib/parliament/utils/helpers/parliament_helper.rb
339
341
  - lib/parliament/utils/helpers/postcode_helper.rb
340
342
  - lib/parliament/utils/helpers/request_helper.rb
343
+ - lib/parliament/utils/helpers/role_grouping_helper.rb
341
344
  - lib/parliament/utils/helpers/translation_helper.rb
342
345
  - lib/parliament/utils/helpers/v_card_helper.rb
343
346
  - lib/parliament/utils/railtie.rb