Class: Geospatial::Distance

Inherits:
Object
  • Object
show all
Defined in:
lib/geospatial/distance.rb

Overview

This location is specifically relating to a WGS84 coordinate on Earth.

Constant Summary collapse

UNITS =
['m', 'km'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Distance

Distance in meters:



25
26
27
28
# File 'lib/geospatial/distance.rb', line 25

def initialize(value)
	@value = value
	@formatted_value = nil
end

Instance Method Details

#*(other) ⇒ Object



68
69
70
# File 'lib/geospatial/distance.rb', line 68

def * other
	Distance.new(@value * other.to_f)
end

#+(other) ⇒ Object



60
61
62
# File 'lib/geospatial/distance.rb', line 60

def + other
	Distance.new(@value + other.to_f)
end

#-(other) ⇒ Object



64
65
66
# File 'lib/geospatial/distance.rb', line 64

def - other
	Distance.new(@value - other.to_f)
end

#/(other) ⇒ Object



72
73
74
# File 'lib/geospatial/distance.rb', line 72

def / other
	Distance.new(@value / other.to_f)
end

#==(other) ⇒ Object



76
77
78
# File 'lib/geospatial/distance.rb', line 76

def == other
	@value == other.to_f
end

#formatted_valueObject Also known as: to_s



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/geospatial/distance.rb', line 38

def formatted_value
	unless @formatted_value
		scale = 0
		value = @value
		
		while value > 1000 and scale < UNITS.size
			value /= 1000.0
			scale += 1
		end
		
		@formatted_value = sprintf("%0.#{scale}f%s", value, UNITS.fetch(scale))
	end
	
	return @formatted_value
end

#freezeObject



30
31
32
33
34
# File 'lib/geospatial/distance.rb', line 30

def freeze
	formatted_value
	
	super
end

#to_fObject



56
57
58
# File 'lib/geospatial/distance.rb', line 56

def to_f
	@value
end