-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixins.rb
47 lines (36 loc) · 839 Bytes
/
mixins.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Geolocatable
EARTH_MEAN_RADIUS = 6371
attr_accessor :lat, :lng
def distance_from(point)
lat_diff = point.lat - self.lat
lng_diff = point.lng - self.lng
a = Math.sin(lat_diff/2) * Math.sin(lat_diff/2) +
Math.cos(self.lat) * Math.cos(point.lat) * Math.sin(lng_diff/2) * Math.sin(lng_diff/2)
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
d = EARTH_MEAN_RADIUS * c
return d
end
end
class Float
def kms_in_ms
self * 1000
end
end
class User
include Geolocatable
attr_accessor :name, :password
def initialize(options = {})
options.each do |k,v|
self.send("#{k}=",v)
end
end
end
class Location
include Geolocatable
attr_accessor :name
def initialize(options = {})
options.each do |k,v|
self.send("#{k}=",v)
end
end
end