sig_mongoid_spacial 0.2.17
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +49 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/README.md +180 -0
- data/Rakefile +18 -0
- data/lib/mongoid_spacial.rb +11 -0
- data/lib/mongoid_spacial/contexts/mongo.rb +115 -0
- data/lib/mongoid_spacial/criteria.rb +5 -0
- data/lib/mongoid_spacial/criterion.rb +3 -0
- data/lib/mongoid_spacial/criterion/complex.rb +19 -0
- data/lib/mongoid_spacial/criterion/inclusion.rb +14 -0
- data/lib/mongoid_spacial/criterion/near_spacial.rb +50 -0
- data/lib/mongoid_spacial/criterion/within_spacial.rb +60 -0
- data/lib/mongoid_spacial/extentions/hash/criteria_helpers.rb +22 -0
- data/lib/mongoid_spacial/extentions/symbol/inflections.rb +46 -0
- data/lib/mongoid_spacial/field_option.rb +38 -0
- data/lib/mongoid_spacial/finders.rb +5 -0
- data/lib/mongoid_spacial/spacial.rb +56 -0
- data/lib/mongoid_spacial/spacial/core_ext.rb +27 -0
- data/lib/mongoid_spacial/spacial/document.rb +29 -0
- data/lib/mongoid_spacial/spacial/formulas.rb +52 -0
- data/lib/mongoid_spacial/spacial/geo_near_results.rb +140 -0
- data/lib/mongoid_spacial/spacial/version.rb +5 -0
- data/mongoid_spacial.gemspec +27 -0
- data/spec/config/mongod.conf +3 -0
- data/spec/config/mongoid.yml +18 -0
- data/spec/functional/mongoid/contexts/mongo_spec.rb +125 -0
- data/spec/functional/mongoid/criterion/inclusion_spec.rb +356 -0
- data/spec/functional/mongoid/spacial/geo_near_results_spec.rb +73 -0
- data/spec/functional/mongoid/spacial_spec.rb +18 -0
- data/spec/models/account.rb +19 -0
- data/spec/models/acolyte.rb +9 -0
- data/spec/models/address.rb +62 -0
- data/spec/models/address_component.rb +5 -0
- data/spec/models/agent.rb +10 -0
- data/spec/models/alert.rb +5 -0
- data/spec/models/animal.rb +21 -0
- data/spec/models/answer.rb +4 -0
- data/spec/models/bar.rb +9 -0
- data/spec/models/birthday.rb +13 -0
- data/spec/models/book.rb +5 -0
- data/spec/models/business.rb +7 -0
- data/spec/models/callbacks.rb +57 -0
- data/spec/models/category.rb +13 -0
- data/spec/models/circus.rb +7 -0
- data/spec/models/comment.rb +13 -0
- data/spec/models/country_code.rb +6 -0
- data/spec/models/description.rb +11 -0
- data/spec/models/division.rb +5 -0
- data/spec/models/drug.rb +5 -0
- data/spec/models/employer.rb +5 -0
- data/spec/models/entry.rb +6 -0
- data/spec/models/event.rb +20 -0
- data/spec/models/favorite.rb +6 -0
- data/spec/models/fruits.rb +11 -0
- data/spec/models/game.rb +18 -0
- data/spec/models/ghost.rb +7 -0
- data/spec/models/house.rb +4 -0
- data/spec/models/inheritance.rb +90 -0
- data/spec/models/league.rb +5 -0
- data/spec/models/location.rb +5 -0
- data/spec/models/login.rb +6 -0
- data/spec/models/membership.rb +4 -0
- data/spec/models/mixed_drink.rb +4 -0
- data/spec/models/name.rb +13 -0
- data/spec/models/namespacing.rb +11 -0
- data/spec/models/observed.rb +41 -0
- data/spec/models/override.rb +16 -0
- data/spec/models/owner.rb +6 -0
- data/spec/models/page.rb +5 -0
- data/spec/models/page_question.rb +4 -0
- data/spec/models/paranoid_post.rb +18 -0
- data/spec/models/parents.rb +32 -0
- data/spec/models/patient.rb +15 -0
- data/spec/models/person.rb +146 -0
- data/spec/models/pet.rb +7 -0
- data/spec/models/pet_owner.rb +6 -0
- data/spec/models/phone.rb +7 -0
- data/spec/models/player.rb +23 -0
- data/spec/models/post.rb +26 -0
- data/spec/models/preference.rb +9 -0
- data/spec/models/question.rb +8 -0
- data/spec/models/quiz.rb +6 -0
- data/spec/models/rating.rb +8 -0
- data/spec/models/river.rb +20 -0
- data/spec/models/role.rb +5 -0
- data/spec/models/service.rb +6 -0
- data/spec/models/shelf.rb +5 -0
- data/spec/models/slave_address_numbers.rb +14 -0
- data/spec/models/survey.rb +5 -0
- data/spec/models/tag.rb +6 -0
- data/spec/models/tracking_id_validation_history.rb +25 -0
- data/spec/models/translation.rb +5 -0
- data/spec/models/tree.rb +9 -0
- data/spec/models/user.rb +9 -0
- data/spec/models/user_account.rb +10 -0
- data/spec/models/vet_visit.rb +5 -0
- data/spec/models/video.rb +9 -0
- data/spec/models/wiki_page.rb +6 -0
- data/spec/spec_helper.rb +43 -0
- data/spec/support/authentication.rb +29 -0
- data/spec/unit/mongoid/criterion/complex_spec.rb +15 -0
- data/spec/unit/mongoid/criterion/inclusion_spec.rb +0 -0
- data/spec/unit/mongoid/criterion/near_spacial_spec.rb +39 -0
- data/spec/unit/mongoid/criterion/within_spacial_spec.rb +52 -0
- data/spec/unit/mongoid/spacial/formulas_spec.rb +37 -0
- data/spec/unit/mongoid/spacial_spec.rb +6 -0
- metadata +369 -0
data/spec/models/pet.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
class Player
|
2
|
+
include Mongoid::Document
|
3
|
+
field :active, :type => Boolean
|
4
|
+
field :frags, :type => Integer
|
5
|
+
field :deaths, :type => Integer
|
6
|
+
field :status
|
7
|
+
|
8
|
+
named_scope :active, criteria.where(:active => true) do
|
9
|
+
def extension
|
10
|
+
"extension"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
named_scope :inactive, :where => { :active => false }
|
14
|
+
named_scope :frags_over, lambda { |count| { :where => { :frags.gt => count } } }
|
15
|
+
named_scope :deaths_under, lambda { |count| criteria.where(:deaths.lt => count) }
|
16
|
+
scope :deaths_over, lambda { |count| criteria.where(:deaths.gt => count) }
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def alive
|
20
|
+
criteria.where(:status => "Alive")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/models/post.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
class Post
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::MultiParameterAttributes
|
4
|
+
include Mongoid::Versioning
|
5
|
+
include Mongoid::Timestamps
|
6
|
+
|
7
|
+
field :title, :type => String
|
8
|
+
field :content, :type => String
|
9
|
+
field :rating, :type => Integer
|
10
|
+
|
11
|
+
belongs_to :person
|
12
|
+
belongs_to :author, :foreign_key => :author_id, :class_name => "User"
|
13
|
+
has_and_belongs_to_many :tags
|
14
|
+
has_many :videos, :validate => false
|
15
|
+
|
16
|
+
scope :recent, where(:created_at => { "$lt" => Time.now, "$gt" => 30.days.ago })
|
17
|
+
scope :posting, where(:content.in => [ "Posting" ])
|
18
|
+
|
19
|
+
validates_format_of :title, :without => /\$\$\$/
|
20
|
+
|
21
|
+
class << self
|
22
|
+
def old
|
23
|
+
where(:created_at => { "$lt" => 30.days.ago })
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class Preference
|
2
|
+
include Mongoid::Document
|
3
|
+
field :name, :type => String
|
4
|
+
field :value, :type => String
|
5
|
+
field :ranking, :type => Integer
|
6
|
+
has_and_belongs_to_many :people
|
7
|
+
validates_length_of :name, :minimum => 2, :allow_nil => true
|
8
|
+
scope :posting, where(:value.in => [ "Posting" ])
|
9
|
+
end
|
data/spec/models/quiz.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
class Rating
|
2
|
+
include Mongoid::Document
|
3
|
+
field :value, :type => Integer
|
4
|
+
referenced_in :ratable, :polymorphic => true
|
5
|
+
references_many :comments
|
6
|
+
validates_numericality_of :value, :less_than => 100, :allow_nil => true
|
7
|
+
validates :ratable, :associated => true
|
8
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class River
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Spacial::Document
|
4
|
+
|
5
|
+
field :name, type: String
|
6
|
+
field :length, type: Integer
|
7
|
+
field :average_discharge, type: Integer
|
8
|
+
field :source, type: Array, spacial: true
|
9
|
+
# set return_array to true if you do not want a hash returned all the time
|
10
|
+
field :mouth, type: Array, spacial: {lat: 'latitude', lng: 'longitude'}
|
11
|
+
field :mouth_array, type: Array, spacial: {return_array: true}
|
12
|
+
|
13
|
+
# simplified spacial indexing
|
14
|
+
# you can only index one field in mongodb < 1.9
|
15
|
+
spacial_index :source
|
16
|
+
# alternatives
|
17
|
+
# index [[ :spacial, Mongo::GEO2D ]], {min:-400, max:400}
|
18
|
+
# index [[ :spacial, Mongo::GEO2D ]], {bit:32}
|
19
|
+
# index [[ :spacial, Mongo::GEO2D ],:name]
|
20
|
+
end
|
data/spec/models/role.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# These models used for Github 263
|
2
|
+
class Slave
|
3
|
+
include Mongoid::Document
|
4
|
+
field :first_name
|
5
|
+
field :last_name
|
6
|
+
embeds_many :address_numbers
|
7
|
+
end
|
8
|
+
|
9
|
+
class AddressNumber
|
10
|
+
include Mongoid::Document
|
11
|
+
field :country_code, :type => Integer, :default => 1
|
12
|
+
field :number
|
13
|
+
embedded_in :slave
|
14
|
+
end
|
data/spec/models/tag.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# These models are spcific to test for Github #313.
|
2
|
+
module MyCompany
|
3
|
+
module Model
|
4
|
+
class TrackingId
|
5
|
+
include Mongoid::Document
|
6
|
+
include Mongoid::Timestamps
|
7
|
+
store_in :tracking_ids
|
8
|
+
embeds_many :validation_history, :class_name => "MyCompany::Model::TrackingIdValidationHistory"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module MyCompany
|
14
|
+
module Model
|
15
|
+
# A TrackingId validation state change
|
16
|
+
class TrackingIdValidationHistory
|
17
|
+
include Mongoid::Document
|
18
|
+
field :old_state, :type => String
|
19
|
+
field :new_state, :type => String
|
20
|
+
field :when_changed, :type => DateTime
|
21
|
+
attr_protected :_id
|
22
|
+
embedded_in :tracking_id, :class_name => "MyCompany::Model::TrackingId"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/models/tree.rb
ADDED
data/spec/models/user.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
class UserAccount
|
2
|
+
include Mongoid::Document
|
3
|
+
field :username
|
4
|
+
field :name
|
5
|
+
field :email
|
6
|
+
validates_uniqueness_of :username, :message => "is not unique"
|
7
|
+
validates_uniqueness_of :email, :message => "is not unique", :case_sensitive => false
|
8
|
+
validates_length_of :name, :minimum => 2, :allow_nil => true
|
9
|
+
references_and_referenced_in_many :people
|
10
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
MODELS = File.join(File.dirname(__FILE__), "models")
|
5
|
+
SUPPORT = File.join(File.dirname(__FILE__), "support")
|
6
|
+
$LOAD_PATH.unshift(MODELS)
|
7
|
+
$LOAD_PATH.unshift(SUPPORT)
|
8
|
+
|
9
|
+
require "mongoid"
|
10
|
+
require "mocha"
|
11
|
+
require "rspec"
|
12
|
+
require "mongoid_spacial"
|
13
|
+
|
14
|
+
LOGGER = Logger.new($stdout)
|
15
|
+
|
16
|
+
if RUBY_VERSION >= '1.9.2'
|
17
|
+
YAML::ENGINE.yamler = 'syck'
|
18
|
+
end
|
19
|
+
|
20
|
+
Mongoid.configure do |config|
|
21
|
+
name = "mongoid_spacial_test"
|
22
|
+
config.master = Mongo::Connection.new.db(name)
|
23
|
+
config.logger = nil
|
24
|
+
config.allow_dynamic_fields = true
|
25
|
+
end
|
26
|
+
|
27
|
+
Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
|
28
|
+
Dir[ File.join(SUPPORT, "*.rb") ].each { |file| require File.basename(file) }
|
29
|
+
|
30
|
+
RSpec.configure do |config|
|
31
|
+
config.mock_with(:mocha)
|
32
|
+
|
33
|
+
config.after(:suite) { Mongoid.purge! }
|
34
|
+
|
35
|
+
# We filter out the specs that require authentication if the database has not
|
36
|
+
# had the mongoid user set up properly.
|
37
|
+
user_configured = Support::Authentication.configured?
|
38
|
+
warn(Support::Authentication.message) unless user_configured
|
39
|
+
|
40
|
+
config.filter_run_excluding(:config => lambda { |value|
|
41
|
+
return true if value == :user && !user_configured
|
42
|
+
})
|
43
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Support #:nodoc:
|
3
|
+
module Authentication
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def configured?
|
7
|
+
begin
|
8
|
+
master_uri = "mongodb://mongoid:test@localhost:27017/mongoid_spacial_test"
|
9
|
+
Mongo::Connection.from_uri(master_uri)
|
10
|
+
true
|
11
|
+
rescue Mongo::AuthenticationError => e
|
12
|
+
false
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def message
|
17
|
+
%Q{
|
18
|
+
---------------------------------------------------------------------
|
19
|
+
A user needs to be configured for authentication, otherwise some
|
20
|
+
configuration specs will not get run. You may set it up from the
|
21
|
+
mongo console:
|
22
|
+
|
23
|
+
$ use mongoid_spacial_test;
|
24
|
+
$ db.addUser("mongoid", "test");
|
25
|
+
---------------------------------------------------------------------
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Criterion::Complex do
|
4
|
+
|
5
|
+
let(:complex) { Mongoid::Criterion::Complex.new(:key => :field, :operator => "gt") }
|
6
|
+
|
7
|
+
let(:value) { 40 }
|
8
|
+
|
9
|
+
context "#to_mongo_query" do
|
10
|
+
it "should turn value into appropriate query" do
|
11
|
+
complex.to_mongo_query(value).should == {"$gt" => value}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
File without changes
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Criterion::NearSpacial do
|
4
|
+
|
5
|
+
let(:within) do
|
6
|
+
{
|
7
|
+
:flat => Mongoid::Criterion::WithinSpacial.new(:key => :field, :operator => "near"),
|
8
|
+
:sphere => Mongoid::Criterion::WithinSpacial.new(:key => :field, :operator => "nearSphere"),
|
9
|
+
}
|
10
|
+
end
|
11
|
+
NEAR = {
|
12
|
+
:flat =>
|
13
|
+
{
|
14
|
+
'Point' => [[1,2],5],
|
15
|
+
'Hash Point' => {:point => [-73.98, 40.77], :max => 5},
|
16
|
+
'Hash Point Unit' => {:point => [-73.98, 40.77], :max => 5, :unit => :km}
|
17
|
+
},
|
18
|
+
:sphere =>
|
19
|
+
{
|
20
|
+
'Point' => [[1,2],5],
|
21
|
+
'Hash Point' => {:point => [-73.98, 40.77], :max => 5},
|
22
|
+
'Hash Point Unit' => {:point => [-73.98, 40.77], :max => 5, :unit => :km}
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
context "#to_mongo_query" do
|
27
|
+
|
28
|
+
NEAR.each do |shape, points|
|
29
|
+
points.each do |input_name,input|
|
30
|
+
it "#{shape} should generate a query with #{input_name}" do
|
31
|
+
within[shape].to_mongo_query(input).should be_a_kind_of(Hash)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Criterion::WithinSpacial do
|
4
|
+
|
5
|
+
let(:within) do
|
6
|
+
{
|
7
|
+
:box => Mongoid::Criterion::WithinSpacial.new(:key => :field, :operator => "box"),
|
8
|
+
:polygon => Mongoid::Criterion::WithinSpacial.new(:key => :field, :operator => "polygon"),
|
9
|
+
:center => Mongoid::Criterion::WithinSpacial.new(:key => :field, :operator => "center"),
|
10
|
+
:center_sphere => Mongoid::Criterion::WithinSpacial.new(:key => :field, :operator => "box"),
|
11
|
+
}
|
12
|
+
end
|
13
|
+
WITHIN = {
|
14
|
+
:box =>
|
15
|
+
{
|
16
|
+
'Array of Arrays' => [[10,20], [15,25]],
|
17
|
+
'Array of Hashes' => [{ x: 10, y: 20 }, { x: 15, y: 25 }],
|
18
|
+
'Hash of Hashes' => { a: { x: 10, y: 20 }, b: { x: 15, y: 25 }}
|
19
|
+
},
|
20
|
+
:polygon =>
|
21
|
+
{
|
22
|
+
'Array of Arrays' => [[10,20], [15,25]],
|
23
|
+
'Array of Hashes' => [{ x: 10, y: 20 }, { x: 15, y: 25 }],
|
24
|
+
'Hash of Hashes' => { a: { x: 10, y: 20 }, b: { x: 15, y: 25 }}
|
25
|
+
},
|
26
|
+
:center =>
|
27
|
+
{
|
28
|
+
'Point' => [[1,2],5],
|
29
|
+
'Hash Point' => {:point => [-73.98, 40.77], :max => 5},
|
30
|
+
'Hash Point Unit' => {:point => [-73.98, 40.77], :max => 5, :unit => :km}
|
31
|
+
},
|
32
|
+
:center_sphere =>
|
33
|
+
{
|
34
|
+
'Point' => [[1,2],5],
|
35
|
+
'Hash Point' => {:point => [-73.98, 40.77], :max => 5},
|
36
|
+
'Hash Point Unit' => {:point => [-73.98, 40.77], :max => 5, :unit => :km}
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
context "#to_mongo_query" do
|
41
|
+
|
42
|
+
WITHIN.each do |shape, points|
|
43
|
+
points.each do |input_name,input|
|
44
|
+
it "#{shape} should generate a query with #{input_name}" do
|
45
|
+
within[shape].to_mongo_query(input).should be_a_kind_of(Hash)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|