Class: Sinatra::Monk::MHash
- 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
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#local ⇒ Object
readonly
Returns the value of attribute local.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sync ⇒ Object
Sync the local collection with the remote one.
Attributes inherited from MBase
#client, #database, #password, #username
Instance Method Summary collapse
-
#[](key) ⇒ Object
Gives read access to a key in the collection.
-
#[]=(key, value) ⇒ Object
Gives write access to a key in the collection.
-
#_ol_initialize ⇒ Object
Aliased initialize method, made to add Ruby 2.0 version.
-
#delete(key) ⇒ Object
Delete a key from the collection.
-
#include?(key) ⇒ Boolean
Returns true if collection has a key.
-
#initialize(name = 'monk', user = '', pass = '', database = 'local', host = 'localhost', port = 27017, opts = {:sync => true}) ⇒ MHash
constructor
(Any Ruby) Differently from MBase, MHash enforce autoconnect.
-
#inspect ⇒ Object
Returns the collection inspect data.
-
#need_sync? ⇒ Boolean
Returns true if collection need synchronization.
Methods inherited from MBase
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.
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
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
84 85 86 |
# File 'lib/sinatra-monk/monk.rb', line 84 def collection @collection end |
#local ⇒ Object (readonly)
Returns the value of attribute local.
84 85 86 |
# File 'lib/sinatra-monk/monk.rb', line 84 def local @local end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
84 85 86 |
# File 'lib/sinatra-monk/monk.rb', line 84 def name @name end |
#sync ⇒ Object
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.
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.
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_initialize ⇒ Object
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.
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.
131 132 133 |
# File 'lib/sinatra-monk/monk.rb', line 131 def include?(key) return @collection['content'].has_key? key end |
#inspect ⇒ Object
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.
125 126 127 |
# File 'lib/sinatra-monk/monk.rb', line 125 def need_sync? return @need_sync end |