Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Callbacks, named scopes and more...

  • Loading branch information...
commit c840b9aa6509a0da47443e890350c7b02e3b0ea3 1 parent 7b62c01
@mantas authored
View
44 README.textile
@@ -11,8 +11,8 @@ h2. Installation
# Add to your Rails application Gemfile
# Run gem bundle
# Load gem. For example, create initializer that says "require 'active_mongo'"
-# Create config/mongo.yml. Format is the same as database.yml. Database, host and port are required
-# Create a model, which is a subclass of ActiveMonog::Base
+# Create config/mongo.yml. Format is the same as database.yml. Database, host and port are required. User and password are optional
+# Create a model, which is a subclass of ActiveMongo::Base
h2. Usage
@@ -78,13 +78,45 @@ You can limit what may be assigned by passing a hash to #new or #update_attribut
attr_accessible :attribute
</code></pre>
+h3. Do not save specific field to database
+
+<pre><code>
+ attr_clear :attribute
+</code></pre>
+
+h3. Indexes
+
+<pre><code>
+ ensure_index :attribute
+ ensure_index :attribute, :unique => true
+ ensure_index [ [:attribute, Mongo::ASCENDING] ], :unique => true # takes mongo-driver syntax
+</code></pre>
+
+h3. Callbacks
+
+after initialize and before/after/around create, update and save callbacks are supported
+
+<pre><code>
+ after_save :method
+</code></pre>
+
+h4. Named Scopes
+
+<pre><code>
+ #in model
+ named_scope :with_value, :attribute => :value #all mongo_driver find() parameters accepted
+
+ #in code
+ Model.with_value #returns scoped class
+ Model.with_value.find() #run find with scope
+ Model.with_value.new() #initialize an object with scope
+</code></pre>
+
h2. Coming soon
-* Named scopes
-* Callbacks
-* Tests
-* Mongo authentication support
* Documentation
+* Tests
+* Even more goodies
h2. License
View
2  Rakefile
@@ -10,7 +10,7 @@ begin
gem.email = "mantas@idev.lt"
gem.homepage = "http://github.com/mantas/ActiveMongo"
gem.authors = ["Mantas Masalskis"]
- gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
+ #gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
View
3  lib/README
@@ -1,3 +0,0 @@
-ActiveMongo is a Ruby on Rails 3.0 and Ruby 1.9 compatible totally schemaless ORM.
-
-It's currently in pre-Alpha state. Feel free to try it if you feel adventurous!
View
17 lib/active_mongo.rb
@@ -7,16 +7,33 @@
$mongo_conn = Connection.new(config["host"], config["port"], :pool_size => 5, :timeout => 5)
$mongo_db = $mongo_conn.db(config["database"])
+if config["user"]
+ if !$mongo_db.authenticate(config["user"], config["password"])
+ puts "Wrong MongoDB user and/or password!!!"
+ end
+end
+
require 'active_mongo_has_many'
+require 'active_mongo_attr_accessible'
+require 'active_mongo_indexes'
+require 'active_mongo_named_scopes'
module ActiveMongo
class Base
include ActiveModel::Conversion
include ActiveModel::Validations
+ include ActiveModel::Callbacks
+ extend ActiveModel::Callbacks
+ extend ActiveMongo::Indexes::ClassMethods
+ extend ActiveMongo::AttrAccessible::ClassMethods
+ extend ActiveMongo::NamedScopes::ClassMethods
extend ActiveMongo::HasMany::ClassMethods
include ActiveMongo::HasMany::InstanceMethods
+ define_model_callbacks :create, :save, :update
+ define_model_callbacks :initializer, :only => :after
+
class << self; attr_accessor :scope; end
def self.extended(klass)
View
34 lib/active_mongo_attr_accessible.rb
@@ -0,0 +1,34 @@
+module ActiveMongo
+ module AttrAccessible
+ module ClassMethods
+ def attr_accessible(*input)
+ @@internal_attr_accessible = []
+
+ input.each do |field|
+ @@internal_attr_accessible.push(field.to_sym).uniq!
+ end
+ end
+
+ def attr_accessible_get
+ @@internal_attr_accessible || []
+ end
+
+ def attr_clear(*input)
+ @@internal_attr_clear = []
+
+ input.each do |field|
+ @@internal_attr_clear.push(field.to_sym).uniq!
+ end
+ end
+
+ def attr_accessible_get
+ @@internal_attr_accessible || []
+ end
+
+ def attr_clear_get
+ @@internal_attr_clear || []
+ end
+
+ end
+ end
+end
View
27 lib/active_mongo_collection.rb
@@ -29,10 +29,18 @@ def self.destroy_all(attrs = {})
end
def self.find(attrs = {})
- if attrs.class == String
- id = Mongo::ObjectID.from_string(attrs[0])
+ if attrs.class == String || attrs.class == Mongo::ObjectID
+ id = Mongo::ObjectID.from_string(attrs) if attrs.class == String
- return self.collection.find_one(id)
+ id ||= attrs
+
+ obj = self.collection.find_one(id)
+
+ model = eval(self.name || @name).new
+
+ obj.each {|key, value| model.set_var(key, value) }
+
+ return model
else
attrs = self.scope.merge(attrs) if self.scope
@@ -61,11 +69,14 @@ class << klass
def self.new(*attrs)
attrs = attrs[0]
- # if self.scope
- # attrs ||= {}
- # attrs.merge!(self.scope)
- # end
-
eval(self.name || @name).__old_new(attrs, :scope => self.scope)
end
+
+ def self.method_missing(m, *attrs, &block)
+ if self.internal_named_scopes_get(m)
+ return self.named_scope_hit(m)
+ end
+
+ super
+ end
end
View
13 lib/active_mongo_has_many.rb
@@ -5,14 +5,6 @@ def has_many(name, attrs = {})
internal_has_manies_set(name, attrs)
end
- def attr_accessible(*input)
- @@internal_attr_accessible = []
-
- input.each do |field|
- @@internal_attr_accessible.push(field.to_sym).uniq!
- end
- end
-
def internal_has_manies_set(name, attrs)
@@internal_has_manies ||= {}
@@ -25,11 +17,6 @@ def internal_has_manies_get(name)
@@internal_has_manies[name.to_sym]
end
-
- def attr_accessible_get
- @@internal_attr_accessible || []
- end
-
end
module InstanceMethods
View
17 lib/active_mongo_indexes.rb
@@ -0,0 +1,17 @@
+module ActiveMongo
+ module Indexes
+ module ClassMethods
+ def ensure_index(*attr)
+ fields = attr[0]
+
+ options = attr[1]
+ unique = options[:unique] if options.class == Hash
+
+ unique ||= false
+
+ self.collection.create_index fields, :unique => unique
+ end
+
+ end
+ end
+end
View
48 lib/active_mongo_instance.rb
@@ -13,6 +13,8 @@ def initialize(*attr)
attrs.each do |key, value|
self.set_var(key, value)
end
+
+ _run_initializer_callbacks :after
end
def to_json
@@ -30,11 +32,51 @@ def to_hash
end
def save(do_validate = true)
- return false if do_validate && !self.valid?
- id = self.class.collection.save(self.to_hash)
+ def do_save
+ hash = self.to_hash
+
+ if self.class.attr_clear_get.any?
+ hash.delete_if {|key, value| self.class.attr_clear_get.include?(key.to_sym) }
+ end
+
+ id = self.class.collection.save(hash)
+
+ self.set_var("_id", id) if self._id.nil?
+ end
- self.set_var("_id", id) if self._id.nil?
+ if self.new_record?
+
+ _run_create_callbacks do
+ _run_save_callbacks do
+
+ if !do_validate || self.valid?
+
+ do_save
+
+ else
+ return false
+ end
+ end
+ end
+
+ else
+
+ _run_update_callbacks do
+ _run_save_callbacks do
+
+ if !do_validate || self.valid?
+
+ do_save
+
+ else
+ return false
+ end
+ end
+ end
+
+
+ end
return true
end
View
25 lib/active_mongo_named_scopes.rb
@@ -0,0 +1,25 @@
+module ActiveMongo
+ module NamedScopes
+ module ClassMethods
+ def named_scope(name, attrs = {})
+ internal_named_scopes_set(name, attrs)
+ end
+
+ def internal_named_scopes_set(name, attrs)
+ @@internal_named_scopes ||= {}
+
+ @@internal_named_scopes[name.to_sym] = attrs if @@internal_named_scopes[name].nil?
+ end
+
+ def internal_named_scopes_get(name)
+ @@internal_named_scopes ||= {}
+
+ @@internal_named_scopes[name.to_sym]
+ end
+
+ def named_scope_hit(name)
+ return eval(self.name).with_scope( @@internal_named_scopes[name] )
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.