class Sinatra::Monk::MHash

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

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

Attributes

collection[R]
local[R]
name[R]
sync[RW]

Public Class Methods

new(name:'monk', user:'', pass:'', database:'local', host:'localhost', port:27017, opts:{:connect => false}) click to toggle source

(Ruby 2.x) Differently from MBase, MHash enforce autoconnect. @param name [String] name of the used/created collection, @param user [String] the database username, @param pass [String] the password of the previous database user, @param database [String] the name of the database, @param host [String] the host of the database, @param port [Fixnum] the port to connect to the database, @param opts [Hash] monk options (set :conect to autoconnect the Monk)

# 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
Also aliased as: _ol_initialize

Public Instance Methods

[](key) click to toggle source

Gives read access to a key in the collection. @param key [String] the key.

# 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) click to toggle source

Gives write access to a key in the collection. @param key [String] the key.

# 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_initialize(name:'monk', user:'', pass:'', database:'local', host:'localhost', port:27017, opts:{:connect => false})

Aliased initialize method, made to add Ruby 2.0 version.

Alias for: new
delete(key) click to toggle source

Delete a key from the collection. @param key [String] the key.

# 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) click to toggle source

Returns true if collection has a key. @param key [String] the key.

# File lib/sinatra-monk/monk.rb, line 131
def include?(key)
  return @collection['content'].has_key? key
end
inspect() click to toggle source

Returns the collection inspect data.

# File lib/sinatra-monk/monk.rb, line 136
def inspect
  return @collection['content'].inspect
end
need_sync?() click to toggle source

Returns true if collection need synchronization.

# File lib/sinatra-monk/monk.rb, line 125
def need_sync?
  return @need_sync
end

Private Instance Methods

install() click to toggle source

Install the collection in the database (may not install if already exists, in that case it just uses it).

# File lib/sinatra-monk/monk.rb, line 171
def install
  if @database.collection_names.include? @name
    # setup for use existent collection
    @collection = @database[@name].find_one({'name'=>@name})
  else
    # install collection
    @database.create_collection @name
    @database[@name].insert([{'name' => @name,
                                     'content' => {}}])
    @collection = @database[@name].find_one({'name'=>@name})
    need_sync!
  end
end
need_sync!() click to toggle source

Raises a sync flag, if autosync mode is on, it just sync the database.

# File lib/sinatra-monk/monk.rb, line 186
def need_sync!
  @need_sync = true
  sync if @sync # autosync
end