Permalink
Browse files

core ext specs ok

  • Loading branch information...
1 parent 73ea1cc commit b701521b604f490f9833236d6755d27b7cdeaf03 @kristianmandrup committed Feb 6, 2011
View
@@ -22,7 +22,7 @@ Jeweler::Tasks.new do |gem|
# Include your dependencies below. Runtime dependencies are required when using your gem,
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
- gem.add_runtime_dependency 'geo-distance', '>= 0.1.0'
+ # gem.add_runtime_dependency 'geo-distance', '>= 0.1.0'
gem.add_runtime_dependency 'httparty', '>= 0.6.0'
# gem.add_development_dependency 'rspec', '> 1.2.3'
@@ -1,7 +1,5 @@
-require 'geo-distance'
require 'geo_magic/point'
-require 'geo_magic/util'
-require 'geo_magic/location'
+require 'geo_magic/distance'
module GeoMagic
module Calculate #:nodoc:
View
@@ -1,23 +1,47 @@
-class Float
- def round_to(x)
- (self * 10**x).round.to_f / 10**x
- end
+require 'geo_magic/distance'
- def ceil_to(x)
- (self * 10**x).ceil.to_f / 10**x
+module GeoMagic
+ module UnitExt
+ ::GeoMagic::Distance::Unit.units.each do |unit|
+ class_eval %{
+ def #{unit}
+ GeoMagic::Distance.new(self, :#{unit})
+ end
+ }
+ end
end
+end
+
+module GeoMagic
+ module MathExt
+ def round_to(x)
+ (self * 10**x).round.to_f / 10**x
+ end
+
+ def ceil_to(x)
+ (self * 10**x).ceil.to_f / 10**x
+ end
+
+ def floor_to(x)
+ (self * 10**x).floor.to_f / 10**x
+ end
- def floor_to(x)
- (self * 10**x).floor.to_f / 10**x
+ def rpd
+ self * GeoMagic::Distance.radians_per_degree
+ end
+ alias_method :to_radians, :rpd
end
-
- def rpd
- self * GeoDistance.radians_per_degree
- end
- alias_method :to_radians, :rpd
+end
+
+class Fixnum
+ include GeoMagic::MathExt
+ include GeoMagic::UnitExt
end
-require 'geo_magic/distance'
+class Float
+ include GeoMagic::MathExt
+ include GeoMagic::UnitExt
+end
class Symbol
def radians_ratio
@@ -32,16 +56,6 @@ def radians_ratio
end
-class Integer
- ::GeoMagic::Distance::Unit.units.each do |unit|
- class_eval %{
- def #{unit}
- GeoDistance::Distance.new(self, :#{unit})
- end
- }
- end
-end
-
class Array
def are_points?
each do |p|
@@ -13,7 +13,7 @@ def initialize distance, unit = nil
@distance = distance
return if !unit
- raise ArgumentError, "Invalid unit: #{unit} - must be one of #{GeoDistance.units}" if !GeoDistance.units.include?(unit.to_sym)
+ raise ArgumentError, "Invalid unit: #{unit} - must be one of #{GeoMagic::Distance.units}" if !GeoMagic::Distance.units.include?(unit.to_sym)
@unit = unit.to_sym
end
@@ -16,7 +16,7 @@ def units
end
def radians_ratio unit
- GeoDistance.radians_per_degree * earth_radius[unit]
+ GeoMagic::Distance.radians_per_degree * earth_radius[unit]
end
def default_algorithm= name
@@ -27,11 +27,11 @@ def default_algorithm= name
def distance( lat1, lon1, lat2, lon2)
klass = case default_algorithm
when :haversine
- GeoDistance::Haversine
+ GeoMagic::Distance::Haversine
when :spherical
- GeoDistance::Spherical
+ GeoMagic::Distance::Spherical
when :vincenty
- GeoDistance::Vincenty
+ GeoMagic::Distance::Vincenty
else
raise ArgumentError, "Not a valid algorithm. Must be one of: #{algorithms}"
end
@@ -23,7 +23,7 @@ def self.distance( lat1, lon1, lat2, lon2)
Math.cos(to_longitude - from_longitude)
) #* EARTH_RADIUS[units.to_sym]
- GeoDistance::Distance.new c
+ GeoMagic::Distance.new c
end
end
end
@@ -70,7 +70,7 @@ def self.distance(lat1, lon1, lat2, lon2)
c = earth_minor_axis_radius * a * (sigma-delta_sigma)
- GeoDistance::Distance.new(c / unkilometer)
+ GeoMagic::Distance.new(c / unkilometer)
end
private
@@ -1,4 +1,4 @@
-require 'geo_magic/map_point'
+require 'geo_magic/point/map_point'
module GeoMagic
class Location < MapPoint
@@ -68,9 +68,9 @@ def get_proc precision
when :low
Proc.new {|point_a, point_b| ::GeoMagic::Calculate.plane_distance point_a, point_b }
when :normal
- Proc.new {|point_a, point_b| ::GeoDistance::Haversine.point_distance point_a, point_b }
+ Proc.new {|point_a, point_b| ::GeoMagic::Distance::Haversine.point_distance point_a, point_b }
when :high
- Proc.new {|point_a, point_b| ::GeoDistance::Vincenty.point_distance point_a, point_b }
+ Proc.new {|point_a, point_b| ::GeoMagic::Distance::Vincenty.point_distance point_a, point_b }
else
raise ArgumentError, "Unknown precision: #{precision}"
end
@@ -21,7 +21,7 @@ def random_point_within
end
def random_points_within number
- conversion = GeoDistance.radians_ratio(distance.unit)
+ conversion = GeoMagic::Distance.radians_ratio(distance.unit)
max_radius_rad = distance.distance
range = normalize max_radius_rad
@@ -9,7 +9,7 @@ def initialize center, distance
# Factory
def random_point_within
- conversion = GeoDistance.radians_ratio(distance.unit)
+ conversion = GeoMagic::Distance.radians_ratio(distance.unit)
max_radius_rad = dist.distance
range = (max_radius_rad * normalize).to_i
@@ -16,7 +16,7 @@
end
it "Changing default distance algorithm to vincenty" do
- GeoDistance.default_algorithm = :vincenty
+ GeoMagic::Distance.default_algorithm = :vincenty
dist = GeoMagic::Calculate.distance [@long1, @lat1], [@long2, @lat2]
puts dist
end
@@ -1,15 +1,31 @@
require 'spec_helper'
-describe Symbol do
- it "should return radians ratio" do
- km_radians = :km.radians_ratio
- puts km_radians
+describe 'Core extensions' do
+ describe Symbol do
+ it "should return radians ratio" do
+ km_radians = :km.radians_ratio
+ km_radians.should > 111
+ end
end
-end
-describe String do
- it "should return radians ratio" do
- km_radians = 'km'.radians_ratio
- puts km_radians
+ describe String do
+ it "should return radians ratio" do
+ km_radians = 'km'.radians_ratio
+ km_radians.should > 111
+ end
+ end
+
+ describe Fixnum do
+ it "should return distance" do
+ km_dist = 5.km
+ km_dist[:km].number.should > 0
+ end
+ end
+
+ describe Float do
+ it "should return distance" do
+ km_dist = 5.2.km
+ km_dist[:km].number.should > 0
+ end
end
end

0 comments on commit b701521

Please sign in to comment.