Module: Mongo::DocumentClass

Defined in:
lib/mongo/document.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

For other methods on collection(or not), just send to it.



35
36
37
# File 'lib/mongo/document.rb', line 35

def method_missing(name, *args)
  collection.send(name, *args)
end

Instance Method Details

#clientObject Also known as: connection



9
10
11
12
13
14
# File 'lib/mongo/document.rb', line 9

def client
  if !@client
    raise "No connection! You should call Mongo::Document.establish_connection before."
  end
  @client
end

#collectionObject



17
18
19
# File 'lib/mongo/document.rb', line 17

def collection
  @collection ||= client[collection_name]
end

#collection_nameObject



21
22
23
# File 'lib/mongo/document.rb', line 21

def collection_name
  @collection_name ||= name.underscore.pluralize
end

#collection_name=(name) ⇒ Object



25
26
27
# File 'lib/mongo/document.rb', line 25

def collection_name=(name)
  @collection_name = name
end

#find(*args) ⇒ Object

A little optimazation for the most frequently-used method.



30
31
32
# File 'lib/mongo/document.rb', line 30

def find(*args)
  collection.find(*args)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/mongo/document.rb', line 39

def respond_to_missing?(name, include_private = false)
  collection.respond_to?(name, include_private) || super
end