Module: Barometer::Query::Converter

Defined in:
lib/barometer/query/converter.rb,
lib/barometer/query/converters/to_woe_id.rb,
lib/barometer/query/converters/to_geocode.rb,
lib/barometer/query/converters/to_weather_id.rb,
lib/barometer/query/converters/from_weather_id_to_geocode.rb,
lib/barometer/query/converters/from_geocode_to_coordinates.rb,
lib/barometer/query/converters/from_short_zipcode_to_zipcode.rb,
lib/barometer/query/converters/from_woe_id_or_ipv4_to_geocode.rb,
lib/barometer/query/converters/from_coordinates_to_noaa_station_id.rb

Defined Under Namespace

Classes: FromCoordinatesToNoaaStationId, FromGeocodeToCoordinates, FromShortZipcodeToZipcode, FromWeatherIdToGeocode, FromWoeIdOrIpv4ToGeocode, ToGeocode, ToWeatherId, ToWoeId

Constant Summary collapse

@@converters =
{}

Class Method Summary collapse

Class Method Details

._find_direct_converter(from_format, to_formats) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/barometer/query/converter.rb', line 35

def self._find_direct_converter(from_format, to_formats)
  converter = nil
  to_formats.each do |to_format|
    converter = find(from_format, to_format)
    break if converter
  end
  [converter].compact
end

._find_indirect_converters(from_format, to_formats) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/barometer/query/converter.rb', line 44

def self._find_indirect_converters(from_format, to_formats)
  geocode_converter = find(from_format, :geocode)
  converter = nil
  to_formats.each do |to_format|
    converter = find(:geocode, to_format)
    break if converter
  end
  geocode_converter && converter ? [geocode_converter, converter] : []
end

.convertersObject



10
11
12
# File 'lib/barometer/query/converter.rb', line 10

def self.converters
  @@converters
end

.converters=(converters) ⇒ Object



6
7
8
# File 'lib/barometer/query/converter.rb', line 6

def self.converters=(converters)
  @@converters = converters
end

.find(from_format, to_format) ⇒ Object



23
24
25
26
# File 'lib/barometer/query/converter.rb', line 23

def self.find(from_format, to_format)
  converter = @@converters.fetch(to_format, {}).fetch(from_format, nil)
  {to_format => converter} if converter
end

.find_all(from_format, to_formats) ⇒ Object



28
29
30
31
32
33
# File 'lib/barometer/query/converter.rb', line 28

def self.find_all(from_format, to_formats)
  converters = _find_direct_converter(from_format, Array(to_formats))
  return converters unless converters.empty?

  _find_indirect_converters(from_format, Array(to_formats))
end

.register(to_format, converter_klass) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/barometer/query/converter.rb', line 14

def self.register(to_format, converter_klass)
  # return unless converter_klass.respond_to?(:from)

  @@converters[to_format] ||= {}
  converter_klass.from.each do |from_format|
    @@converters[to_format][from_format] = converter_klass
  end
end