active_mongo 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +38 -6
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/active_mongo.gemspec +4 -5
- data/lib/active_mongo.rb +17 -0
- data/lib/active_mongo_attr_accessible.rb +34 -0
- data/lib/active_mongo_collection.rb +19 -8
- data/lib/active_mongo_has_many.rb +0 -13
- data/lib/active_mongo_indexes.rb +17 -0
- data/lib/active_mongo_instance.rb +45 -3
- data/lib/active_mongo_named_scopes.rb +25 -0
- metadata +6 -13
- data/lib/README +0 -3
data/README.textile
CHANGED
@@ -11,8 +11,8 @@ h2. Installation
|
|
11
11
|
# Add to your Rails application Gemfile
|
12
12
|
# Run gem bundle
|
13
13
|
# Load gem. For example, create initializer that says "require 'active_mongo'"
|
14
|
-
# Create config/mongo.yml. Format is the same as database.yml. Database, host and port are required
|
15
|
-
# Create a model, which is a subclass of
|
14
|
+
# Create config/mongo.yml. Format is the same as database.yml. Database, host and port are required. User and password are optional
|
15
|
+
# Create a model, which is a subclass of ActiveMongo::Base
|
16
16
|
|
17
17
|
h2. Usage
|
18
18
|
|
@@ -78,13 +78,45 @@ You can limit what may be assigned by passing a hash to #new or #update_attribut
|
|
78
78
|
attr_accessible :attribute
|
79
79
|
</code></pre>
|
80
80
|
|
81
|
+
h3. Do not save specific field to database
|
82
|
+
|
83
|
+
<pre><code>
|
84
|
+
attr_clear :attribute
|
85
|
+
</code></pre>
|
86
|
+
|
87
|
+
h3. Indexes
|
88
|
+
|
89
|
+
<pre><code>
|
90
|
+
ensure_index :attribute
|
91
|
+
ensure_index :attribute, :unique => true
|
92
|
+
ensure_index [ [:attribute, Mongo::ASCENDING] ], :unique => true # takes mongo-driver syntax
|
93
|
+
</code></pre>
|
94
|
+
|
95
|
+
h3. Callbacks
|
96
|
+
|
97
|
+
after initialize and before/after/around create, update and save callbacks are supported
|
98
|
+
|
99
|
+
<pre><code>
|
100
|
+
after_save :method
|
101
|
+
</code></pre>
|
102
|
+
|
103
|
+
h4. Named Scopes
|
104
|
+
|
105
|
+
<pre><code>
|
106
|
+
#in model
|
107
|
+
named_scope :with_value, :attribute => :value #all mongo_driver find() parameters accepted
|
108
|
+
|
109
|
+
#in code
|
110
|
+
Model.with_value #returns scoped class
|
111
|
+
Model.with_value.find() #run find with scope
|
112
|
+
Model.with_value.new() #initialize an object with scope
|
113
|
+
</code></pre>
|
114
|
+
|
81
115
|
h2. Coming soon
|
82
116
|
|
83
|
-
* Named scopes
|
84
|
-
* Callbacks
|
85
|
-
* Tests
|
86
|
-
* Mongo authentication support
|
87
117
|
* Documentation
|
118
|
+
* Tests
|
119
|
+
* Even more goodies
|
88
120
|
|
89
121
|
h2. License
|
90
122
|
|
data/Rakefile
CHANGED
@@ -10,7 +10,7 @@ begin
|
|
10
10
|
gem.email = "mantas@idev.lt"
|
11
11
|
gem.homepage = "http://github.com/mantas/ActiveMongo"
|
12
12
|
gem.authors = ["Mantas Masalskis"]
|
13
|
-
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
13
|
+
#gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
14
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
15
|
end
|
16
16
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/active_mongo.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{active_mongo}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Mantas Masalskis"]
|
@@ -24,11 +24,13 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"active_mongo.gemspec",
|
27
|
-
"lib/README",
|
28
27
|
"lib/active_mongo.rb",
|
28
|
+
"lib/active_mongo_attr_accessible.rb",
|
29
29
|
"lib/active_mongo_collection.rb",
|
30
30
|
"lib/active_mongo_has_many.rb",
|
31
|
+
"lib/active_mongo_indexes.rb",
|
31
32
|
"lib/active_mongo_instance.rb",
|
33
|
+
"lib/active_mongo_named_scopes.rb",
|
32
34
|
"lib/active_mongo_scope.rb",
|
33
35
|
"lib/active_mongo_uniquenesss.rb",
|
34
36
|
"test/helper.rb",
|
@@ -49,12 +51,9 @@ Gem::Specification.new do |s|
|
|
49
51
|
s.specification_version = 3
|
50
52
|
|
51
53
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
52
|
-
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
53
54
|
else
|
54
|
-
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
55
55
|
end
|
56
56
|
else
|
57
|
-
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
58
57
|
end
|
59
58
|
end
|
60
59
|
|
data/lib/active_mongo.rb
CHANGED
@@ -7,16 +7,33 @@ config = YAML::load(File.open("#{RAILS_ROOT}/config/mongo.yml"))[Rails.env]
|
|
7
7
|
$mongo_conn = Connection.new(config["host"], config["port"], :pool_size => 5, :timeout => 5)
|
8
8
|
$mongo_db = $mongo_conn.db(config["database"])
|
9
9
|
|
10
|
+
if config["user"]
|
11
|
+
if !$mongo_db.authenticate(config["user"], config["password"])
|
12
|
+
puts "Wrong MongoDB user and/or password!!!"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
10
16
|
require 'active_mongo_has_many'
|
17
|
+
require 'active_mongo_attr_accessible'
|
18
|
+
require 'active_mongo_indexes'
|
19
|
+
require 'active_mongo_named_scopes'
|
11
20
|
|
12
21
|
module ActiveMongo
|
13
22
|
|
14
23
|
class Base
|
15
24
|
include ActiveModel::Conversion
|
16
25
|
include ActiveModel::Validations
|
26
|
+
include ActiveModel::Callbacks
|
27
|
+
extend ActiveModel::Callbacks
|
28
|
+
extend ActiveMongo::Indexes::ClassMethods
|
29
|
+
extend ActiveMongo::AttrAccessible::ClassMethods
|
30
|
+
extend ActiveMongo::NamedScopes::ClassMethods
|
17
31
|
extend ActiveMongo::HasMany::ClassMethods
|
18
32
|
include ActiveMongo::HasMany::InstanceMethods
|
19
33
|
|
34
|
+
define_model_callbacks :create, :save, :update
|
35
|
+
define_model_callbacks :initializer, :only => :after
|
36
|
+
|
20
37
|
class << self; attr_accessor :scope; end
|
21
38
|
|
22
39
|
def self.extended(klass)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ActiveMongo
|
2
|
+
module AttrAccessible
|
3
|
+
module ClassMethods
|
4
|
+
def attr_accessible(*input)
|
5
|
+
@@internal_attr_accessible = []
|
6
|
+
|
7
|
+
input.each do |field|
|
8
|
+
@@internal_attr_accessible.push(field.to_sym).uniq!
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def attr_accessible_get
|
13
|
+
@@internal_attr_accessible || []
|
14
|
+
end
|
15
|
+
|
16
|
+
def attr_clear(*input)
|
17
|
+
@@internal_attr_clear = []
|
18
|
+
|
19
|
+
input.each do |field|
|
20
|
+
@@internal_attr_clear.push(field.to_sym).uniq!
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def attr_accessible_get
|
25
|
+
@@internal_attr_accessible || []
|
26
|
+
end
|
27
|
+
|
28
|
+
def attr_clear_get
|
29
|
+
@@internal_attr_clear || []
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -29,10 +29,18 @@ ActiveMongo::Base.class_eval do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def self.find(attrs = {})
|
32
|
-
if attrs.class == String
|
33
|
-
id = Mongo::ObjectID.from_string(attrs
|
32
|
+
if attrs.class == String || attrs.class == Mongo::ObjectID
|
33
|
+
id = Mongo::ObjectID.from_string(attrs) if attrs.class == String
|
34
34
|
|
35
|
-
|
35
|
+
id ||= attrs
|
36
|
+
|
37
|
+
obj = self.collection.find_one(id)
|
38
|
+
|
39
|
+
model = eval(self.name || @name).new
|
40
|
+
|
41
|
+
obj.each {|key, value| model.set_var(key, value) }
|
42
|
+
|
43
|
+
return model
|
36
44
|
else
|
37
45
|
attrs = self.scope.merge(attrs) if self.scope
|
38
46
|
|
@@ -61,11 +69,14 @@ ActiveMongo::Base.class_eval do
|
|
61
69
|
def self.new(*attrs)
|
62
70
|
attrs = attrs[0]
|
63
71
|
|
64
|
-
# if self.scope
|
65
|
-
# attrs ||= {}
|
66
|
-
# attrs.merge!(self.scope)
|
67
|
-
# end
|
68
|
-
|
69
72
|
eval(self.name || @name).__old_new(attrs, :scope => self.scope)
|
70
73
|
end
|
74
|
+
|
75
|
+
def self.method_missing(m, *attrs, &block)
|
76
|
+
if self.internal_named_scopes_get(m)
|
77
|
+
return self.named_scope_hit(m)
|
78
|
+
end
|
79
|
+
|
80
|
+
super
|
81
|
+
end
|
71
82
|
end
|
@@ -5,14 +5,6 @@ module ActiveMongo
|
|
5
5
|
internal_has_manies_set(name, attrs)
|
6
6
|
end
|
7
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
8
|
def internal_has_manies_set(name, attrs)
|
17
9
|
@@internal_has_manies ||= {}
|
18
10
|
|
@@ -25,11 +17,6 @@ module ActiveMongo
|
|
25
17
|
@@internal_has_manies[name.to_sym]
|
26
18
|
end
|
27
19
|
|
28
|
-
|
29
|
-
def attr_accessible_get
|
30
|
-
@@internal_attr_accessible || []
|
31
|
-
end
|
32
|
-
|
33
20
|
end
|
34
21
|
|
35
22
|
module InstanceMethods
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActiveMongo
|
2
|
+
module Indexes
|
3
|
+
module ClassMethods
|
4
|
+
def ensure_index(*attr)
|
5
|
+
fields = attr[0]
|
6
|
+
|
7
|
+
options = attr[1]
|
8
|
+
unique = options[:unique] if options.class == Hash
|
9
|
+
|
10
|
+
unique ||= false
|
11
|
+
|
12
|
+
self.collection.create_index fields, :unique => unique
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -13,6 +13,8 @@ ActiveMongo::Base.class_eval do
|
|
13
13
|
attrs.each do |key, value|
|
14
14
|
self.set_var(key, value)
|
15
15
|
end
|
16
|
+
|
17
|
+
_run_initializer_callbacks :after
|
16
18
|
end
|
17
19
|
|
18
20
|
def to_json
|
@@ -30,11 +32,51 @@ ActiveMongo::Base.class_eval do
|
|
30
32
|
end
|
31
33
|
|
32
34
|
def save(do_validate = true)
|
33
|
-
return false if do_validate && !self.valid?
|
34
35
|
|
35
|
-
|
36
|
+
def do_save
|
37
|
+
hash = self.to_hash
|
38
|
+
|
39
|
+
if self.class.attr_clear_get.any?
|
40
|
+
hash.delete_if {|key, value| self.class.attr_clear_get.include?(key.to_sym) }
|
41
|
+
end
|
42
|
+
|
43
|
+
id = self.class.collection.save(hash)
|
44
|
+
|
45
|
+
self.set_var("_id", id) if self._id.nil?
|
46
|
+
end
|
36
47
|
|
37
|
-
|
48
|
+
if self.new_record?
|
49
|
+
|
50
|
+
_run_create_callbacks do
|
51
|
+
_run_save_callbacks do
|
52
|
+
|
53
|
+
if !do_validate || self.valid?
|
54
|
+
|
55
|
+
do_save
|
56
|
+
|
57
|
+
else
|
58
|
+
return false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
else
|
64
|
+
|
65
|
+
_run_update_callbacks do
|
66
|
+
_run_save_callbacks do
|
67
|
+
|
68
|
+
if !do_validate || self.valid?
|
69
|
+
|
70
|
+
do_save
|
71
|
+
|
72
|
+
else
|
73
|
+
return false
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
end
|
38
80
|
|
39
81
|
return true
|
40
82
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ActiveMongo
|
2
|
+
module NamedScopes
|
3
|
+
module ClassMethods
|
4
|
+
def named_scope(name, attrs = {})
|
5
|
+
internal_named_scopes_set(name, attrs)
|
6
|
+
end
|
7
|
+
|
8
|
+
def internal_named_scopes_set(name, attrs)
|
9
|
+
@@internal_named_scopes ||= {}
|
10
|
+
|
11
|
+
@@internal_named_scopes[name.to_sym] = attrs if @@internal_named_scopes[name].nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
def internal_named_scopes_get(name)
|
15
|
+
@@internal_named_scopes ||= {}
|
16
|
+
|
17
|
+
@@internal_named_scopes[name.to_sym]
|
18
|
+
end
|
19
|
+
|
20
|
+
def named_scope_hit(name)
|
21
|
+
return eval(self.name).with_scope( @@internal_named_scopes[name] )
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_mongo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mantas Masalskis
|
@@ -11,17 +11,8 @@ cert_chain: []
|
|
11
11
|
|
12
12
|
date: 2010-01-10 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
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:
|
14
|
+
dependencies: []
|
15
|
+
|
25
16
|
description: Schemaless Mongo ORM for Rails 3.0
|
26
17
|
email: mantas@idev.lt
|
27
18
|
executables: []
|
@@ -39,11 +30,13 @@ files:
|
|
39
30
|
- Rakefile
|
40
31
|
- VERSION
|
41
32
|
- active_mongo.gemspec
|
42
|
-
- lib/README
|
43
33
|
- lib/active_mongo.rb
|
34
|
+
- lib/active_mongo_attr_accessible.rb
|
44
35
|
- lib/active_mongo_collection.rb
|
45
36
|
- lib/active_mongo_has_many.rb
|
37
|
+
- lib/active_mongo_indexes.rb
|
46
38
|
- lib/active_mongo_instance.rb
|
39
|
+
- lib/active_mongo_named_scopes.rb
|
47
40
|
- lib/active_mongo_scope.rb
|
48
41
|
- lib/active_mongo_uniquenesss.rb
|
49
42
|
- test/helper.rb
|
data/lib/README
DELETED