sinatra-monk 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: adb6107dab9b741150f7b27cdd1d117747384816
4
- data.tar.gz: 827bd3d7689803a6df5e1a0509ddc161b387712d
3
+ metadata.gz: a53615c0d27d013975e9c0fed1378d3f9a3bc5c2
4
+ data.tar.gz: 3f6bcadaf0cb1471dea96280a792daac7733bb67
5
5
  SHA512:
6
- metadata.gz: 0fb87d0466fcffb17d5048e9c65bed64e0e8129cce80573a3e8c229d53328b7b2c25b3ebbd244eb3503b4ab9e729a6e520374f705e03ce388ca417f82559a2dc
7
- data.tar.gz: 9e8584f65f88d31da5ad0794954c57222a5baa91f3d7e6002d1712d7d218cd98e9f475e3a78c4947cabbeee8000f862ca5bbadaa59b48c651cb746473977f025
6
+ metadata.gz: 563438d4304e56b7a4095e2c545ee670a418062575820b00f47d5409feeb73c127c9082a8a6ca5b7373baf6cdd6626097f110eed81a8482834a24e03835513f9
7
+ data.tar.gz: 1becfb4d9d65ee3d7e3a73d890c7be50d8696b17df3b48e506eac5142c329031a188a615e385e462d6aa4e0316ded74f4a852b41359f9a42a304f6177846501e
data/lib/sinatra-monk.rb CHANGED
@@ -24,4 +24,4 @@
24
24
  ##
25
25
 
26
26
  require 'sinatra-monk/base'
27
- require 'sinatra-monk/2.0' if RUBY_VERSION.chr == '2'
27
+ require 'sinatra-monk/2.0' if RUBY_VERSION.chr == '2'
@@ -29,29 +29,68 @@ require 'sinatra-monk/base'
29
29
 
30
30
  module Sinatra
31
31
  module Monk
32
+ # Provides a base class for Mongo database management. This is the simplest way
33
+ # to connect to Mongo with Monk. It is intented to keep Sinatra's philosofy of
34
+ # keeping it as simple as possible for use.
32
35
  class MBase
33
- # monk base class args are fully optional
36
+ # Aliased initialize method, made to add Ruby 2.0 version.
34
37
  alias _ol_initialize initialize
38
+
39
+ # (Ruby 2.x) Setup the Monk with the data that was sent.
40
+ # Autoconnect if :connect flag is true.
41
+ # @param user [String] the database username,
42
+ # @param pass [String] the password of the previous database user,
43
+ # @param database [String] the name of the database,
44
+ # @param host [String] the host of the database,
45
+ # @param port [Fixnum] the port to connect to the database,
46
+ # @param opts [Hash] monk options (set :conect to autoconnect the Monk)
35
47
  def initialize(user:'', pass:'', database:'local', host:'localhost',
36
48
  port:27017, opts:{:connect => false})
37
49
  _ol_initialize(user, pass, database, host, port, opts)
38
50
  end
39
51
  end
40
52
 
41
- class HBase
42
- # monk base class args are fully optional
53
+ # A type of Monk that acts like a Hash, it was made to look very native.
54
+ # It still need some improvement.
55
+ class MHash
56
+ # Aliased initialize method, made to add Ruby 2.0 version.
43
57
  alias _ol_initialize initialize
58
+ # (Ruby 2.x) Differently from MBase, MHash enforce autoconnect.
59
+ # @param name [String] name of the used/created collection,
60
+ # @param user [String] the database username,
61
+ # @param pass [String] the password of the previous database user,
62
+ # @param database [String] the name of the database,
63
+ # @param host [String] the host of the database,
64
+ # @param port [Fixnum] the port to connect to the database,
65
+ # @param opts [Hash] monk options (set :conect to autoconnect the Monk)
44
66
  def initialize(name:'monk', user:'', pass:'', database:'local', host:'localhost',
45
67
  port:27017, opts:{:connect => false})
46
68
  _ol_initialize(name, user, pass, database, host, port, opts)
47
69
  end
48
70
  end
49
71
 
72
+ # (Ruby 2.x) A helper that create a MBase instance.
73
+ # @param user [String] the database username,
74
+ # @param pass [String] the password of the previous database user,
75
+ # @param database [String] the name of the database,
76
+ # @param host [String] the host of the database,
77
+ # @param port [Fixnum] the port to connect to the database,
78
+ # @param opts [Hash] monk options (set :conect to autoconnect the Monk)
79
+ # @return [MBase] the Monk instance.
50
80
  def monk(user:'', pass:'', database:'local', host:'localhost',
51
81
  port:27017, opts:{:connect => false})
52
82
  return MBase.new(user, pass, database, host, port, opts)
53
83
  end
54
84
 
