Class: WrfLibrary::Location::LocationMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/wrf_library/data/location_mapping.rb

Overview

TODO:

this class is only used in tests at the moment

Simple data class to store a hash mapping from a location key to a location name The csv formatted input file need to contain key;location pairs for each entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location_file) ⇒ LocationMapping

initialization

Parameters:

  • location_file (String)

    the filepath to the location file



16
17
18
19
# File 'lib/wrf_library/data/location_mapping.rb', line 16

def initialize(location_file)
  @locations = Hash.new()
  create_mapping(location_file)
end

Instance Attribute Details

#locationsHash (readonly, private)

Returns the mapping of location key and location name.

Returns:

  • (Hash)

    the mapping of location key and location name



30
31
32
# File 'lib/wrf_library/data/location_mapping.rb', line 30

def locations
  @locations
end

Instance Method Details

#create_mapping(location_file) ⇒ Object (private)

method to read the mapping file and generate the hash mapping for the locations

Parameters:

  • location_file (String)

    the filepath to the location file



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wrf_library/data/location_mapping.rb', line 34

def create_mapping(location_file)
  data = RubyUtils::FileReader.new(location_file, ";").data
  data.each { |line|
    if (line.length == 2 && line[0] != nil && line[1] != nil)
      @locations[line[0]] = line[1]
    else
      raise ArgumentError, "Error: Location file contains invalid location pair".red
    end
  }
  nil
end

#get_file_to_location(location_shortcut) ⇒ String

method to return the location name to a given key

Returns:

  • (String)

    returns the location name if found, else nil



23
24
25
# File 'lib/wrf_library/data/location_mapping.rb', line 23

def get_file_to_location(location_shortcut)
  @locations.fetch(location_shortcut, nil)
end