Module: Locatable::Helpers

Defined in:
lib/locatable/helpers.rb

Class Method Summary collapse

Class Method Details

.convert_to_meters(distance, units) ⇒ Object



27
28
29
30
31
32
# File 'lib/locatable/helpers.rb', line 27

def convert_to_meters(distance, units)
  distance = parse_float(distance)
  return nil if distance.nil?

  distance * meters_per_units(units)
end

.extract_lat_lng(origin) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/locatable/helpers.rb', line 4

def extract_lat_lng(origin)
  lat, lng = case origin
  when Array
    [origin[0], origin[1]]
  when Hash
    [origin[:lat] || origin["lat"], origin[:lng] || origin["lng"]]
  else
    [origin.try(:lat), origin.try(:lng)]
  end

  [parse_float(lat), parse_float(lng)]
end

.meters_per_units(units) ⇒ Object



23
24
25
# File 'lib/locatable/helpers.rb', line 23

def meters_per_units(units)
  Locatable::METERS_PER_UNIT.fetch(Locatable.normalize_units(units))
end

.parse_float(value) ⇒ Object



17
18
19
20
21
# File 'lib/locatable/helpers.rb', line 17

def parse_float(value)
  Float(value)
rescue ArgumentError, TypeError
  nil
end