usefuldb 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/CHANGELOG ADDED
@@ -0,0 +1,18 @@
1
+ usefuldb gem CHANGELOG
2
+
3
+ ## Version 0.0.1
4
+ - Added test for adding entries to the database
5
+ - Added test for removing entries from the database
6
+ - Added test for loading and saving the database
7
+ - Added executable script usefuldb, and the barebones parameter parsing system therein
8
+ - Refactored the tests to allow for easier reading of the code
9
+ - Updated the README to include information about dependencies and installation instructions
10
+ - Wrapped all current codebase in a single Ruby module: UsefulDB
11
+ - Added search functionality to the system
12
+ - Added the functionality to add elements to the database
13
+ - Added the functionality to list all elements in the database
14
+ - Added functionality to delete an element from the database
15
+ - On first run, the system will now install the database in the users home directory at ~/.usefuldb/db.yaml
16
+
17
+ ## Version 0.0.0
18
+ - Added core functionality
data/COPYING ADDED
@@ -0,0 +1,4 @@
1
+ usefuldb - simple commands and or urls database for storage of useful information.
2
+
3
+ usefuldb is released under the creative commons attribution-sharealike 3.0 unported (cc by-sa 3.0) licence.
4
+ for more information see: http://creativecommons.org/licenses/by-sa/3.0/
data/README ADDED
@@ -0,0 +1,13 @@
1
+ ## Readme
2
+
3
+ ### Dependencies
4
+ In order to run the rake tests, you may require the following gems to be installed on your system:
5
+
6
+ : rake
7
+ : rack-test
8
+ : test-unit
9
+
10
+
11
+ ### Install
12
+ : rake test
13
+ : rake install / sudo rake install
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :default => 'menu'
5
+
6
+ task :menu do
7
+ puts welcomeMsg = <<-MSG
8
+ rake build # Build usefuldb-x.x.x.gem into the pkg directory
9
+ rake install # Build and install usefuldb-x.x.x.gem into system gems
10
+ rake release # Create tag vx.x.x and build and push usefuldb-x.x.x.gem to http://rubygems.org/
11
+ rake test # Run the unit testing suite on this gem.
12
+ MSG
13
+ end
14
+
15
+ desc "Run the unit testing suite on this gem."
16
+ task :test do
17
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/lib')
18
+ require 'usefuldb.rb'
19
+ Dir.glob("./test/*_test.rb").each do |file|
20
+ require file
21
+ end
22
+ end
data/bin/usefuldb ADDED
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'usefuldb'
4
+ require 'optparse'
5
+
6
+ #####################################################
7
+ def addColour(text, colour_code)
8
+ "\e[#{colour_code}m#{text}\e[0m"
9
+ end
10
+
11
+ def red(text); addColour(text, 31); end
12
+ def green(text); addColour(text, 32); end
13
+ def yellow(text); addColour(text, 33); end
14
+ def blue(text); addColour(text, 34); end
15
+ #####################################################
16
+
17
+ options = {}
18
+ optparse = nil
19
+
20
+ # Setup the system for use
21
+ UsefulDB.setup
22
+
23
+ begin
24
+ optparse = OptionParser.new do |opts|
25
+
26
+ opts.banner = <<-WELCOME
27
+ UsefulDB - a simple command and URL database
28
+
29
+ Useage: usefuldb <searchtag1> <searchtag2> .. <searchtagN>
30
+
31
+ WELCOME
32
+
33
+ opts.on('-a', '--add', "Add an entry to the database") do |arg|
34
+ options[:a] = arg
35
+ end
36
+
37
+ opts.on('-d', '--delete', "Delete an entry from the database") do |arg|
38
+ options[:d] = arg
39
+ end
40
+
41
+ opts.on('-l', '--list', "List all entries in the database") do |arg|
42
+ options[:l] = arg
43
+ end
44
+
45
+ opts.on('-v', '--verbose', "Runs the system in verbose mode") do |arg|
46
+ options[:v] = arg
47
+ end
48
+
49
+ opts.on('-h', '--help', "Print out this message") do |arg|
50
+ puts opts
51
+ exit
52
+ end
53
+
54
+ end.parse!
55
+ rescue OptionParser::InvalidOption => e
56
+ puts e.message + "\n\nUseage: usefuldb -h or --help for help using the system"
57
+ exit()
58
+ end
59
+
60
+ #############################################################################
61
+
62
+ # Checking for flag parameters
63
+ if options.empty?
64
+ # No parameters of any kind were passed
65
+ if ARGV.empty?
66
+ puts "Useage: usefuldb -h or --help for help using the system"
67
+ exit()
68
+
69
+ # At least one non flag parameter was passed
70
+ else
71
+ puts green("Launching the search system:")
72
+ UsefulDB::GUI.search(ARGV)
73
+ end
74
+
75
+ # Some flags parameters were passed
76
+ else
77
+ #puts options
78
+
79
+ if options[:a] # The -a or --add flag check
80
+ puts green("Launching the add system:")
81
+ UsefulDB::GUI.add(options)
82
+
83
+ elsif options[:l]
84
+ puts green("Launching the list system:")
85
+ UsefulDB::GUI.list
86
+
87
+ elsif options[:d] # The -d or --delete flag check
88
+ puts "Run the delete system:"
89
+ UsefulDB::GUI.remove(options)
90
+
91
+ end
92
+ end
93
+
94
+
@@ -0,0 +1,14 @@
1
+ require 'usefuldb'
2
+
3
+ module UsefulDB
4
+
5
+ class EntryInDB < Exception
6
+ end
7
+
8
+ class EmptyDB < Exception
9
+ end
10
+
11
+ class KeyOutOfBounds < Exception
12
+ end
13
+
14
+ end
@@ -0,0 +1,90 @@
1
+ require 'usefuldb/utilities'
2
+
3
+ module UsefulDB
4
+
5
+ class GUI
6
+ class << self
7
+ attr_accessor
8
+
9
+ def search(args)
10
+ UsefulDB.dbLoad
11
+ args.each {|i| puts "\n" + UsefulDB::UsefulUtils.search(i)}
12
+ end
13
+
14
+ def list
15
+ UsefulDB.dbLoad
16
+ UsefulDB::UsefulUtils.list.each_with_index do |i, index|
17
+ puts red(index)
18
+ puts "- Tags: " + yellow(i["tag"].to_s) + "\n"
19
+ puts "- Value: " + red(i["value"]) + blue("\n##\n")
20
+ end
21
+ end
22
+
23
+
24
+ def remove(opts)
25
+ if opts[:v] then puts "in verbose mode\n"; end
26
+ list()
27
+
28
+ puts "Enter the number of the element from the list above which you want to delete"
29
+ value = STDIN.gets
30
+
31
+ begin
32
+ UsefulDB.remove(value.to_i, {})
33
+ UsefulDB.dbSave
34
+ rescue KeyOutOfBounds => e
35
+ puts e.message + "\nexiting."
36
+ exit()
37
+ end
38
+
39
+ end
40
+
41
+
42
+ def add(opts)
43
+ UsefulDB.dbLoad
44
+ if opts[:v] then puts "in verbose mode\n"; end
45
+
46
+ puts "Please enter the comma separated search tags like the following:"
47
+ if opts[:v] then puts "eg:\nterm1, term2, term3\n\n"; end
48
+
49
+ begin
50
+ tags = ((STDIN.gets).strip).split(', ')
51
+ puts "The following was captured: " + tags.to_s
52
+ puts "Is this correct? y/n"
53
+
54
+ input = (STDIN.gets).strip
55
+ if input == "y"
56
+ puts "Please enter the value you wish to store for this database entry:"
57
+ value = (STDIN.gets).strip
58
+
59
+ puts "The following was captured: " + value
60
+ puts "Is this correct? y/n"
61
+ input = (STDIN.gets).strip
62
+ if input == "y"
63
+ entry = {"tag" => tags, "value" => value}
64
+
65
+ if opts[:v] then puts "Storing the following in the database:\n" + entry.to_s; end
66
+
67
+ UsefulDB.add(entry, {})
68
+ UsefulDB.dbSave
69
+
70
+ else
71
+ puts "Exiting"
72
+ exit
73
+ end
74
+ else
75
+ puts "Exiting"
76
+ exit
77
+ end
78
+
79
+ rescue Exception => e
80
+ puts e.message
81
+ exit
82
+ end
83
+
84
+ end
85
+
86
+
87
+ end
88
+ end
89
+
90
+ end
@@ -0,0 +1,28 @@
1
+ require 'yaml'
2
+
3
+ module UsefulDB
4
+
5
+ class Settings
6
+ class << self
7
+
8
+ attr_accessor :data
9
+
10
+ def load(path)
11
+ @data = begin
12
+ YAML.load(File.open(path))
13
+ rescue ArgumentError => e
14
+ exit("Could not parse YAML: #{e.message}")
15
+ end
16
+ end
17
+
18
+ def save(newData, path)
19
+ @data = newData
20
+ f = File.open(path, "w")
21
+ f.write(@data.to_yaml)
22
+ f.close
23
+ end
24
+
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,108 @@
1
+ require 'usefuldb/exceptions'
2
+
3
+ module UsefulDB
4
+
5
+ class UsefulUtils
6
+ class << self
7
+
8
+ attr_accessor :data, :dbpath
9
+
10
+ #####################################################
11
+ def addColour(text, colour_code)
12
+ "\e[#{colour_code}m#{text}\e[0m"
13
+ end
14
+
15
+ def red(text); addColour(text, 31); end
16
+ def green(text); addColour(text, 32); end
17
+ def yellow(text); addColour(text, 33); end
18
+ def blue(text); addColour(text, 34); end
19
+ #####################################################
20
+
21
+
22
+ # Save the database to disk
23
+ def dbSave()
24
+ if @dbpath.nil?
25
+ #@dbpath = File.expand_path(File.dirname(__FILE__) + '/../../resources/db.yaml')
26
+ @dbpath = ENV['HOME'] + "/.usefuldb/db.yaml"
27
+ Settings.save(@data, @dbpath)
28
+ else
29
+ Settings.save(@data, @dbpath)
30
+ end
31
+ end
32
+
33
+
34
+ # Load the database from disk
35
+ def dbLoad()
36
+ #@dbpath = File.expand_path(File.dirname(__FILE__) + '/../../resources/db.yaml')
37
+ @dbpath = ENV['HOME'] + "/.usefuldb/db.yaml"
38
+ Settings.load(dbpath)
39
+ @data = Settings.data
40
+ end
41
+
42
+
43
+ # Return the number of elements in the database.
44
+ def count()
45
+ if @data.count == 0
46
+ raise EmptyDB, "The DB is currently empty."
47
+ else
48
+ return @data.count
49
+ end
50
+ end
51
+
52
+
53
+ # Add an element to the database
54
+ def add(hash, opts)
55
+ if @data.include?(hash) then raise EntryInDB, "Entry already in the DB"; else @data << hash; end
56
+ end
57
+
58
+
59
+ # Remove an element from the database
60
+ def remove(key, opts)
61
+ if @data.count == 0
62
+ raise EmptyDB, "You cannot call the remove function on an empty Database!"
63
+ elsif @data.count < key || key < 0
64
+ raise KeyOutOfBounds, "Key does not exist in the DB"
65
+ else
66
+ @data.delete_at(key)
67
+ end
68
+ end
69
+
70
+
71
+ # Setup the system for the first time
72
+ def setup()
73
+ resourceDir = ENV['HOME'] + "/.usefuldb/"
74
+ if File.directory?(resourceDir)
75
+ # The folder already exists, do nothing
76
+ else
77
+ # We need to create this folder and install the DB there.
78
+ FileUtils.mkdir(resourceDir)
79
+ dbpath = File.expand_path(File.dirname(__FILE__) + '/../resources/db.yaml')
80
+ FileUtils.cp(dbpath, resourceDir)
81
+ end
82
+ end
83
+
84
+
85
+ # Search for a tag in the DB
86
+ def search(tag)
87
+ msg = "Searching the database for tag: " + yellow(tag) + "\n"
88
+
89
+ @data.each do |db|
90
+ if db["tag"].include?(tag)
91
+ msg += "- Tags: " + db["tag"].to_s + "\n"
92
+ msg += "- Value: " + db["value"] + blue("\n##\n")
93
+ end
94
+ end
95
+ return msg
96
+ end
97
+
98
+
99
+ # List out all elements in the DB
100
+ def list
101
+ return @data
102
+ end
103
+
104
+
105
+ end
106
+ end
107
+
108
+ end
data/lib/usefuldb.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'usefuldb/settings'
2
+ require 'usefuldb/utilities'
3
+ require 'usefuldb/exceptions'
4
+ require 'usefuldb/gui'
5
+ require 'fileutils'
6
+
7
+ module UsefulDB
8
+ class << self
9
+
10
+ def add(hash, opts)
11
+ UsefulDB::UsefulUtils.add(hash, opts)
12
+ end
13
+
14
+ def count()
15
+ UsefulDB::UsefulUtils.count
16
+ end
17
+
18
+ def remove(key, opts)
19
+ UsefulDB::UsefulUtils.remove(key, opts)
20
+ end
21
+
22
+ def dbSave
23
+ UsefulDB::UsefulUtils.dbSave
24
+ end
25
+
26
+ def dbLoad
27
+ UsefulDB::UsefulUtils.dbLoad
28
+ end
29
+
30
+ def search(args)
31
+ return UsefulDB::UsefulUtils.search(args)
32
+ end
33
+
34
+ def setup
35
+ UsefulDB::UsefulUtils.setup
36
+ end
37
+
38
+ end
39
+ end
data/resources/db.yaml ADDED
@@ -0,0 +1,13 @@
1
+ ---
2
+ - tag:
3
+ - install
4
+ - rubygems
5
+ - website
6
+ - usefuldb
7
+ value: http://rubygems.org/usefuldb
8
+ - tag:
9
+ - search
10
+ - find
11
+ - website
12
+ - google
13
+ value: http://www.google.com
data/test/main_test.rb ADDED
@@ -0,0 +1,166 @@
1
+ require 'test/unit'
2
+ require 'rack/test'
3
+ require 'fileutils'
4
+ require 'usefuldb'
5
+
6
+ ENV['RACK_ENV'] = 'test'
7
+
8
+ #####################################################
9
+ def addColour(text, colour_code)
10
+ "\e[#{colour_code}m#{text}\e[0m"
11
+ end
12
+
13
+ def red(text); addColour(text, 31); end
14
+ def green(text); addColour(text, 32); end
15
+ def yellow(text); addColour(text, 33); end
16
+ def blue(text); addColour(text, 34); end
17
+ #####################################################
18
+
19
+
20
+ class UsefulDBTest < Test::Unit::TestCase
21
+
22
+ include Rack::Test::Methods
23
+
24
+ def setup
25
+ UsefulDB.dbLoad
26
+ end
27
+
28
+ #def teardown
29
+ # UsefulUtils.dbSave
30
+ #end
31
+
32
+
33
+ # Test to check adding of entries to the database
34
+ def test_add
35
+ puts yellow("## Executing test_add")
36
+
37
+ begin
38
+ puts "Check the starting count in the DB is 2 entries: " +
39
+ blue(assert_equal(2, UsefulDB.count()).to_s)
40
+
41
+ entry = {"tag" => ["install", "rubygems", "website", "usefuldb"], "value" => "http://rubygems.org/usefuldb"}
42
+ puts "Create a new entry: " + entry.inspect
43
+
44
+ puts "Test to check adding duplicate element to the DB fails"
45
+ UsefulDB.add(entry, {})
46
+
47
+ rescue UsefulDB::EmptyDB => e
48
+ assert(false, red(e.message))
49
+ rescue UsefulDB::EntryInDB => e
50
+ puts red(e.message) + green(" this is expected")
51
+ #puts e.backtrace
52
+ end
53
+
54
+ begin
55
+ puts "Write the DB structure back to disk"
56
+ UsefulDB.dbSave
57
+ UsefulDB.dbLoad
58
+
59
+ puts "Check the total number of entries in the DB is still 2 " +
60
+ blue(assert_equal(2, UsefulDB.count).to_s)
61
+ rescue UsefulDB::EmptyDB => e
62
+ assert(false, red(e.message))
63
+ end
64
+
65
+ msg = "Test to check adding new element to the DB succeeds: "
66
+ begin
67
+ entry2 = {"tag" => ["interesting", "gem", "website", "twitter"], "value" => "https://github.com/sferik/twitter/"}
68
+ puts "Creating another element" + entry2.inspect
69
+
70
+ UsefulDB.add(entry2, {})
71
+ puts msg + blue(assert_equal(3, UsefulDB.count).to_s)
72
+
73
+ rescue UsefulDB::EntryInDB => e
74
+ assert(false, red(e.message))
75
+ #puts e.backtrace
76
+ rescue UsefulDB::EmptyDB => e
77
+ assert(false, red(e.message))
78
+ end
79
+
80
+ puts green("test_add passed")
81
+ end
82
+
83
+
84
+ # Test to check saving entries to the database
85
+ def test_save
86
+ puts yellow("\n## Executing test_save")
87
+
88
+ msg = "Test to check adding new element to the DB succeeds: "
89
+ begin
90
+ entry = {"tag" => ["interesting", "gem", "website", "twitter"], "value" => "https://github.com/sferik/twitter/"}
91
+ puts "Creating another element" + entry.inspect
92
+
93
+ UsefulDB.add(entry, {})
94
+ puts msg + blue(assert_equal(3, UsefulDB.count).to_s)
95
+ rescue UsefulDB::EntryInDB => e
96
+ puts red(e.message) + green(" this is expected")
97
+ #puts e.backtrace
98
+ rescue UsefulDB::EmptyDB => e
99
+ assert(false, red(e.message))
100
+ end
101
+
102
+ begin
103
+ puts "Saving the DB"
104
+ UsefulDB.dbSave
105
+ UsefulDB.dbLoad
106
+
107
+ puts "Check the total number of entries in the DB is now 3 " +
108
+ blue(assert_equal(3, UsefulDB.count).to_s)
109
+ rescue UsefulDB::EmptyDB => e
110
+ assert(false, red(e.message))
111
+ end
112
+
113
+ begin
114
+ UsefulDB.remove(2, {})
115
+ UsefulDB.dbSave
116
+ rescue UsefulDB::KeyOutOfBounds => e
117
+ assert(false, red(e.message))
118
+ rescue UsefulDB::EmptyDB => e
119
+ assert(false, red(e.message))
120
+ end
121
+
122
+ puts green("test_save passed")
123
+ end
124
+
125
+
126
+ # Test to check removal of entries from the database
127
+ def test_remove()
128
+ puts yellow("\n## Executing test_remove")
129
+
130
+ begin
131
+ puts "Test to check adding new element to the DB succeeds"
132
+
133
+ entry = {"tag" => ["test"], "value" => "testvalue"}
134
+ entry2 = {"tag" => ["test2"], "value" => "testvalue2"}
135
+ puts "Creating another element" + entry.inspect + " and " + entry2.inspect
136
+
137
+ UsefulDB.add(entry, {})
138
+ UsefulDB.add(entry2, {})
139
+ rescue UsefulDB::EntryInDB => e
140
+ assert(false, red(e.message))
141
+ end
142
+
143
+ begin
144
+ puts "Saving the DB"
145
+ UsefulDB.dbSave
146
+ UsefulDB.dbLoad
147
+
148
+ puts "Removing those 2 entries from the database"
149
+ UsefulDB.remove(2, {})
150
+ puts blue(assert_equal(3, UsefulDB.count))
151
+ UsefulDB.remove(2, {})
152
+ puts blue(assert_equal(2, UsefulDB.count))
153
+
154
+ puts "Saving the DB"
155
+ UsefulDB.dbSave
156
+ rescue UsefulDB::KeyOutOfBounds => e
157
+ assert(false, red(e.message))
158
+ rescue UsefulDB::EmptyDB => e
159
+ assert(false, red(e.message))
160
+ end
161
+
162
+ puts green("test_remove passed")
163
+ end
164
+
165
+
166
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: usefuldb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Kirwan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rack-test
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: usefuldb is a simple database which is designed to store useful commands
47
+ and URLs
48
+ email:
49
+ - 00346128@mail.wit.ie
50
+ executables:
51
+ - usefuldb
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - lib/usefuldb.rb
56
+ - lib/usefuldb/utilities.rb
57
+ - lib/usefuldb/exceptions.rb
58
+ - lib/usefuldb/gui.rb
59
+ - lib/usefuldb/settings.rb
60
+ - bin/usefuldb
61
+ - Rakefile
62
+ - CHANGELOG
63
+ - COPYING
64
+ - README
65
+ - resources/db.yaml
66
+ - test/main_test.rb
67
+ homepage: http://rubygems.org/gems/usefuldb
68
+ licenses:
69
+ - CC BY-SA 3.0
70
+ post_install_message: ! 'usefuldb - simple commands and or urls database for storage
71
+ of useful information.
72
+
73
+
74
+ usefuldb is released under the creative commons attribution-sharealike 3.0 unported
75
+ (cc by-sa 3.0) licence.
76
+
77
+ for more information see: http://creativecommons.org/licenses/by-sa/3.0/
78
+
79
+ '
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: 1.8.7
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.24
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: A simple commands and URLs database for storage of useful information
101
+ test_files: []