mordor 0.2.15 → 0.2.16

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -3,6 +3,9 @@ language: ruby
3
3
  jdk:
4
4
  - oraclejdk7
5
5
 
6
+ services:
7
+ - mongodb
8
+
6
9
  rvm:
7
10
  - 1.8.7
8
11
  - 1.9.2
data/Gemfile CHANGED
@@ -8,7 +8,7 @@ source "http://rubygems.org"
8
8
  gem "rake"
9
9
  gem "extlib"
10
10
  gem "mongo"
11
- gem "bson"
11
+ gem "bson_ext", :platforms => :ruby
12
12
  gem "json"
13
13
 
14
14
  group :test do
data/Gemfile.lock CHANGED
@@ -1,30 +1,38 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- bson (1.6.4)
4
+ activesupport (3.2.9)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ bson (1.8.0)
8
+ activesupport
9
+ bson_ext (1.8.0)
10
+ bson (~> 1.8.0)
5
11
  diff-lcs (1.1.3)
6
12
  extlib (0.9.15)
7
- json (1.7.3)
8
- mongo (1.6.4)
9
- bson (~> 1.6.4)
13
+ i18n (0.6.1)
14
+ json (1.7.5)
15
+ mongo (1.8.0)
16
+ bson (~> 1.8.0)
17
+ multi_json (1.5.0)
10
18
  rack (1.4.1)
11
- rack-test (0.6.1)
19
+ rack-test (0.6.2)
12
20
  rack (>= 1.0)
13
- rake (0.9.2.2)
14
- rspec (2.10.0)
15
- rspec-core (~> 2.10.0)
16
- rspec-expectations (~> 2.10.0)
17
- rspec-mocks (~> 2.10.0)
18
- rspec-core (2.10.1)
19
- rspec-expectations (2.10.0)
21
+ rake (10.0.3)
22
+ rspec (2.12.0)
23
+ rspec-core (~> 2.12.0)
24
+ rspec-expectations (~> 2.12.0)
25
+ rspec-mocks (~> 2.12.0)
26
+ rspec-core (2.12.2)
27
+ rspec-expectations (2.12.1)
20
28
  diff-lcs (~> 1.1.3)
21
- rspec-mocks (2.10.1)
29
+ rspec-mocks (2.12.0)
22
30
 
23
31
  PLATFORMS
24
32
  ruby
25
33
 
26
34
  DEPENDENCIES
27
- bson
35
+ bson_ext
28
36
  extlib
29
37
  json
30
38
  mongo
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 J.W. Koelewijn
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/lib/mordor.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
+ require 'rational'
2
3
  require 'mongo'
3
4
  require 'extlib'
4
5
  require 'json'
@@ -136,7 +136,7 @@ module Mordor
136
136
  end
137
137
 
138
138
  def collection
139
- connection.collection(self.collection_name)
139
+ database.collection(self.collection_name)
140
140
  end
141
141
 
142
142
  def collection_name
@@ -155,12 +155,14 @@ module Mordor
155
155
  end
156
156
  end
157
157
 
158
- def connection
159
- unless @connection
160
- @connection = Mongo::Connection.new(Mordor::Config[:hostname], Mordor::Config[:port])
161
- @connection.autenticate(Mordor::Config[:username], Mordor::Config[:password]) if Mordor::Config[:username]
158
+ def database
159
+ unless @db
160
+ connection = Mongo::Connection.new(Mordor::Config[:hostname], Mordor::Config[:port])
161
+ @db = connection.db(Mordor::Config[:database])
162
+ @db.authenticate(Mordor::Config[:username], Mordor::Config[:password]) if Mordor::Config[:username]
162
163
  end
163
- @connection.db(Mordor::Config[:database])
164
+
165
+ @db
164
166
  end
165
167
 
166
168
  def find_by_id(id)
data/mordor.gemspec CHANGED
@@ -2,8 +2,8 @@ Gem::Specification.new do |s|
2
2
  s.name = "mordor"
3
3
 
4
4
  # Do not set the version and date field manually, this is done by the release script
5
- s.version = "0.2.15"
6
- s.date = "2012-06-21"
5
+ s.version = "0.2.16"
6
+ s.date = "2012-12-17"
7
7
 
8
8
  s.summary = "mordor"
