sinatra-monk 0.0.8
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 +7 -0
- data/lib/sinatra-monk.rb +27 -0
- data/lib/sinatra-monk/2.0.rb +60 -0
- data/lib/sinatra-monk/base.rb +26 -0
- data/lib/sinatra-monk/helpers.rb +42 -0
- data/lib/sinatra-monk/monk.rb +176 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: adb6107dab9b741150f7b27cdd1d117747384816
|
4
|
+
data.tar.gz: 827bd3d7689803a6df5e1a0509ddc161b387712d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0fb87d0466fcffb17d5048e9c65bed64e0e8129cce80573a3e8c229d53328b7b2c25b3ebbd244eb3503b4ab9e729a6e520374f705e03ce388ca417f82559a2dc
|
7
|
+
data.tar.gz: 9e8584f65f88d31da5ad0794954c57222a5baa91f3d7e6002d1712d7d218cd98e9f475e3a78c4947cabbeee8000f862ca5bbadaa59b48c651cb746473977f025
|
data/lib/sinatra-monk.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
##
|
3
|
+
# Copyright (c) 2013 Christian Ferraz Lemos de Sousa
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
9
|
+
# of the Software, and to permit persons to whom the Software is furnished to do
|
10
|
+
# so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
16
|
+
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
17
|
+
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
18
|
+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
19
|
+
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
20
|
+
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
####
|
22
|
+
# Sinatra's Monk was done with Ruby 1.9 ('cause of a bug with bson_ext),
|
23
|
+
# but we provide 2.0 automatic named args support.
|
24
|
+
##
|
25
|
+
|
26
|
+
require 'sinatra-monk/base'
|
27
|
+
require 'sinatra-monk/2.0' if RUBY_VERSION.chr == '2'
|
@@ -0,0 +1,60 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
##
|
3
|
+
# Copyright (c) 2013 Christian Ferraz Lemos de Sousa
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
9
|
+
# of the Software, and to permit persons to whom the Software is furnished to do
|
10
|
+
# so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
16
|
+
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
17
|
+
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
18
|
+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
19
|
+
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
20
|
+
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
####
|
22
|
+
|
23
|
+
# Bunch of function redefinition to make use
|
24
|
+
# of Ruby 2.0 new standard. Also, require this
|
25
|
+
# file to enforce the use of Ruby 2.0.
|
26
|
+
|
27
|
+
# ensure that we have monk
|
28
|
+
require 'sinatra-monk/base'
|
29
|
+
|
30
|
+
module Sinatra
|
31
|
+
module Monk
|
32
|
+
class MBase
|
33
|
+
# monk base class args are fully optional
|
34
|
+
alias _ol_initialize initialize
|
35
|
+
def initialize(user:'', pass:'', database:'local', host:'localhost',
|
36
|
+
port:27017, opts:{:connect => false})
|
37
|
+
_ol_initialize(user, pass, database, host, port, opts)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class HBase
|
42
|
+
# monk base class args are fully optional
|
43
|
+
alias _ol_initialize initialize
|
44
|
+
def initialize(name:'monk', user:'', pass:'', database:'local', host:'localhost',
|
45
|
+
port:27017, opts:{:connect => false})
|
46
|
+
_ol_initialize(name, user, pass, database, host, port, opts)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def monk(user:'', pass:'', database:'local', host:'localhost',
|
51
|
+
port:27017, opts:{:connect => false})
|
52
|
+
return MBase.new(user, pass, database, host, port, opts)
|
53
|
+
end
|
54
|
+
|
55
|
+
def hmonk(name:'monk', user:'', pass:'', database:'local', host:'localhost',
|
56
|
+
port:27017, opts:{:connect => false})
|
57
|
+
return MHash.new(name, user, pass, database, host, port, opts)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
##
|
3
|
+
# Copyright (c) 2013 Christian Ferraz Lemos de Sousa
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
9
|
+
# of the Software, and to permit persons to whom the Software is furnished to do
|
10
|
+
# so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
16
|
+
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
17
|
+
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
18
|
+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
19
|
+
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
20
|
+
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
####
|
22
|
+
# a standard way to require monk cross compatible
|
23
|
+
# monk (between 2x and 19x series)
|
24
|
+
##
|
25
|
+
require 'sinatra-monk/monk'
|
26
|
+
require 'sinatra-monk/helpers'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
##
|
3
|
+
# Copyright (c) 2013 Christian Ferraz Lemos de Sousa
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
9
|
+
# of the Software, and to permit persons to whom the Software is furnished to do
|
10
|
+
# so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
16
|
+
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
17
|
+
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
18
|
+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
19
|
+
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
20
|
+
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
####
|
22
|
+
# Some helpers to make life easier on Sinatra.
|
23
|
+
##
|
24
|
+
|
25
|
+
# helpers need sinatra
|
26
|
+
require 'sinatra-monk/base'
|
27
|
+
require 'sinatra/base'
|
28
|
+
|
29
|
+
module Sinatra
|
30
|
+
module Monk
|
31
|
+
def monk user = '', pass = '', database = 'local', host = 'localhost',
|
32
|
+
port = 27017, opts = {:connect => false}
|
33
|
+
return MBase.new(user, pass, database, host, port, opts)
|
34
|
+
end
|
35
|
+
|
36
|
+
def hmonk name = 'monk', user = '', pass = '', database = 'local',
|
37
|
+
host = 'localhost', port = 27017, opts = {:save => true}
|
38
|
+
return MHash.new(name, user, pass, database, host, port, opts)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
helpers Monk
|
42
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
##
|
3
|
+
# Copyright (c) 2013 Christian Ferraz Lemos de Sousa
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
9
|
+
# of the Software, and to permit persons to whom the Software is furnished to do
|
10
|
+
# so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
16
|
+
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
17
|
+
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
18
|
+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
19
|
+
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
20
|
+
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
####
|
22
|
+
# Base Monk classes
|
23
|
+
##
|
24
|
+
|
25
|
+
# monk needs mongo (duh)
|
26
|
+
require 'mongo'
|
27
|
+
|
28
|
+
module Sinatra
|
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
|
32
|
+
class MBase
|
33
|
+
attr_reader :client, :database, :username, :password
|
34
|
+
|
35
|
+
# monk base class args are fully optional
|
36
|
+
def initialize user = '', pass = '', database = 'local', host = 'localhost',
|
37
|
+
port = 27017, opts = {:connect => false}
|
38
|
+
# these data can't just be changed runtime
|
39
|
+
@client = Mongo::MongoClient.new 'localhost', 27017
|
40
|
+
@database = @client[database]
|
41
|
+
@database_name = database
|
42
|
+
|
43
|
+
# non persistent data (can be changed before connect)
|
44
|
+
@username = user
|
45
|
+
@password = pass
|
46
|
+
|
47
|
+
# connect if asked
|
48
|
+
connect if opts[:connect]
|
49
|
+
end
|
50
|
+
|
51
|
+
def close
|
52
|
+
@client.close
|
53
|
+
end
|
54
|
+
|
55
|
+
def connected?
|
56
|
+
return @client.connected?
|
57
|
+
end
|
58
|
+
|
59
|
+
def connect user = @username, pass = @password
|
60
|
+
return true if connected?
|
61
|
+
@client.connect
|
62
|
+
@username = user
|
63
|
+
@password = pass
|
64
|
+
return @database.authenticate @username, @password
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Monk::Hash is a Monk that acts like a Hash, it
|
69
|
+
# was made to look very native.
|
70
|
+
class MHash < MBase
|
71
|
+
attr_reader :local, :name, :collection
|
72
|
+
attr_accessor :sync
|
73
|
+
|
74
|
+
class InexistentKey < StandardError
|
75
|
+
end
|
76
|
+
|
77
|
+
# force to connect
|
78
|
+
def initialize name = 'monk', user = '', pass = '', database = 'local',
|
79
|
+
host = 'localhost', port = 27017, opts = {:save => true}
|
80
|
+
# forced premature connection (option override)
|
81
|
+
opts[:connect]= true
|
82
|
+
|
83
|
+
# HashMonk special data
|
84
|
+
@name = name # collection name
|
85
|
+
@sync = opts[:save] # autosave collection
|
86
|
+
@local = Hash.new # local information
|
87
|
+
|
88
|
+
# need update flag (writes local data to db)
|
89
|
+
@need_sync = false
|
90
|
+
|
91
|
+
# build our monk
|
92
|
+
super host, port, user, pass, opts
|
93
|
+
|
94
|
+
install if connect
|
95
|
+
end
|
96
|
+
|
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
|
131
|
+
def sync
|
132
|
+
if @need_sync
|
133
|
+
@database[@name].save(@collection)
|
134
|
+
@need_sync = false
|
135
|
+
return true
|
136
|
+
end
|
137
|
+
return false
|
138
|
+
end
|
139
|
+
|
140
|
+
# return true if db need to sync
|
141
|
+
def need_sync?
|
142
|
+
return @need_sync
|
143
|
+
end
|
144
|
+
|
145
|
+
def include?(key)
|
146
|
+
return @collection['content'].has_key? key
|
147
|
+
end
|
148
|
+
|
149
|
+
def inspect
|
150
|
+
return @collection['content'].inspect
|
151
|
+
end
|
152
|
+
|
153
|
+
def delete(key)
|
154
|
+
raise TypeError, "#{key} can't be coerced into String" unless key.is_a? String
|
155
|
+
if @collection['content'].has_key? key
|
156
|
+
@collection['content'].delete key
|
157
|
+
need_sync!
|
158
|
+
return true
|
159
|
+
end
|
160
|
+
return false
|
161
|
+
end
|
162
|
+
|
163
|
+
def [](key)
|
164
|
+
raise TypeError, "#{key} can't be coerced into String" unless key.is_a? String
|
165
|
+
return @collection['content'][key] if @collection['content'].has_key? key
|
166
|
+
raise InexistentKey, "Field #{key} not found."
|
167
|
+
end
|
168
|
+
|
169
|
+
def []=(key, value)
|
170
|
+
raise TypeError, "#{key} can't be coerced into String" unless key.is_a? String
|
171
|
+
@collection['content'][key] = value
|
172
|
+
need_sync!
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-monk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christian Ferraz Lemos de Sousa
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-03-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sinatra
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mongo
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A (very) simple wrapper for Mongo driver.
|
42
|
+
email: cferraz95@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/sinatra-monk.rb
|
48
|
+
- lib/sinatra-monk/2.0.rb
|
49
|
+
- lib/sinatra-monk/base.rb
|
50
|
+
- lib/sinatra-monk/monk.rb
|
51
|
+
- lib/sinatra-monk/helpers.rb
|
52
|
+
homepage: http://rubygems.org/gems/sinatra-monk
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.0.0
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Add some Mongo helpers to sinatra.
|
76
|
+
test_files: []
|
77
|
+
has_rdoc:
|