forked from mongomapper/mongomapper
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some house cleaning. Reorganized stuff a bit.
- Loading branch information
1 parent
04e1b7b
commit dbadd97
Showing
12 changed files
with
176 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.