CatBox 0.0.0 → 0.0.1
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.
- data/lib/CatBox.rb +33 -32
- metadata +7 -4
data/lib/CatBox.rb
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
require "msgpack"
|
2
2
|
require "mysql2"
|
3
|
+
# @author Christian Stone
|
4
|
+
#Main CatBox Class
|
5
|
+
#Used for Storing Key Value Data to a MySql table
|
3
6
|
class CatBox
|
7
|
+
# Initializes CatBox by creating DB Connection and Creating CatBox table if needed
|
8
|
+
# @param [String] scheme the database to connect to
|
9
|
+
# @param [String] host the IP address of MySql Server
|
10
|
+
# @param [String] user the MySql user on the server
|
11
|
+
# @param [String, nil] password the password of the user
|
12
|
+
# @return [Boolean] True if All Goes Well
|
4
13
|
def initialize(scheme=nil,host="127.0.0.1",user="admin",password=nil)
|
5
14
|
options={:database => scheme,:host =>host, :username => user}
|
6
15
|
options[:password]=password if !password.nil?
|
@@ -10,58 +19,50 @@ class CatBox
|
|
10
19
|
test=tables.each do |x|
|
11
20
|
return true if x=="cat_box"
|
12
21
|
end
|
13
|
-
|
22
|
+
@client.query( 'CREATE TABLE cat_box (box_key VARCHAR(80) NOT NULL PRIMARY KEY, package BLOB)') if !test
|
14
23
|
return true
|
15
24
|
end
|
16
25
|
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
|
26
|
+
#Clears CatBox Table
|
27
|
+
# @return [Boolean] True if All Goes Well
|
21
28
|
def empty_cache
|
22
29
|
@client.query( 'TRUNCATE TABLE cat_box;')
|
23
|
-
|
30
|
+
return true
|
24
31
|
end
|
25
32
|
|
33
|
+
#Overide Array Style Operator for usefulness
|
34
|
+
# @param [String] index the key to look up
|
35
|
+
# @return [Object,nil] The Object stored in that location
|
26
36
|
def [](index)
|
27
37
|
fetch(index)
|
28
38
|
end
|
29
39
|
|
30
|
-
|
31
|
-
|
40
|
+
#Overide Array Style Operator for usefulness
|
41
|
+
# @param [String] index the key to look up
|
42
|
+
# @param [Object] data the object to be stored
|
43
|
+
# @return [Object] The Object stored in that location
|
44
|
+
def []=(index,data)
|
45
|
+
put(index,data)
|
32
46
|
end
|
33
47
|
|
34
|
-
def self.[](index)
|
35
|
-
fetch(index)
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.[]=(key,index)
|
39
|
-
put(key,index)
|
40
|
-
end
|
41
48
|
|
42
|
-
|
43
|
-
|
49
|
+
#Puts Data into CatBox
|
50
|
+
# @param [String] index the key to look up
|
51
|
+
# @param [Object] data the object to be stored
|
52
|
+
# @return [Object] The Object stored in that location
|
53
|
+
def put(index, data)
|
44
54
|
data=data.to_msgpack
|
45
|
-
@client.query("INSERT INTO cat_box(box_key, package) VALUES ('#{@client.escape(
|
55
|
+
@client.query("INSERT INTO cat_box(box_key, package) VALUES ('#{@client.escape(index.to_s)}', '#{@client.escape(data)}') ON DUPLICATE KEY UPDATE package='#{@client.escape(data)}'")
|
46
56
|
return true
|
47
57
|
end
|
48
58
|
|
49
|
-
|
50
|
-
|
59
|
+
#Gets Data from CatBox
|
60
|
+
# @param [String] index the key to look up
|
61
|
+
# @return [Object,nil] The Object stored in that location
|
62
|
+
def fetch(index)
|
63
|
+
data=@client.query("SELECT package FROM cat_box where box_key='#{@client.escape(index.to_s)}'").first
|
51
64
|
return nil if data.nil?
|
52
65
|
data=data["package"]
|
53
66
|
return MessagePack.unpack(data)
|
54
67
|
end
|
55
|
-
|
56
|
-
def self.put(key, value)
|
57
|
-
@@default_cache.put(key, value)
|
58
|
-
end
|
59
|
-
|
60
|
-
def self.fetch(key)
|
61
|
-
@@default_cache.fetch(key)
|
62
|
-
end
|
63
|
-
|
64
|
-
def self.initialize(scheme=nil,host="127.0.0.1",user="admin",password=nil)
|
65
|
-
@@default_cache=CatBox.new(scheme, host, user, password)
|
66
|
-
end
|
67
68
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: CatBox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -18,7 +18,7 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/CatBox.rb
|
21
|
-
homepage:
|
21
|
+
homepage: https://rubygems.org/gems/CatBox
|
22
22
|
licenses: []
|
23
23
|
post_install_message:
|
24
24
|
rdoc_options: []
|
@@ -36,10 +36,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
36
|
- - ! '>='
|
37
37
|
- !ruby/object:Gem::Version
|
38
38
|
version: '0'
|
39
|
-
requirements:
|
39
|
+
requirements:
|
40
|
+
- msgpack
|
41
|
+
- mysql2
|
40
42
|
rubyforge_project:
|
41
43
|
rubygems_version: 1.8.24
|
42
44
|
signing_key:
|
43
45
|
specification_version: 3
|
44
|
-
summary:
|
46
|
+
summary: CatBox
|
45
47
|
test_files: []
|
48
|
+
has_rdoc:
|