9
9
  s.description = <<-eos
@@ -27,6 +27,6 @@ Gem::Specification.new do |s|
27
27
 
28
28
  # The files and test_files directives are set automatically by the release script.
29
29
  # Do not change them by hand, but make sure to add the files to the git repository.
30
- s.files = %w(.gitignore .travis.yml Gemfile Gemfile.lock README.md Rakefile lib/mordor.rb lib/mordor/collection.rb lib/mordor/config.rb lib/mordor/resource.rb lib/mordor/version.rb mordor.gemspec spec/mordor/collection_spec.rb spec/mordor/connection_spec.rb spec/mordor/resource_spec.rb spec/spec.opts spec/spec_helper.rb tasks/github-gem.rake)
30
+ s.files = %w(.gitignore .travis.yml Gemfile Gemfile.lock LICENSE README.md Rakefile lib/mordor.rb lib/mordor/collection.rb lib/mordor/config.rb lib/mordor/resource.rb lib/mordor/version.rb mordor.gemspec spec/mordor/collection_spec.rb spec/mordor/connection_spec.rb spec/mordor/resource_spec.rb spec/spec.opts spec/spec_helper.rb tasks/github-gem.rake)
31
31
  end
32
32
 
@@ -1,18 +1,22 @@
1
1
  require File.join(File.dirname(__FILE__), '..', '/spec_helper.rb')
2
2
 
3
3
  describe "with respect to collections" do
4
- class TestResource
5
- include Mordor::Resource
4
+ before :each do
5
+ class TestResource
6
+ include Mordor::Resource
6
7
 
7
- attribute :first, :index => true
8
- attribute :second, :index => true, :index_type => Mongo::ASCENDING
9
- attribute :third, :finder_method => :find_by_third_attribute
8
+ attribute :first, :index => true
9
+ attribute :second, :index => true, :index_type => Mongo::ASCENDING
10
+ attribute :third, :finder_method => :find_by_third_attribute
11
+ end
10
12
  end
11
13
 
12
- describe "serialization" do
13
- before :all do
14
- clean_sheet
14
+ after :each do
15
+ drop_db_collections
16
+ end
15
17
 
18
+ describe "serialization" do
19
+ before :each do
16
20
  5.times do |index|
17
21
  res = TestResource.new(:first => "#{index}_first", :second => "#{index}_second", :third => "#{index}_third")
18
22
  res.save.should be_true
@@ -33,9 +37,7 @@ describe "with respect to collections" do
33
37
  end
34
38
 
35
39
  describe "converting to array" do
36
- before :all do
37
- clean_sheet
38
-
40
+ before :each do
39
41
  5.times do |index|
40
42
  res = TestResource.new(:first => "#{index}_first", :second => "#{index}_second", :third => "#{index}_third")
41
43
  res.save.should be_true
@@ -73,9 +75,7 @@ describe "with respect to collections" do
73
75
 
74
76
  describe "counting" do
75
77
 
76
- before :all do
77
- clean_sheet
78
-
78
+ before :each do
79
79
  5.times do |index|
80
80
  res = TestResource.new(:first => "#{index}_first", :second => "#{index}_second", :third => "#{index}_third")
81
81
  res.save.should be_true
@@ -1,26 +1,63 @@
1
1
  require File.join(File.dirname(__FILE__), '..', '/spec_helper.rb')
2
2
 
3
3
  describe "connecting to mongo" do
4
-
5
4
  before :each do
6
- clean_sheet
5
+ class TestResource
6
+ include Mordor::Resource
7
+ end
7
8
  end
8
9
 
9
- it "should have a connection to mongo" do
10
- @connection.should be_instance_of(Mongo::Connection)
10
+ it "should have a mongo database " do
11
+ TestResource.database.should be_instance_of(Mongo::DB)
11
12
  end
12
13
 
13
14
  it "should select the correct database" do
14
- @db.name.should == Mordor::Config[:database]
15
+ database_name = "any_database_name"
16
+ Mordor::Config.use { |config| config[:database] = database_name }
17
+
18
+ TestResource.database.name.should == database_name
15
19
  end
16
20
 
