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
Public Class Methods
(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
Public Instance Methods
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
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
Aliased initialize method, made to add Ruby 2.0 version.
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
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
Returns the collection inspect data.
# File lib/sinatra-monk/monk.rb, line 136 def inspect return @collection['content'].inspect end
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 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
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