Module: Candy

Defined in:
lib/candy/exceptions.rb,
lib/candy.rb,
lib/candy/hash.rb,
lib/candy/array.rb,
lib/candy/piece.rb,
lib/candy/crunch.rb,
lib/candy/wrapper.rb,
lib/candy/factory.rb,
lib/candy/collection.rb,
lib/candy/embeddable.rb,
lib/candy/crunch/document.rb

Overview

encoding: utf-8

Defined Under Namespace

Modules: Collection, Crunch, Embeddable, Factory, Piece, Wrapper Classes: CandyArray, CandyError, CandyHash, ConnectionError, TypeError

Constant Summary

CLASS_KEY =

Special keys for Candy metadata in the Mongo store. We try to keep these to a minimum, and we're using moderately obscure Unicode symbols to reduce the odds of collisions. If by some strange happenstance you might have single-character keys in your Mongo collections that use the 'CIRCLED LATIN SMALL LETTER' Unicode set, you may need to change these constants. Just be consistent about it if you want to use embedded documents in Candy.

''.to_sym
EMBED_KEY =
''.to_sym

Class Method Summary (collapse)

Class Method Details

+ (Object) connection

Returns the connection you gave, or creates a default connection to the default host and port.



49
50
51
# File 'lib/candy/crunch.rb', line 49

def self.connection
  @connection ||= Mongo::Connection.new(host, port, connection_options)
end

+ (Object) connection=(val)

First clears any collection and database we're talking to, then accepts a connection you provide. You're responsible for your own host, port and options if you use this.



43
44
45
46
# File 'lib/candy/crunch.rb', line 43

def self.connection=(val)
  self.db = nil
  @connection = val
end

+ (Object) connection_options

A hash passed to the default connection. See the Mongo::Connection documentation for valid options.



37
38
39
# File 'lib/candy/crunch.rb', line 37

def self.connection_options
  @connection_options ||= {}
end

+ (Object) connection_options=(val)

Overrides the options hash and resets the connection, db, and collection.



21
22
23
24
# File 'lib/candy/crunch.rb', line 21

def self.connection_options=(val)
  @connection = nil
  @connection_options = val
end

+ (Object) db

Returns the database you gave, or creates a default database named for your username (or 'candy' if it can't find a username).



71
72
73
# File 'lib/candy/crunch.rb', line 71

def self.db
  @db ||= maybe_authenticate(Mongo::DB.new(Etc.getlogin || 'candy', connection, :strict => false))
end

+ (Object) db=(val)

Accepts a database you provide. You can provide a Mongo::DB object or a string with the database name. If you provide a Mongo::DB object, the default connection is not used, and the :strict flag should be false or default collection lookup will fail.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/candy/crunch.rb', line 56

def self.db=(val)
  case val
  when Mongo::DB
    @db = val
  when String
    @db = maybe_authenticate(Mongo::DB.new(val, connection))
  when nil
    @db = nil
  else 
    raise ConnectionError, "The db attribute needs a Mongo::DB object or a name string."
  end
end

+ (Object) host

Passed to the default connection. If not set, Mongo's default of localhost will be used.



27
28
29
# File 'lib/candy/crunch.rb', line 27

def self.host
  @host
end

+ (Object) host=(val)

Overrides the host and resets the connection, db, and collection.



9
10
11
12
# File 'lib/candy/crunch.rb', line 9

def self.host=(val)
  @connection = nil
  @host = val
end

+ (Object) password

The password for Mongo authentication



93
94
95
# File 'lib/candy/crunch.rb', line 93

def self.password
  @password
end

+ (Object) password=(val)

Sets the password for Mongo authentication. Both username AND password must be set or nothing will happen. Also ignored if you supply your own Mongo::DB object instead of a string.



88
89
90
# File 'lib/candy/crunch.rb', line 88

def self.password=(val)
  @password = val
end

+ (Object) port

Passed to the default connection. If not set, Mongo's default of 27017 will be used.



32
33
34
# File 'lib/candy/crunch.rb', line 32

def self.port
  @port
end

+ (Object) port=(val)

Overrides the port and resets the connection, db, and collection.



15
16
17
18
# File 'lib/candy/crunch.rb', line 15

def self.port=(val)
  @connection = nil
  @port = val
end

+ (Object) username

The user for Mongo authentication.



82
83
84
# File 'lib/candy/crunch.rb', line 82

def self.username
  @username
end

+ (Object) username=(val)

Sets the user for Mongo authentication. Both username AND password must be set or nothing will happen. Also ignored if you supply your own Mongo::DB object instead of a string.



77
78
79
# File 'lib/candy/crunch.rb', line 77

def self.username=(val)
  @username = val
end