/procedures/meta/comments

Module to initialise, create and update a hash of a hash of route attributes keyed off the ID of a route.

 2 module PARSE_ROUTE_HASH

Method to initialise a hash of a hash of route attributes keyed off the ID of a route.

We use this to store values that are generated from parsing and are not on the route model.

 6   def initialise_route_hash( work_package )

We get all routes which appear in the procedure the work package is subject to, together with the name and type of the source step of each route and the name and type of the target step of each route.

 9     routes = work_package.parliamentary_procedure.routes_with_steps

We record the number of routes in the procedure as an instance variable, so we can use it to report progress.

 12     @route_count = routes.size

If the procedure has no routes ...

 15     if routes.empty?

... we write to the log, explaining that the work package cannot be parsed.

 18       @parse_log << "Work package #{work_package.id} is subject to the #{work_package.parliamentary_procedure.name} procedure. This procedure has no routes and therefore the work package cannot be parsed."

Otherwise, if the procedure does have routes ...

 21     else

... we create a hash for the routes and their attributes.

This is created as an instance variable because we want to write to it as we parse and report on it later.

 25       @routes = {}

... we loop through the routes and ...

 28       routes.each do |route|

... for each route create a hash of values we want to store for that route.

 31         route_hash = create_and_add_route_hash(
 32           route, # We pass in the route which we'll use as the key of the hash.
 33           'NULL', # We pass in the current attribute to capture if the route is currently active. This is 'NULL' until the route is parsed.
 34           'UNPARSED', # We pass in the status attribute of the route. This is 'UNPARSED' until the route is parsed.
 35           false, # We pass in the parsed attribute of the route. This is false until the route is successfully parsed. A route may have many parse passes before being successfully parsed.
 36           0, # We pass in the actualised has happened count of the route. This is 0 until we parse either ...

... a route whose source is a business step, then we set this to the count of business items, with dates in the past or of today, actualising that step ...

... or a route whose source is a SUM step or an INCREMENT step, then we set this to the value output by the step.

 39           0, # We pass in the parse pass count attribute of this route. This is 0 until parsed.
 40         )
 41       end
 42     end
 43   end

Method to create a hash of attributes for a route and to add the attributes hash to the containing routes hash.

 46   def create_and_add_route_hash( route, current, status, parsed, actualisation_count, parse_pass_count )

We create a hash of route attributes.

 49     route_hash = {
 50       :current => current,
 51       :status => status,
 52       :parsed => parsed,
 53       :actualisation_count => actualisation_count,
 54       :parse_pass_count => parse_pass_count,
 55       :route => route
 56     }

We add the hash to the routes hash, keyed off the ID of the route.

 59     @routes[route.id] = route_hash
 60   end

Method to update the hash of attributes for a route within the containing route hash.

 63   def update_route_hash( route_id, current, status, parsed, actualisation_count, parse_pass_count )

We check if this method has been passed a value for an attribute.

Where the method has been passed nil as an attribute value, we use the attribute value as it exists in the hash.

 67     current = current || @routes[route_id][:current]
 68     status = status || @routes[route_id][:status]
 69     parsed = parsed || @routes[route_id][:parsed]
 70     actualisation_count = actualisation_count || @routes[route_id][:actualisation_count]
 71     parse_pass_count = parse_pass_count || @routes[route_id][:parse_pass_count]

We create a hash of attributes for the route with any revised values.

 74     route_hash = {
 75       :current => current,
 76       :status => status,
 77       :parsed => parsed,
 78       :actualisation_count => actualisation_count,
 79       :parse_pass_count => parse_pass_count,
 80       :route => @routes[route_id][:route]
 81     }

We push this back into the hash of routes, keyed off the ID of the route.

 84     @routes[route_id] = route_hash
 85   end

A set of methods to get attributes of a route.

We call all methods with the ID of the route.

Method to get the hash of route attributes from the routes hash.

 91   def route_hash( route_id )
 92     @routes[route_id]
 93   end

Method to get the current attribute of the route hash.

 96   def route_current_attribute( route_id )
 97     route_hash( route_id )[:current]
 98   end

