Module: Candy

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

Defined Under Namespace

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

Constant Summary collapse

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

.connectionObject

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

.connection=(val) ⇒ Object

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

.connection_optionsObject

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

.connection_options=(val) ⇒ Object

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

.dbObject

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

.db=(val) ⇒ Object

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

.hostObject

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

.host=(val) ⇒ Object

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

.passwordObject

The password for Mongo authentication



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

def self.password
  @password
end

.password=(val) ⇒ Object

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

.portObject

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

.port=(val) ⇒ Object

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

.usernameObject

The user for Mongo authentication.



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

def self.username
  @username
end

.username=(val) ⇒ Object

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