85
+ # (Ruby 2.x) A helper that create a MHash instance.
86
+ # @param name [String] name of the used/created collection,
87
+ # @param user [String] the database username,
88
+ # @param pass [String] the password of the previous database user,
89
+ # @param database [String] the name of the database,
90
+ # @param host [String] the host of the database,
91
+ # @param port [Fixnum] the port to connect to the database,
92
+ # @param opts [Hash] monk options (set :save to autosave after each change)
93
+ # @return [MHash] the Monk instance.
55
94
  def hmonk(name:'monk', user:'', pass:'', database:'local', host:'localhost',
56
95
  port:27017, opts:{:connect => false})
57
96
  return MHash.new(name, user, pass, database, host, port, opts)
@@ -22,5 +22,6 @@
22
22
  # a standard way to require monk cross compatible
23
23
  # monk (between 2x and 19x series)
24
24
  ##
25
+
25
26
  require 'sinatra-monk/monk'
26
- require 'sinatra-monk/helpers'
27
+ require 'sinatra-monk/helpers'
@@ -28,11 +28,28 @@ require 'sinatra/base'
28
28
 
29
29
  module Sinatra
30
30
  module Monk
31
+ # (Any Ruby) A helper that create a MBase instance.
32
+ # @param user [String] the database username,
33
+ # @param pass [String] the password of the previous database user,
34
+ # @param database [String] the name of the database,
35
+ # @param host [String] the host of the database,
36
+ # @param port [Fixnum] the port to connect to the database,
37
+ # @param opts [Hash] monk options (set :conect to autoconnect the Monk)
38
+ # @return [MBase] the Monk instance.
31
39
  def monk user = '', pass = '', database = 'local', host = 'localhost',
32
40
  port = 27017, opts = {:connect => false}
33
41
  return MBase.new(user, pass, database, host, port, opts)
34
42
  end
35
43
 
44
+ # (Any Ruby) A helper that create a MHash instance.
45
+ # @param name [String] name of the used/created collection,
46
+ # @param user [String] the database username,
47
+ # @param pass [String] the password of the previous database user,
48
+ # @param database [String] the name of the database,
49
+ # @param host [String] the host of the database,
50
+ # @param port [Fixnum] the port to connect to the database,
51
+ # @param opts [Hash] monk options (set :save to autosave after each change)
52
+ # @return [MHash] the Monk instance.
36
53
  def hmonk name = 'monk', user = '', pass = '', database = 'local',
37
54
  host = 'localhost', port = 27017, opts = {:save => true}
38
55
  return MHash.new(name, user, pass, database, host, port, opts)
@@ -27,12 +27,19 @@ require 'mongo'
27
27
 
28
28
  module Sinatra
29
29
  module Monk
30
- # Base provides a base class for mongo database management it is the simplest way
31
- # to connect to it. It is intented to keep sinatra's philosofy of keeping it simple
30
+ # Provides a base class for Mongo database management. This is the simplest way
31
+ # to connect to Mongo with Monk. It is intented to keep Sinatra's philosofy of
32
+ # keeping it as simple as possible for use.
32
33
  class MBase
33
34
  attr_reader :client, :database, :username, :password
34
35
 
35
- # monk base class args are fully optional
36
+ # (Any Ruby) Setup the Monk with the data that was sent. Autoconnect if :connect flag is true.
37
+ # @param user [String] the database username,
38
+ # @param pass [String] the password of the previous database user,
39
+ # @param database [String] the name of the database,
40
+ # @param host [String] the host of the database,
41
+ # @param port [Fixnum] the port to connect to the database,
42
+ # @param opts [Hash] monk options (set :conect to autoconnect the Monk)
36
43
  def initialize user = '', pass = '', database = 'local', host = 'localhost',
37
44
  port = 27017, opts = {:connect => false}
38
45
  # these data can't just be changed runtime
@@ -48,14 +55,20 @@ module Sinatra
48
55
  connect if opts[:connect]
49
56
  end
50
57
 
58
+ # Closes the connection with the server, you MAY NOT TRY to alter the collection while
59
+ # in that state.
51
60
  def close
52
61
  @client.close
53
62
  end
54
63
 
64
+ # Returns the Monk connection state.
55
65
  def connected?
56
66
  return @client.connected?
57
67
  end
58
68
 
69
+ # Conects the Monk to Mongo.
70
+ # @param user [String] the database username,
71
+ # @param pass [String] the password of the previous database user.
59
72
  def connect user = @username, pass = @password
60
73
  return true if connected?
61
74
  @client.connect
@@ -65,28 +78,32 @@ module Sinatra
65
78
  end
66
79
  end
67
80
 
68
- # Monk::Hash is a Monk that acts like a Hash, it
69
- # was made to look very native.
81
+ # A type of Monk that acts like a Hash, it was made to look very native.
82
+ # It still need some improvement.
70
83
  class MHash < MBase
71
84
  attr_reader :local, :name, :collection
72
85
  attr_accessor :sync
73
86
 
