Class: Sinatra::Monk::MHash

Inherits:
MBase
  • Object
show all
Defined in:
lib/sinatra-monk/2.0.rb,
lib/sinatra-monk/monk.rb

Overview

A type of Monk that acts like a Hash, it was made to look very native. It still need some improvement.

Instance Attribute Summary collapse

Attributes inherited from MBase

#client, #database, #password, #username

Instance Method Summary collapse

Methods inherited from MBase

#close, #connect, #connected?

Constructor Details

#initialize(name = 'monk', user = '', pass = '', database = 'local', host = 'localhost', port = 27017, opts = {:sync => true}) ⇒ MHash

(Any Ruby) Differently from MBase, MHash enforce autoconnect.

Parameters:

  • name (String) (defaults to: 'monk')

    name of the used/created collection,

  • user (String) (defaults to: '')

    the database username,

  • pass (String) (defaults to: '')

    the password of the previous database user,

  • database (String) (defaults to: 'local')

    the name of the database,

  • host (String) (defaults to: 'localhost')

    the host of the database,

  • port (Fixnum) (defaults to: 27017)

    the port to connect to the database,

  • opts (Hash) (defaults to: {:sync => true})

    monk options (set :conect to autoconnect the Monk)



66
67
68
69
# File 'lib/sinatra-monk/2.0.rb', line 66

def initialize(name:'monk', user:'', pass:'', database:'local', host:'localhost',
               port:27017, opts:{:connect => false})
  _ol_initialize(name, user, pass, database, host, port, opts)
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



84
85
86
# File 'lib/sinatra-monk/monk.rb', line 84

def collection
  @collection
end

#localObject (readonly)

Returns the value of attribute local.



84
85
86
# File 'lib/sinatra-monk/monk.rb', line 84

def local
  @local
end

#nameObject (readonly)

Returns the value of attribute name.



84
85
86
# File 'lib/sinatra-monk/monk.rb', line 84

def name
  @name
end

#syncObject

Sync the local collection with the remote one.



115
116
117
# File 'lib/sinatra-monk/monk.rb', line 115

def sync
  @sync
end

Instance Method Details

#[](key) ⇒ Object

Gives read access to a key in the collection.

Parameters:

  • key (String)

    the key.

Raises:

  • (TypeError)


154
155
156
157
158
# File 'lib/sinatra-monk/monk.rb', line 154

def [](key)
  raise TypeError, "#{key} can't be coerced into String" unless key.is_a? String
  return @collection['content'][key] if @collection['content'].has_key? key
  raise InexistentKey, "Field #{key} not found."
end

#[]=(key, value) ⇒ Object

Gives write access to a key in the collection.

Parameters:

  • key (String)

    the key.

Raises:

  • (TypeError)


162
163
164
165
166
# File 'lib/sinatra-monk/monk.rb', line 162

def []=(key, value)
  raise TypeError, "#{key} can't be coerced into String" unless key.is_a? String
  @collection['content'][key] = value
  need_sync!
end

#_ol_initializeObject

Aliased initialize method, made to add Ruby 2.0 version.



57
# File 'lib/sinatra-monk/2.0.rb', line 57

alias _ol_initialize initialize

#delete(key) ⇒ Object

Delete a key from the collection.

Parameters:

  • key (String)

    the key.

Raises:

  • (TypeError)


142
143
144
145
146
147
148
149
150
# File 'lib/sinatra-monk/monk.rb', line 142

def delete(key)
  raise TypeError, "#{key} can't be coerced into String" unless key.is_a? String
  if @collection['content'].has_key? key
    @collection['content'].delete key
    need_sync!
    return true
  end
  return false
end

#include?(key) ⇒ Boolean

Returns true if collection has a key.

Parameters:

  • key (String)

    the key.

Returns:

  • (Boolean)


131
132
133
# File 'lib/sinatra-monk/monk.rb', line 131

def include?(key)
  return @collection['content'].has_key? key
end

#inspectObject

Returns the collection inspect data.



136
137
138
# File 'lib/sinatra-monk/monk.rb', line 136

def inspect
  return @collection['content'].inspect
end

#need_sync?Boolean

Returns true if collection need synchronization.

Returns:

  • (Boolean)


125
126
127
# File 'lib/sinatra-monk/monk.rb', line 125

def need_sync?
  return @need_sync
end