17
- it "should be possible to change the configuration" do
18
- old_db = Mordor::Config[:database]
19
- Mordor::Config.use do |c|
20
- c[:database] = 'some_other'
21
+ describe "when credentials are provided" do
22
+ let(:credentials) { {:username => "A username", :password => "A password"} }
23
+
24
+ before :each do
25
+ Mordor::Config.use do |config|
26
+ config[:username] = credentials[:username]
27
+ config[:password] = credentials[:password]
28
+ end
29
+
30
+ @mock_db = mock("db")
31
+ Mongo::Connection.stub(:new).and_return(mock("connection", :db => @mock_db))
21
32
  end
22
33
 
23
- Mordor::Config[:database].should == 'some_other'
24
- Mordor::Config[:database] = old_db
34
+ it "should authenticate with username and password" do
35
+ @mock_db.should_receive(:authenticate).with(credentials[:username], credentials[:password])
36
+ TestResource.database
37
+ end
38
+ end
39
+
40
+ describe "the Mongo database connection" do
41
+ before :each do
42
+ @mock_connection = mock("connection", :db => mock("db"))
43
+ end
44
+
45
+ it "should connect with specified host" do
46
+ host = "any host IP or reachable hostname"
47
+ Mordor::Config.use { |config| config[:hostname] = host }
48
+
49
+ Mongo::Connection.should_receive(:new).with(host, anything).and_return(@mock_connection)
50
+
51
+ TestResource.database
52
+ end
53
+
54
+ it "should connect on specified port" do
55
+ port = rand(10000)
56
+ Mordor::Config.use { |config| config[:port] = port }
57
+
58
+ Mongo::Connection.should_receive(:new).with(anything, port).and_return(@mock_connection)
59
+
60
+ TestResource.database
61
+ end
25
62
  end
26
63
  end
@@ -2,7 +2,6 @@ require File.join(File.dirname(__FILE__), '..', '/spec_helper.rb')
2
2
 
3
3
  describe "with respect to resources" do
4
4
  before :each do
5
- Object.send(:remove_const, :TestResource) if Object.const_defined?(:TestResource)
6
5
  class TestResource
7
6
  include Mordor::Resource
8
7
 
@@ -21,6 +20,10 @@ describe "with respect to resources" do
21
20
  end
22
21
  end
23
22
 
23
+ after :each do
24
+ drop_db_collections
25
+ end
26
+
24
27
  it "should create accessor methods for all attributes" do
25
28
  ["first", "first=", "second", "second="].each{ |v| TestResource.public_instance_methods.map{|m| m.to_s}.should include(v) }
26
29
  end
@@ -71,10 +74,6 @@ describe "with respect to resources" do
71
74
  end
72
75
 
73
76
  context "with respect to replacing params" do
74
- before :each do
75
- clean_sheet
76
- end
77
-
78
77
  it "should correctly substitute non-alphanumeric characters in keys with underscores" do
