Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 7f1e1ac93474f3785f1c10739c1084b4a91195fd @netskin-ci netskin-ci committed Aug 4, 2010
Showing with 99 additions and 0 deletions.
  1. +66 −0 lib/sequel_mappable.rb
  2. +7 −0 lib/sequel_mappable/version.rb
  3. +26 −0 sequel_mappable.gemspec
@@ -0,0 +1,66 @@
+module Sequel
+ module Plugins
+ module Mappable
+
+ def self.apply(model, opts={}, &block)
+ model.send(:include, Geokit::Geocoders)
+ model.send(:include, Geokit::Mappable)
+ end
+
+ def self.configure(model, opts={}, &block)
+ end
+
+ module ClassMethods
+ def acts_as_mappable
+ true
+ end
+
+ def distance_column_name
+ :distance
+ end
+
+ def lat_column_name
+ :lat
+ end
+
+ def lng_column_name
+ :lng
+ end
+
+ def distance_sql(origin)
+ lat = deg2rad(origin.lat)
+ lng = deg2rad(origin.lng)
+ multiplier = units_sphere_multiplier(:kms)
+ %|
+ (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(lat))*COS(RADIANS(lng))+
+ COS(#{lat})*SIN(#{lng})*COS(RADIANS(lat))*SIN(RADIANS(lng))+
+ SIN(#{lat})*SIN(RADIANS(lat))))*#{multiplier})
+ |
+ end
+ end
+
+ module InstanceMethods
+ end
+
+ module DatasetMethods
+ def bounded_location(origin, within)
+ bounds = Geokit::Bounds.from_point_and_radius(origin, within, :units => :kms)
+ sw, ne = bounds.sw, bounds.ne
+ filter = self
+ if bounds.crosses_meridian?
+ filter = filter.filter{ (:lng < ne.lng) | (:lng > sw.lng) }
+ else
+ filter = filter.filter{ (:lng < ne.lng) & (:lng > sw.lng) }
+ end
+ filter.filter{ (:lat < ne.lat) & (:lat > sw.lat) }
+ end
+
+ def area(origin, within)
+ sql = model.distance_sql(origin)
+ bounded_location(origin, within).filter{ sql.lit <= within }
+ end
+ end
+ end
+ end
+end
+
@@ -0,0 +1,7 @@
+module Sequel
+ module Plugins
+ module Mappable
+ VERSION = '0.0.1'.freeze
+ end
+ end
+end
@@ -0,0 +1,26 @@
+#!/usr/bin/env gem build
+# -*- encoding: utf-8 -*-
+
+require 'date'
+require 'lib/sequel_mappable/version'
+
+Gem::Specification.new do |gem|
+ gem.name = 'sequel_mappable'
+ gem.version = Sequel::Plugins::Mappable::VERSION.dup
+ gem.authors = ['Corin Langosch']
+ gem.date = Date.today.to_s
+ gem.email = 'info@netskin.com'
+ gem.homepage = 'http://github.com/gucki/sequel_mappable'
+ gem.summary = 'Sequel plugin which provides distance-based filters and distance calculation functionality for model.'
+ gem.description = gem.summary
+
+ gem.has_rdoc = true
+ gem.require_paths = ['lib']
+ gem.extra_rdoc_files = ['README.rdoc', 'LICENSE', 'CHANGELOG']
+ gem.files = Dir['Rakefile', '{lib,spec}/**/*', 'README*', 'LICENSE*', 'CHANGELOG*'] & `git ls-files -z`.split("\0")
+
+ gem.add_dependency 'sequel', ">= 3.0.0"
+ gem.add_development_dependency 'sqlite3-ruby'
+ gem.add_development_dependency 'rspec'
+ gem.add_development_dependency 'yard'
+end

0 comments on commit 7f1e1ac

Please sign in to comment.