active_mongo 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/active_mongo.gemspec +60 -0
- data/lib/README +3 -0
- data/lib/active_mongo.rb +27 -0
- data/lib/active_mongo_collection.rb +74 -0
- data/lib/active_mongo_has_many.rb +46 -0
- data/lib/active_mongo_instance.rb +117 -0
- data/lib/active_mongo_scope.rb +16 -0
- data/lib/active_mongo_uniquenesss.rb +33 -0
- data/test/helper.rb +10 -0
- data/test/test_active_mongo.rb +7 -0
- metadata +81 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Mantas Masalskis
|
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/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "active_mongo"
|
8
|
+
gem.summary = %Q{Schemaless Mongo ORM for Rails 3.0}
|
9
|
+
gem.description = %Q{Schemaless Mongo ORM for Rails 3.0}
|
10
|
+
gem.email = "mantas@idev.lt"
|
11
|
+
gem.homepage = "http://github.com/mantas/active_mongo"
|
12
|
+
gem.authors = ["Mantas Masalskis"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "active_mongo #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{active_mongo}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Mantas Masalskis"]
|
12
|
+
s.date = %q{2010-01-10}
|
13
|
+
s.description = %q{Schemaless Mongo ORM for Rails 3.0}
|
14
|
+
s.email = %q{mantas@idev.lt}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"active_mongo.gemspec",
|
27
|
+
"lib/README",
|
28
|
+
"lib/active_mongo.rb",
|
29
|
+
"lib/active_mongo_collection.rb",
|
30
|
+
"lib/active_mongo_has_many.rb",
|
31
|
+
"lib/active_mongo_instance.rb",
|
32
|
+
"lib/active_mongo_scope.rb",
|
33
|
+
"lib/active_mongo_uniquenesss.rb",
|
34
|
+
"test/helper.rb",
|
35
|
+
"test/test_active_mongo.rb"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/mantas/active_mongo}
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.3.5}
|
41
|
+
s.summary = %q{Schemaless Mongo ORM for Rails 3.0}
|
42
|
+
s.test_files = [
|
43
|
+
"test/helper.rb",
|
44
|
+
"test/test_active_mongo.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
data/lib/README
ADDED
data/lib/active_mongo.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
require 'lib/active_mongo_uniquenesss'
|
3
|
+
include Mongo
|
4
|
+
|
5
|
+
config = YAML::load(File.open("#{RAILS_ROOT}/config/mongo.yml"))[Rails.env]
|
6
|
+
|
7
|
+
$mongo_conn = Connection.new(config["host"], config["port"], :pool_size => 5, :timeout => 5)
|
8
|
+
$mongo_db = $mongo_conn.db(config["database"])
|
9
|
+
|
10
|
+
require 'lib/active_mongo_has_many'
|
11
|
+
|
12
|
+
module ActiveMongo
|
13
|
+
|
14
|
+
class Base
|
15
|
+
include ActiveModel::Conversion
|
16
|
+
include ActiveModel::Validations
|
17
|
+
extend ActiveMongo::HasMany::ClassMethods
|
18
|
+
include ActiveMongo::HasMany::InstanceMethods
|
19
|
+
|
20
|
+
class << self; attr_accessor :scope; end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
require 'lib/active_mongo_collection'
|
26
|
+
require 'lib/active_mongo_instance'
|
27
|
+
require 'lib/active_mongo_scope'
|
@@ -0,0 +1,74 @@
|
|
1
|
+
ActiveMongo::Base.class_eval do
|
2
|
+
def self.collection_name
|
3
|
+
(self.name || @name).downcase.pluralize
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.collection
|
7
|
+
$mongo_db.collection(collection_name)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.count(attrs = {})
|
11
|
+
attrs = self.scope.merge(attrs) if self.scope
|
12
|
+
|
13
|
+
if attrs.any?
|
14
|
+
self.collection.find(attrs).count
|
15
|
+
else
|
16
|
+
self.collection.count
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.destroy_all(attrs = {})
|
21
|
+
attrs = self.scope.merge(attrs) if self.scope
|
22
|
+
|
23
|
+
|
24
|
+
if attrs.any?
|
25
|
+
self.collection.remove(attrs)
|
26
|
+
else
|
27
|
+
self.collection.remove()
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.find(attrs = {})
|
32
|
+
if attrs.class == String
|
33
|
+
id = Mongo::ObjectID.from_string(attrs[0])
|
34
|
+
|
35
|
+
return self.collection.find_one(id)
|
36
|
+
else
|
37
|
+
attrs = self.scope.merge(attrs) if self.scope
|
38
|
+
|
39
|
+
ret = []
|
40
|
+
|
41
|
+
self.collection.find(attrs).to_a.map do |obj|
|
42
|
+
model = eval(self.name || @name).new
|
43
|
+
|
44
|
+
obj.each {|key, value| model.set_var(key, value) }
|
45
|
+
|
46
|
+
ret.push model
|
47
|
+
end
|
48
|
+
|
49
|
+
return ret
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.extended(klass)
|
54
|
+
class << klass
|
55
|
+
alias __old_new new
|
56
|
+
def new(*args, &blk)
|
57
|
+
__old_new(*args, &blk).freeze
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
self.extended(self)
|
63
|
+
|
64
|
+
def self.new(*attrs)
|
65
|
+
attrs = attrs[0]
|
66
|
+
|
67
|
+
if self.scope
|
68
|
+
attrs ||= {}
|
69
|
+
attrs.merge!(self.scope)
|
70
|
+
end
|
71
|
+
|
72
|
+
eval(self.name || @name).__old_new(attrs)
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ActiveMongo
|
2
|
+
module HasMany
|
3
|
+
module ClassMethods
|
4
|
+
def has_many(name, attrs = {})
|
5
|
+
internal_has_manies_set(name, attrs)
|
6
|
+
end
|
7
|
+
|
8
|
+
def attr_accessible(*input)
|
9
|
+
@@internal_attr_accessible = []
|
10
|
+
|
11
|
+
input.each do |field|
|
12
|
+
@@internal_attr_accessible.push(field.to_sym).uniq!
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def internal_has_manies_set(name, attrs)
|
17
|
+
@@internal_has_manies ||= {}
|
18
|
+
|
19
|
+
@@internal_has_manies[name.to_sym] = attrs if @@internal_has_manies[name].nil?
|
20
|
+
end
|
21
|
+
|
22
|
+
def internal_has_manies_get(name)
|
23
|
+
@@internal_has_manies ||= {}
|
24
|
+
|
25
|
+
@@internal_has_manies[name.to_sym]
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def attr_accessible_get
|
30
|
+
@@internal_attr_accessible || []
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
module InstanceMethods
|
36
|
+
def has_many_hit(name, attrs)
|
37
|
+
return false if self.new_record?
|
38
|
+
|
39
|
+
attrs[:class_name] ||= name.to_s.classify
|
40
|
+
attrs[:foreign_key] ||= name.to_s.singularize.underscore+"_id"
|
41
|
+
|
42
|
+
return eval(attrs[:class_name]).with_scope(attrs[:foreign_key] => self._id )
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
ActiveMongo::Base.class_eval do
|
2
|
+
def initialize(*attr)
|
3
|
+
@vars = []
|
4
|
+
|
5
|
+
attrs = attr[0] || {}
|
6
|
+
|
7
|
+
self.class.attr_accessible_get.each do |field|
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
if self.class.attr_accessible_get.any?
|
12
|
+
attrs.delete_if {|field| !self.class.attr_accessible_get.include?(field.to_sym) }
|
13
|
+
end
|
14
|
+
|
15
|
+
attrs.merge!(self.class.scope) if self.class.scope
|
16
|
+
|
17
|
+
attrs.each do |key, value|
|
18
|
+
self.set_var(key, value)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_json
|
23
|
+
to_hash.to_json
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_hash
|
27
|
+
h = {}
|
28
|
+
|
29
|
+
@vars.each do |var|
|
30
|
+
h[var] = instance_variable_get("@#{var}")
|
31
|
+
end
|
32
|
+
|
33
|
+
return h
|
34
|
+
end
|
35
|
+
|
36
|
+
def save
|
37
|
+
return false if !self.valid?
|
38
|
+
|
39
|
+
id = self.class.collection.save(self.to_hash)
|
40
|
+
|
41
|
+
self.set_var("_id", id) if self._id.nil?
|
42
|
+
|
43
|
+
return true
|
44
|
+
end
|
45
|
+
|
46
|
+
def destroy
|
47
|
+
return true if self.new_record?
|
48
|
+
|
49
|
+
self.class.destroy_all :_id => self._id
|
50
|
+
end
|
51
|
+
|
52
|
+
def new_record?
|
53
|
+
@_id.nil?
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_var(var, val)
|
57
|
+
if var.to_s == "_id" && val.class != Mongo::ObjectID
|
58
|
+
val = Mongo::ObjectID.from_string(val)
|
59
|
+
end
|
60
|
+
|
61
|
+
instance_variable_set("@#{var}", val)
|
62
|
+
@vars.push(var.to_sym).uniq!
|
63
|
+
end
|
64
|
+
|
65
|
+
def get_var(var)
|
66
|
+
instance_variable_get("@#{var}")
|
67
|
+
end
|
68
|
+
|
69
|
+
def update_attributes(*attrs)
|
70
|
+
attrs = attrs[0]
|
71
|
+
if self.class.attr_accessible_get.any?
|
72
|
+
attrs.delete_if {|field| !self.class.attr_accessible_get.include?(field.to_sym) }
|
73
|
+
end
|
74
|
+
|
75
|
+
attrs.each do |key, value|
|
76
|
+
self.set_var(key, value)
|
77
|
+
end
|
78
|
+
|
79
|
+
return self
|
80
|
+
end
|
81
|
+
|
82
|
+
def unset(var)
|
83
|
+
return false if self.new_record?
|
84
|
+
|
85
|
+
hash = self.class.collection.find_one self._id
|
86
|
+
|
87
|
+
hash.delete(var)
|
88
|
+
|
89
|
+
self.class.collection.save(hash)
|
90
|
+
|
91
|
+
self.set_value(var, nil)
|
92
|
+
|
93
|
+
@vars.delete var.to_sym
|
94
|
+
|
95
|
+
true
|
96
|
+
end
|
97
|
+
|
98
|
+
def method_missing(m, *attrs, &block)
|
99
|
+
if m.to_s.match(/\=$/) && attrs.length > 0
|
100
|
+
var_name = m.to_s.sub /\=$/, ""
|
101
|
+
|
102
|
+
self.set_var(var_name, attrs[0])
|
103
|
+
elsif self.class.internal_has_manies_get(m.to_sym)
|
104
|
+
return self.has_many_hit(m, self.class.internal_has_manies_get(m) )
|
105
|
+
elsif @vars.include? m.to_sym
|
106
|
+
begin
|
107
|
+
var = self.get_var(m)
|
108
|
+
error = false
|
109
|
+
rescue
|
110
|
+
error = true
|
111
|
+
end
|
112
|
+
|
113
|
+
return var unless error
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
ActiveMongo::Base.class_eval do
|
2
|
+
def self.with_scope(attrs)
|
3
|
+
@name = (self.name || @name)
|
4
|
+
cloned = self.clone.with_scope_set(attrs)
|
5
|
+
|
6
|
+
return cloned
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.with_scope_set(attrs)
|
10
|
+
@scope ||= {}
|
11
|
+
|
12
|
+
@scope.merge! attrs
|
13
|
+
|
14
|
+
return self
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_support/core_ext/object/blank'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
class UniquenessValidator < EachValidator
|
6
|
+
def validate(record)
|
7
|
+
attribute = attributes[0]
|
8
|
+
|
9
|
+
query = {}
|
10
|
+
query[:_id] = {"$ne" => record._id}
|
11
|
+
query[attribute] = record.get_var(attribute)
|
12
|
+
|
13
|
+
options[:scope].each do |scope|
|
14
|
+
query[scope] = record.get_var(scope)
|
15
|
+
end
|
16
|
+
|
17
|
+
int = record.class.count( query )
|
18
|
+
|
19
|
+
record.errors.add(attribute, options[:message]) if int > 0
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
|
25
|
+
def validates_uniqueness_of(*attr_names)
|
26
|
+
options = _merge_attributes(attr_names)
|
27
|
+
options[:message] ||= "is taken"
|
28
|
+
options[:scope] ||= []
|
29
|
+
validates_with UniquenessValidator, options
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_mongo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mantas Masalskis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-10 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Schemaless Mongo ORM for Rails 3.0
|
26
|
+
email: mantas@idev.lt
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- active_mongo.gemspec
|
42
|
+
- lib/README
|
43
|
+
- lib/active_mongo.rb
|
44
|
+
- lib/active_mongo_collection.rb
|
45
|
+
- lib/active_mongo_has_many.rb
|
46
|
+
- lib/active_mongo_instance.rb
|
47
|
+
- lib/active_mongo_scope.rb
|
48
|
+
- lib/active_mongo_uniquenesss.rb
|
49
|
+
- test/helper.rb
|
50
|
+
- test/test_active_mongo.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/mantas/active_mongo
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.3.5
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Schemaless Mongo ORM for Rails 3.0
|
79
|
+
test_files:
|
80
|
+
- test/helper.rb
|
81
|
+
- test/test_active_mongo.rb
|