Skip to content

Commit

Permalink
Some house cleaning. Reorganized stuff a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Feb 4, 2010
1 parent 04e1b7b commit dbadd97
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 172 deletions.
18 changes: 0 additions & 18 deletions lib/mongo_mapper.rb
Expand Up @@ -102,25 +102,7 @@ def self.normalize_object_id(value)

require 'mongo_mapper/support'
require 'mongo_mapper/finder_options'
require 'mongo_mapper/dynamic_finder'
require 'mongo_mapper/descendant_appends'

require 'mongo_mapper/plugins'
require 'mongo_mapper/plugins/associations'
require 'mongo_mapper/plugins/callbacks'
require 'mongo_mapper/plugins/clone'
require 'mongo_mapper/plugins/descendants'
require 'mongo_mapper/plugins/dirty'
require 'mongo_mapper/plugins/equality'
require 'mongo_mapper/plugins/identity_map'
require 'mongo_mapper/plugins/inspect'
require 'mongo_mapper/plugins/keys'
require 'mongo_mapper/plugins/logger'
require 'mongo_mapper/plugins/pagination'
require 'mongo_mapper/plugins/protected'
require 'mongo_mapper/plugins/rails'
require 'mongo_mapper/plugins/serialization'
require 'mongo_mapper/plugins/validations'

require 'mongo_mapper/document'
require 'mongo_mapper/embedded_document'
44 changes: 0 additions & 44 deletions lib/mongo_mapper/descendant_appends.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/mongo_mapper/document.rb
@@ -1,12 +1,12 @@
module MongoMapper
module Document
extend DescendantAppends
extend Support::DescendantAppends

def self.included(model)
model.class_eval do
include InstanceMethods
extend ClassMethods
extend Finders
extend Support::Find
extend Plugins

plugin Plugins::Associations
Expand Down
74 changes: 0 additions & 74 deletions lib/mongo_mapper/dynamic_finder.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/mongo_mapper/embedded_document.rb
@@ -1,6 +1,6 @@
module MongoMapper
module EmbeddedDocument
extend DescendantAppends
extend Support::DescendantAppends

def self.included(model)
model.class_eval do
Expand Down
18 changes: 17 additions & 1 deletion lib/mongo_mapper/plugins.rb
Expand Up @@ -11,4 +11,20 @@ def plugin(mod)
plugins << mod
end
end
end
end

require 'mongo_mapper/plugins/associations'
require 'mongo_mapper/plugins/callbacks'
require 'mongo_mapper/plugins/clone'
require 'mongo_mapper/plugins/descendants'
require 'mongo_mapper/plugins/dirty'
require 'mongo_mapper/plugins/equality'
require 'mongo_mapper/plugins/identity_map'
require 'mongo_mapper/plugins/inspect'
require 'mongo_mapper/plugins/keys'
require 'mongo_mapper/plugins/logger'
require 'mongo_mapper/plugins/pagination'
require 'mongo_mapper/plugins/protected'
require 'mongo_mapper/plugins/rails'
require 'mongo_mapper/plugins/serialization'
require 'mongo_mapper/plugins/validations'
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/associations/in_array_proxy.rb
Expand Up @@ -2,7 +2,7 @@ module MongoMapper
module Plugins
module Associations
class InArrayProxy < Collection
include ::MongoMapper::Finders
include Support::Find

def find(*args)
options = args.extract_options!
Expand Down
Expand Up @@ -2,7 +2,7 @@ module MongoMapper
module Plugins
module Associations
class ManyDocumentsProxy < Collection
include ::MongoMapper::Finders
include Support::Find

def find(*args)
options = args.extract_options!
Expand Down
5 changes: 4 additions & 1 deletion lib/mongo_mapper/support.rb
Expand Up @@ -211,4 +211,7 @@ class Mongo::ObjectID
def to_json(options = nil)
%Q("#{to_s}")
end
end
end

require 'mongo_mapper/support/descendant_appends'
require 'mongo_mapper/support/find'
46 changes: 46 additions & 0 deletions lib/mongo_mapper/support/descendant_appends.rb
@@ -0,0 +1,46 @@
module MongoMapper
module Support
module DescendantAppends
def included(model)
extra_extensions.each { |extension| model.extend(extension) }
extra_inclusions.each { |inclusion| model.send(:include, inclusion) }
descendants << model
end

# @api public
def descendants
@descendants ||= Set.new
end

# @api public
def append_extensions(*extensions)
extra_extensions.concat extensions

# Add the extension to existing descendants
descendants.each do |model|
extensions.each { |extension| model.extend(extension) }
end
end

# @api public
def append_inclusions(*inclusions)
extra_inclusions.concat inclusions

# Add the inclusion to existing descendants
descendants.each do |model|
inclusions.each { |inclusion| model.send(:include, inclusion) }
end
end

# @api private
def extra_extensions
@extra_extensions ||= []
end

# @api private
def extra_inclusions
@extra_inclusions ||= []
end
end
end
end
77 changes: 77 additions & 0 deletions lib/mongo_mapper/support/find.rb
@@ -0,0 +1,77 @@
module MongoMapper
module Support
# @api private
module Find
def dynamic_find(finder, args)
attributes = {}

finder.attributes.each_with_index do |attr, index|
attributes[attr] = args[index]
end

options = args.extract_options!.merge(attributes)

if result = send(finder.finder, options)
result
else
if finder.raise?
raise DocumentNotFound, "Couldn't find Document with #{attributes.inspect} in collection named #{collection.name}"
end

if finder.instantiator
self.send(finder.instantiator, attributes)
end
end
end

class DynamicFinder
attr_reader :method, :attributes, :finder, :bang, :instantiator

def initialize(method)
@method = method
@finder = :first
@bang = false
match
end

def found?
@finder.present?
end

def raise?
bang == true
end

protected
def match
case method.to_s
when /^find_(all_by|by)_([_a-zA-Z]\w*)$/
@finder = :all if $1 == 'all_by'
names = $2
when /^find_by_([_a-zA-Z]\w*)\!$/
@bang = true
names = $1
when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
@instantiator = $1 == 'initialize' ? :new : :create
names = $2
else
@finder = nil
end

@attributes = names && names.split('_and_')
end
end

protected
def method_missing(method, *args, &block)
finder = DynamicFinder.new(method)

if finder.found?
dynamic_find(finder, args)
else
super
end
end
end
end
end

0 comments on commit dbadd97

Please sign in to comment.