Method to get the status attribute of the route hash.

 101   def route_status_attribute( route_id )
 102     route_hash( route_id )[:status]
 103   end

Method to check if the untraversable attribute of the route is set to untraversable.

 106   def route_is_untraversable?( route_id )

We assume the route is traversable.

 109     untraversable = false

We set the value of untraversable to true if the routes hash has a status of ‘UNTRAVERSABLE’ for the route with this ID.

 112     untraversable = true if route_status_attribute( route_id ) == 'UNTRAVERSABLE'

We return the boolean.

 115     untraversable
 116   end

Method to get the parsed attribute of the route hash.

 119   def route_parsed_attribute( route_id )
 120     route_hash( route_id )[:parsed]
 121   end

Method to get the parse pass count attribute of the route hash.

 124   def route_parse_pass_count_attribute( route_id )
 125     route_hash( route_id )[:parse_pass_count]
 126   end

Method to get the route object from the routes hash.

 129   def route_object( route_id )
 130     route_hash( route_id )[:route]
 131   end

Method to check if a route is current.

 134   def route_is_current?( route_id )

A route may have start and end dates.

We get the start and end dates from the route object.

 138     start_date = route_object( route_id ).start_date
 139     end_date = route_object( route_id ).end_date

If the route has a start date and the start date is in the future, being after today ...

 142     if start_date and start_date > Date.today

.. we set current to false because the road is not yet open.

 145       current = false

Otherwise, if the route has an end date and the end date is in the past, being earlier than today ...

 148     elsif end_date and end_date < Date.today

... we set current to false because the road closed in the past.

 151       current = false

Otherwise, if the route has no start date or a start date in the past or no end date or an end date in the future ...

 154     else

... we set current to true because the road is open.

 157       current = true
 158     end

We return the value we've set for the route's currency.

 161     current
 162   end

Method to get the ID of the source step of a route.

 165   def route_source_step_id( route_id )

We get the from_step_id of the route in the routes hash with this ID.

 168     route_object( route_id ).from_step_id
 169   end

Method to get the name of the source step of a route.

 172   def route_source_step_name( route_id )

We get the name of the source step of the route in the routes hash with this ID.

 175     route_object( route_id ).source_step_name
 176   end

Method to get the name of the type of the source step of a route.

 179   def route_source_step_type( route_id )

We get the name of the source step of the route in the routes hash with this ID.

 182     route_object( route_id ).source_step_type
 183   end

Method to get the source step object of a route.

 186   def route_source_step( route_id )

We get the step object with the ID of the source step of the route.

 189     @steps[route_source_step_id( route_id )]
 190   end

Method to get the ID of the target step of a route.

 193   def route_target_step_id( route_id )

We get the to_step_id of the route in the routes hash with this ID.

 196     route_object( route_id ).to_step_id
 197   end

Method to get the name of the target step of a route.

 200   def route_target_step_name( route_id )

We get the name of the target step of the route in the routes hash with this ID.

 203     route_object( route_id ).target_step_name
 204   end

Method to get the name of the type of the target step of a route.

 207   def route_target_step_type( route_id )

We get the name of the target step of the route in the routes hash with this ID.

 210     route_object( route_id ).target_step_type
 211   end

Method to get the target step object of a route.

 214   def route_target_step( route_id )

We get the step object with the ID of the target step of the route.

 217     @steps[route_target_step_id( route_id )]
 218   end

Method to check if the target step of a route is actualised by a business item, regardless of the date of that business item.

 221   def route_target_step_is_actualised?( route_id )

We check if the target step of the route has been actualised.

 224     step_has_been_actualised?( route_target_step( route_id ) )
 225   end

Method to get the actualised as happened_count from the source step of a route.

 228   def route_source_step_actualised_has_happened_count( route_id )

We get the actualised as happened count from the source step.

 231     step_actualised_as_happened_count( route_source_step_id( route_id ) )
 232   end

Method to get the actualisation count of a route.

 235   def route_actualisation_count( route_id )

We get the actualisation count of the route in the routes hash with this ID.

 238     route_object( route_id ).actualisation_count
 239   end
 240 end