74
- class InexistentKey < StandardError
75
- end
76
-
77
- # force to connect
87
+ # (Any Ruby) Differently from MBase, MHash enforce autoconnect.
88
+ # @param name [String] name of the used/created collection,
89
+ # @param user [String] the database username,
90
+ # @param pass [String] the password of the previous database user,
91
+ # @param database [String] the name of the database,
92
+ # @param host [String] the host of the database,
93
+ # @param port [Fixnum] the port to connect to the database,
94
+ # @param opts [Hash] monk options (set :conect to autoconnect the Monk)
78
95
  def initialize name = 'monk', user = '', pass = '', database = 'local',
79
- host = 'localhost', port = 27017, opts = {:save => true}
96
+ host = 'localhost', port = 27017, opts = {:sync => true}
80
97
  # forced premature connection (option override)
81
98
  opts[:connect]= true
82
99
 
83
100
  # HashMonk special data
84
- @name = name # collection name
85
- @sync = opts[:save] # autosave collection
86
- @local = Hash.new # local information
101
+ @name = name # collection name
102
+ @sync = opts[:sync] # autosave collection
103
+ @local = Hash.new # local information
87
104
 
88
105
  # need update flag (writes local data to db)
89
- @need_sync = false
106
+ @need_sync = false
90
107
 
91
108
  # build our monk
92
109
  super host, port, user, pass, opts
@@ -94,40 +111,7 @@ module Sinatra
94
111
  install if connect
95
112
  end
96
113
 
97
- # install the collection in the database
98
- # (may not install if already exists)
99
- private
100
- def install
101
- if @database.collection_names.include? @name
102
- # setup for use existent collection
103
- @collection = @database[@name].find_one({'name'=>@name})
104
- else
105
- # install collection
106
- @database.create_collection @name
107
- @database[@name].insert([{'name' => @name,
108
- 'content' => {}}])
109
- @collection = @database[@name].find_one({'name'=>@name})
110
- need_sync!
111
- end
112
- end
113
-
114
- def connect user = @username, pass = @password
115
- return true if connected?
116
- @client.connect
117
- @username = user
118
- @password = pass
119
- return @database.authenticate @username, @password
120
- end
121
-
122
- # setup for sync
123
- def need_sync!
124
- @need_sync = true
125
- sync if @sync # autosync
126
- end
127
-
128
- public
129
- # save data, may be called automagically
130
- # if autosave mode is on
114
+ # Sync the local collection with the remote one.
131
115
  def sync
132
116
  if @need_sync
133
117
  @database[@name].save(@collection)
@@ -137,19 +121,24 @@ module Sinatra
137
121
  return false
138
122
  end
139
123
 
140
- # return true if db need to sync
124
+ # Returns true if collection need synchronization.
141
125
  def need_sync?
142
126
  return @need_sync
143
127
  end
144
128
 
129
+ # Returns true if collection has a key.
130
+ # @param key [String] the key.
145
131
  def include?(key)
146
132
  return @collection['content'].has_key? key
147
133
  end
148
134
 
135
+ # Returns the collection inspect data.
149
136
  def inspect
150
137
  return @collection['content'].inspect
151
138
  end
152
139
 
140
+ # Delete a key from the collection.
141
+ # @param key [String] the key.
153
142
  def delete(key)
154
143
  raise TypeError, "#{key} can't be coerced into String" unless key.is_a? String
155
144
  if @collection['content'].has_key? key
@@ -160,17 +149,44 @@ module Sinatra
160
149
  return false
161
150
  end
162
151
 
152
+ # Gives read access to a key in the collection.
153
+ # @param key [String] the key.
163
154
  def [](key)
164
155
  raise TypeError, "#{key} can't be coerced into String" unless key.is_a? String
165
156
  return @collection['content'][key] if @collection['content'].has_key? key
166
157
  raise InexistentKey, "Field #{key} not found."
167
158
  end
168
159
 
160
+ # Gives write access to a key in the collection.
161
+ # @param key [String] the key.
169
162
  def []=(key, value)
170
163
  raise TypeError, "#{key} can't be coerced into String" unless key.is_a? String
171
164
  @collection['content'][key] = value
172
165
  need_sync!
173
166
  end
167
+
168
+ private
169
+ # Install the collection in the database (may not install if already exists,
170
+ # in that case it just uses it).
171
+ def install
172
+ if @database.collection_names.include? @name
173
+ # setup for use existent collection
174
+ @collection = @database[@name].find_one({'name'=>@name})
175
+ else
176
+ # install collection
177
+ @database.create_collection @name
178
+ @database[@name].insert([{'name' => @name,
179
+ 'content' => {}}])
180
+ @collection = @database[@name].find_one({'name'=>@name})
181
+ need_sync!
182
+ end
183
+ end
184
+
185
+ # Raises a sync flag, if autosync mode is on, it just sync the database.
186
+ def need_sync!
187
+ @need_sync = true
188
+ sync if @sync # autosync
189
+ end
174
190
  end
175
191
  end
176
192
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-monk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Ferraz Lemos de Sousa