Class: OsMapRef::InputProcessor
- Inherits:
-
Object
- Object
- OsMapRef::InputProcessor
- Defined in:
- lib/os_map_ref/input_processor.rb
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
Instance Method Summary collapse
- #determine_easting_and_northing ⇒ Object
- #determine_map_reference_elements ⇒ Object
- #easting_and_northing ⇒ Object
- #easting_northing_params ⇒ Object
-
#easting_northing_pattern ⇒ Object
Matches are: 1: First digits which may be followed with a decimal point and more digits 2: The decimal point and trailing digits from first match (if present) 3: Second digits which may be followed with a decimal point and more digits 4: The decimal point and trailing digits from second match (if present) So: “1234.5, 6789.0” –> 1: “1234.5”, 2: “.5”, 3: “6789.0”, 4: “.0” “1234 6789” –> 1: “1234”, 2: nil, 3: “6789”, 4: nil.
- #extended_length ⇒ Object
- #grid_letters ⇒ Object
-
#initialize(input) ⇒ InputProcessor
constructor
A new instance of InputProcessor.
- #map_reference_easting ⇒ Object
- #map_reference_elements ⇒ Object
- #map_reference_northing ⇒ Object
- #map_reference_params ⇒ Object
- #map_reference_pattern ⇒ Object
- #normal_length ⇒ Object
- #northing_longer_than_easting? ⇒ Boolean
- #padded_map_reference_easting ⇒ Object
- #padded_map_reference_northing ⇒ Object
- #padding ⇒ Object
- #params ⇒ Object
- #processed_map_reference ⇒ Object
Constructor Details
#initialize(input) ⇒ InputProcessor
Returns a new instance of InputProcessor.
7 8 9 |
# File 'lib/os_map_ref/input_processor.rb', line 7 def initialize(input) @input = input end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
5 6 7 |
# File 'lib/os_map_ref/input_processor.rb', line 5 def input @input end |
Instance Method Details
#determine_easting_and_northing ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/os_map_ref/input_processor.rb', line 83 def determine_easting_and_northing match = easting_northing_pattern.match input easting = match[1] northing = match[3] length = northing.length > easting.length ? extended_length : normal_length [ easting.ljust(normal_length, padding), northing.ljust(length, padding) ] end |
#determine_map_reference_elements ⇒ Object
45 46 47 48 |
# File 'lib/os_map_ref/input_processor.rb', line 45 def determine_map_reference_elements match = map_reference_pattern.match input (1..3).collect { |n| match[n] } end |
#easting_and_northing ⇒ Object
79 80 81 |
# File 'lib/os_map_ref/input_processor.rb', line 79 def easting_and_northing @easting_and_northing ||= determine_easting_and_northing end |
#easting_northing_params ⇒ Object
26 27 28 29 30 31 |
# File 'lib/os_map_ref/input_processor.rb', line 26 def easting_northing_params { easting: easting_and_northing[0], northing: easting_and_northing[1] } end |
#easting_northing_pattern ⇒ Object
Matches are: 1: First digits which may be followed with a decimal point and more digits 2: The decimal point and trailing digits from first match (if present) 3: Second digits which may be followed with a decimal point and more digits 4: The decimal point and trailing digits from second match (if present) So: “1234.5, 6789.0” –> 1: “1234.5”, 2: “.5”, 3: “6789.0”, 4: “.0” “1234 6789” –> 1: “1234”, 2: nil, 3: “6789”, 4: nil
114 115 116 117 118 119 120 |
# File 'lib/os_map_ref/input_processor.rb', line 114 def easting_northing_pattern eastings = /\d{3,6}/ # 3 to 6 digits northings = /\d{3,7}/ # 3 to 7 digits decimals = /\.\d+/ # decimal point and trailing digits separator = /[\,\s]+/ # commas or spaces /(#{eastings}(#{decimals})?)#{separator}(#{northings}(#{decimals})?)/ end |
#extended_length ⇒ Object
94 95 96 |
# File 'lib/os_map_ref/input_processor.rb', line 94 def extended_length normal_length + 1 end |
#grid_letters ⇒ Object
50 51 52 |
# File 'lib/os_map_ref/input_processor.rb', line 50 def grid_letters map_reference_elements[0] end |
#map_reference_easting ⇒ Object
54 55 56 |
# File 'lib/os_map_ref/input_processor.rb', line 54 def map_reference_easting map_reference_elements[1] end |
#map_reference_elements ⇒ Object
41 42 43 |
# File 'lib/os_map_ref/input_processor.rb', line 41 def map_reference_elements @map_reference_elements ||= determine_map_reference_elements end |
#map_reference_northing ⇒ Object
58 59 60 |
# File 'lib/os_map_ref/input_processor.rb', line 58 def map_reference_northing map_reference_elements[2] end |
#map_reference_params ⇒ Object
22 23 24 |
# File 'lib/os_map_ref/input_processor.rb', line 22 def map_reference_params { map_reference: processed_map_reference } end |
#map_reference_pattern ⇒ Object
75 76 77 |
# File 'lib/os_map_ref/input_processor.rb', line 75 def map_reference_pattern /([a-zA-Z]{2})\s*(\d{3,5})\s*(\d{3,6})/ end |
#normal_length ⇒ Object
98 99 100 |
# File 'lib/os_map_ref/input_processor.rb', line 98 def normal_length 6 end |
#northing_longer_than_easting? ⇒ Boolean
71 72 73 |
# File 'lib/os_map_ref/input_processor.rb', line 71 def northing_longer_than_easting? map_reference_northing.length > map_reference_easting.length end |
#padded_map_reference_easting ⇒ Object
62 63 64 |
# File 'lib/os_map_ref/input_processor.rb', line 62 def padded_map_reference_easting map_reference_easting.ljust((normal_length - 1), padding) end |
#padded_map_reference_northing ⇒ Object
66 67 68 69 |
# File 'lib/os_map_ref/input_processor.rb', line 66 def padded_map_reference_northing target_length = northing_longer_than_easting? ? extended_length : normal_length map_reference_northing.ljust((target_length - 1), padding) end |
#padding ⇒ Object
102 103 104 |
# File 'lib/os_map_ref/input_processor.rb', line 102 def padding "0" end |
#params ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/os_map_ref/input_processor.rb', line 11 def params case input when map_reference_pattern map_reference_params when easting_northing_pattern easting_northing_params else raise OsMapRef::Error, "Unable to process input #{input}" end end |
#processed_map_reference ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/os_map_ref/input_processor.rb', line 33 def processed_map_reference [ grid_letters.upcase, padded_map_reference_easting, padded_map_reference_northing ].join(" ") end |