Skip to content
Browse files

cool improvements

  • Loading branch information...
1 parent b87064c commit 42f6ff318abe5cd663443ceda93c48742d19ad26 @kristianmandrup committed Feb 9, 2011
View
21 lib/geo_magic/distance.rb
@@ -6,7 +6,7 @@
module GeoMagic
class Distance
- attr_reader :distance, :unit
+ attr_accessor :distance, :unit
extend ClassMethods
@@ -19,22 +19,21 @@ def initialize distance, unit = nil
end
# select all points within radius
- def select_within points, center
-
+ def select_within points, center
end
# reject all points within radius
def reject_within points, center
end
+ def * arg
+ multiply arg
+ end
+
def multiply arg
- case arg
- when Fixnum
- self.distance *= arg
- else
- raise ArgumentError, "Argument must be a Fixnum" if !arg.kind_of?
- end
+ check_numeric! arg
+ self.distance *= arg
end
def radius center, type = :circular
@@ -77,6 +76,10 @@ def to_s
end
protected
+
+ def check_numeric! arg
+ raise ArgumentError, "Argument must be Numeric" if !arg.is_a? Numeric
+ end
# delta between the two points in miles
GeoMagic::Distance.units.each do |unit|
View
33 lib/geo_magic/distance/vector.rb
@@ -12,26 +12,30 @@ def initialize lat_distance, long_distance
@long_distance = long_distance
end
+ def * arg
+ multiply arg
+ end
+
def multiply arg
factors = case arg
- when Fixnum
+ when Numeric
[arg, arg]
when Hash
[factor(arg, lat_symbols), factor(arg, long_symbols)]
else
- raise ArgumentError, "Argument must be a Fixnum or a Hash specifying factor to multiply latitude and/or longitude with" if !arg.kind_of?
+ raise ArgumentError, "Argument must be a Fixnum or a Hash specifying factor to multiply latitude and/or longitude with, was #{arg.class}"
end
multiply_lat factors.first
multiply_long factors.last
end
def multiply_lat arg
- raise ArgumentError, "Argument must be a Fixnum" if !arg.kind_of? Fixnum
+ check_numeric! arg
self.lat_distance *= arg
end
def multiply_long arg
- raise ArgumentError, "Argument must be a Fixnum" if !arg.kind_of? Fixnum
+ check_numeric! arg
self.long_distance *= arg
end
@@ -45,6 +49,10 @@ def to_s
protected
+ def check_numeric! arg
+ raise ArgumentError, "Argument must be Numeric" if !arg.is_a? Numeric
+ end
+
include GeoMagic::GeoSymbols
class << self
@@ -55,9 +63,7 @@ class << self
def create_from *args
case args.size
when 1
- arg = args.first
- return arg if arg.kind_of? self.class
- new *extract_from_hash(arg)
+ extract_from_single args.first
when 2
new *args
else
@@ -67,6 +73,17 @@ def create_from *args
protected
+ def extract_from_single arg
+ case arg
+ when GeoMagic::Distance::Vector
+ arg
+ when GeoMagic::Distance
+ new arg, arg
+ else
+ new *extract_from_hash(arg)
+ end
+ end
+
def extract_from_hash hash
raise ArgumentError, "single argument must be a hash" if !hash.kind_of? Hash
[lat_dist(hash), long_dist(hash)]
@@ -86,7 +103,7 @@ def long_dist hash
end
def factor hash, symbols
- s = symbols.select {|s| hash[s].kind_of? Fixnum }
+ s = symbols.select {|s| hash[s].is_a? Numeric }
s.empty? ? 1 : hash[s]
end
end
View
22 lib/geo_magic/radius.rb
@@ -20,17 +20,29 @@ def create_#{name} center, distance
end
def double
- raise "Must be implemented by subclass. Should simply double the radius of the shape"
+ multiply 2
end
- def halve
- raise "Must be implemented by subclass. Should simply halve the radius of the shape"
+ def double!
+ multiply! 2
+ end
+
+ def halve!
+ multiply 0.5
+ end
+
+ def halve!
+ multiply! 0.5
end
def multiply arg
raise "Must be implemented by subclass. Should multiply size by a given factor"
end
+ def multiply! arg
+ raise "Must be implemented by subclass. Should multiply size by a given factor"
+ end
+
# select all points within radius
def select_within points
raise "Must be implemented by subclass"
@@ -64,6 +76,10 @@ def center= point
end
protected
+
+ def check_numeric! arg
+ raise ArgumentError, "Argument must be Numeric" if !arg.is_a? Numeric
+ end
include RandomRadiant
end
View
11 lib/geo_magic/radius/circular.rb
@@ -9,8 +9,13 @@ def initialize center, distance
end
def multiply arg
- raise ArgumentError, "Argument must be a Fixnum" if !arg.kind_of? Fixnum
- self.distance *= arg
+ self.clone.multiply! arg
+ end
+
+ def multiply! arg
+ check_numeric! arg
+ self.distance.distance *= arg
+ self
end
def to_s
@@ -50,6 +55,6 @@ def random_points_within number
res << point
res
end
- end
+ end
end
end
View
17 lib/geo_magic/radius/rectangular.rb
@@ -10,8 +10,19 @@ def initialize center, vector_distance
end
def multiply arg
- self.vector_distance.multiply arg
- self
+ self.clone.multiply! arg
+ end
+
+ def multiply! arg
+ case arg
+ when Numeric
+ self.vector_distance.multiply arg
+ self
+ when Hash
+ self.radius_from_factors [factor(arg, [:lat, :latitude]), factor(arg, [:long, :longitude])]
+ else
+ raise ArgumentError, "Argument must be Numeric or a Hash specifying factor to multiply latitude and/or longitude with"
+ end
end
def create_from *args
@@ -55,6 +66,6 @@ def random_point_within
res << point
res
end
- end
+ end
end
end
View
17 lib/geo_magic/radius/square.rb
@@ -8,18 +8,15 @@ def initialize center, distance
@distance = distance
end
- def multiply arg
- case arg
- when Fixnum
- self.distance.multiply arg
- self
- when Hash
- radius_from_factors [factor(arg, [:lat, :latitude]), factor(arg, [:long, :longitude])]
- else
- raise ArgumentError, "Argument must be a Fixnum or a Hash specifying factor to multiply latitude and/or longitude with" if !arg.kind_of?
- end
+ def multiply! arg
+ self.distance.multiply arg
+ self
end
+ def multiply arg
+ self.clone.multiply! arg
+ end
+
# Factory
def random_point_within
conversion = GeoMagic::Distance.radians_ratio(distance.unit)
View
33 spec/geo_magic/radius/circular_spec.rb
@@ -1,16 +1,37 @@
require 'spec_helper'
describe GeoMagic::CircularRadius do
- describe '#new' do
- let(:center) do
- {:lat => 40, :long => 11}
- end
+ let(:center) do
+ {:lat => 40, :long => 11}
+ end
+ let(:distance) { 3.2.km }
- let(:distance) { 3.2 }
-
+ describe '#new' do
it "should create a new circular radius" do
circle = GeoMagic::CircularRadius.new center, distance
puts circle.inspect
end
end
+
+ describe '#double' do
+ let (:circle) do
+ GeoMagic::CircularRadius.new center, distance
+ end
+
+ it "should double the radius distance" do
+ circle.double!
+ puts circle.inspect
+ end
+ end
+
+ describe '#halve' do
+ let (:circle) do
+ GeoMagic::CircularRadius.new center, distance
+ end
+
+ it "should halve the radius distance" do
+ circle.halve!
+ puts circle.inspect
+ end
+ end
end
View
44 spec/geo_magic/radius/rectangular_spec.rb
@@ -1,18 +1,48 @@
require 'spec_helper'
describe GeoMagic::RectangularRadius do
- describe '#new' do
- let(:center) do
- {:lat => 40, :long => 11}
+ let(:center) do
+ {:lat => 40, :long => 11}
+ end
+
+ let(:vector_distance) do
+ {:lat => 1.2.km, :long => 0.4.km}
+ end
+
+ let(:distance) { 2.2.km }
+
+ describe '#new' do
+ it "should create a new rectangular radius" do
+ rect = GeoMagic::RectangularRadius.new center, vector_distance
+ puts rect.inspect
end
- let(:vector_distance) do
- {:lat => 1.2.km, :long => 0.4.km}
+ it "should create a new rectangular radius that is square" do
+ rect = GeoMagic::RectangularRadius.new center, distance
+ puts rect.inspect
+ end
+ end
+
+ describe '#double' do
+ let (:rect) do
+ GeoMagic::RectangularRadius.new center, vector_distance
end
- it "should create a new rectangular radius" do
- rect = GeoMagic::RectangularRadius.new center, vector_distance
+ it "should double the radius distance" do
+ rect.double!
+ puts rect.inspect
+ end
+ end
+
+ describe '#halve' do
+ let (:rect) do
+ GeoMagic::RectangularRadius.new center, distance
+ end
+
+ it "should halve the radius distance" do
+ rect.halve!
puts rect.inspect
end
end
+
end

0 comments on commit 42f6ff3

Please sign in to comment.
Something went wrong with that request. Please try again.