79
78
  options = {
80
79
  "o*p#t>i_o@n)s" => "test"
@@ -176,7 +175,7 @@ describe "with respect to resources" do
176
175
  end
177
176
 
178
177
  context "when ranges are changed to queries" do
179
- before :all do
178
+ before :each do
180
179
  @range = TestResource.send(:day_to_range, DateTime.civil(2012, 1, 19))
181
180
  @query = TestResource.send(:date_range_to_query, @range)
182
181
  end
@@ -261,7 +260,6 @@ describe "with respect to resources" do
261
260
 
262
261
  context "with respect to creating" do
263
262
  before :each do
264
- clean_sheet
265
263
  @resource = TestResource.create({:first => "first", :second => "second", :third => "third"})
266
264
  end
267
265
 
@@ -281,7 +279,6 @@ describe "with respect to resources" do
281
279
 
282
280
  context "with respect to destroying" do
283
281
  before :each do
284
- clean_sheet
285
282
  @resource = TestResource.create({:first => "first", :second => "second", :third => "third"})
286
283
  end
287
284
 
@@ -309,10 +306,6 @@ describe "with respect to resources" do
309
306
  end
310
307
 
311
308
  context "with respect to saving and retrieving" do
312
- before :each do
313
- clean_sheet
314
- end
315
-
316
309
  it "should correctly save resources" do
317
310
  resource = TestResource.new({:first => "first", :second => "second"})
318
311
  resource.save.should be_true
@@ -477,15 +470,13 @@ describe "with respect to resources" do
477
470
  end
478
471
 
479
472
  context "with respect to retrieving by day" do
480
- class TestTimedResource
481
- include Mordor::Resource
482
-
483
- attribute :first
484
- attribute :at
485
- end
486
-
487
473
  before :each do
488
- clean_sheet
474
+ class TestTimedResource
475
+ include Mordor::Resource
476
+
477
+ attribute :first
478
+ attribute :at
479
+ end
489
480
  end
490
481
 
491
482
  it "should be possible to retrieve a Resource by day" do
@@ -509,8 +500,8 @@ describe "with respect to resources" do
509
500
  TestResource.collection_name.should == "testresources"
510
501
  end
511
502
 
512
- it "should correctly create a connection" do
513
- TestResource.connection.should_not be_nil
503
+ it "should be connected to a database" do
504
+ TestResource.database.should_not be_nil
514
505
  end
515
506
  end
516
507
 
@@ -521,11 +512,8 @@ describe "with respect to resources" do
521
512
  end
522
513
 
523
514
  it "should return nil when an non existing id is queried" do
524
- clean_sheet
525
515
  resource = TestResource.find_by_id('4eb8f3570e02e10cce000002')
526
516
  resource.should be_nil
527
517
  end
528
518
  end
529
-
530
-
531
519
  end
data/spec/spec_helper.rb CHANGED
@@ -4,16 +4,46 @@ require 'mongo'
4
4
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
5
5
  require 'mordor'
6
6
 
7
- Mordor::Config[:database] = 'test'
7
+ RSpec.configure do |config|
8
+ config.before :each do
9
+ reset_mordor_config
10
+ end
11
+
12
+ config.after :each do
13
+ remove_class_constants
14
+ end
15
+ end
16
+
17
+ def reset_mordor_config
18
+ Mordor::Config.use do |config|
19
+ config[:username] = nil
20
+ config[:password] = nil
21
+ config[:hostname] = '127.0.0.1'
22
+ config[:port] = 27017
23
+ config[:database] = 'test'
24
+ end
25
+ end
8
26
 
9
- def clean_sheet
10
- @connection ||= Mongo::Connection.new(Mordor::Config[:hostname], Mordor::Config[:port])
11
- @db ||= @connection[Mordor::Config[:database]]
27
+ def drop_db_collections
28
+ connection = Mongo::Connection.new(Mordor::Config[:hostname], Mordor::Config[:port])
29
+ db = connection[Mordor::Config[:database]]
12
30
 
13
- ['TestResource', 'TestTimedResource'].each do |resource|
31
+ test_class_names.each do |resource|
14
32
  if Object.const_defined?(resource)
15
- @db[Object.const_get(resource).collection_name].drop
33
+ db[Object.const_get(resource).collection_name].remove
34
+ db[Object.const_get(resource).collection_name].drop
16
35
  end
17
36
  end
18
37
  end
19
38
 
39
+ def remove_class_constants
40
+ test_class_names.each do |resource_class|
41
+ Object.send(:remove_const, resource_class) if Object.const_defined?(resource_class)
42
+ end
43
+ end
44
+
45
+ def test_class_names
46
+ [:TestResource, :TestResource2, :TestTimedResource]
47
+ end
48
+
49
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mordor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.15
4
+ version: 0.2.16
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-06-21 00:00:00.000000000 Z
13
+ date: 2012-12-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -156,6 +156,7 @@ files:
156
156
  - .travis.yml
157
157
  - Gemfile
158
158
  - Gemfile.lock
159
+ - LICENSE
159
160
  - README.md
160
161
  - Rakefile
161
162
  - lib/mordor.rb
@@ -184,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
184
185
  version: '0'
185
186
  segments:
186
187
  - 0
187
- hash: -811110001220592058
188
+ hash: 3720048276768169463
188
189
  required_rubygems_version: !ruby/object:Gem::Requirement
189
190
  none: false
190
191
  requirements:
@@ -193,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
194
  version: '0'
194
195
  requirements: []
195
196
  rubyforge_project:
196
- rubygems_version: 1.8.22
197
+ rubygems_version: 1.8.24
197
198
  signing_key:
198
199
  specification_version: 3
199
200
  summary: mordor