ruby-notes 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/notes.rb +9 -0
- data/lib/notes.rb +35 -0
- data/lib/notes/controller/crud_controller.rb +63 -0
- data/lib/notes/controller/db_controller.rb +63 -0
- data/lib/notes/controller/option_parser.rb +18 -0
- data/lib/notes/export/base_export.rb +17 -0
- data/lib/notes/export/console_export.rb +38 -0
- data/lib/notes/export/csv_export.rb +27 -0
- data/lib/notes/export/html_export.rb +51 -0
- data/lib/notes/model/criteria.rb +16 -0
- data/lib/notes/model/note.rb +25 -0
- data/lib/notes/options/add_note.rb +13 -0
- data/lib/notes/options/all.rb +14 -0
- data/lib/notes/options/base_option.rb +17 -0
- data/lib/notes/options/console.rb +13 -0
- data/lib/notes/options/csv.rb +18 -0
- data/lib/notes/options/current.rb +13 -0
- data/lib/notes/options/description.rb +17 -0
- data/lib/notes/options/due_date.rb +17 -0
- data/lib/notes/options/find_note.rb +13 -0
- data/lib/notes/options/html.rb +17 -0
- data/lib/notes/options/option_consts.rb +17 -0
- data/lib/notes/options/options.rb +12 -0
- data/lib/notes/options/remove_note.rb +13 -0
- data/lib/notes/options/tag.rb +17 -0
- data/lib/notes/options/token.rb +17 -0
- data/lib/notes/options/update_note.rb +13 -0
- data/spec/criteria_spec.rb +16 -0
- data/spec/crud_spec.rb +32 -0
- data/spec/db_spec.rb +51 -0
- data/spec/export_spec.rb +84 -0
- data/spec/note_spec.rb +52 -0
- data/spec/options_spec.rb +86 -0
- metadata +101 -0
data/bin/notes.rb
ADDED
data/lib/notes.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'trollop'
|
2
|
+
require 'mongo'
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
require 'notes/export/base_export'
|
6
|
+
require 'notes/export/console_export'
|
7
|
+
require 'notes/export/html_export'
|
8
|
+
require 'notes/export/csv_export'
|
9
|
+
|
10
|
+
require 'notes/model/note'
|
11
|
+
require 'notes/model/criteria'
|
12
|
+
|
13
|
+
require 'notes/controller/option_parser'
|
14
|
+
require 'notes/controller/crud_controller'
|
15
|
+
require 'notes/controller/db_controller'
|
16
|
+
|
17
|
+
require 'notes/utils/array_util'
|
18
|
+
|
19
|
+
require 'notes/options/base_option'
|
20
|
+
require 'notes/options/add_note'
|
21
|
+
require 'notes/options/description'
|
22
|
+
require 'notes/options/due_date'
|
23
|
+
require 'notes/options/find_note'
|
24
|
+
require 'notes/options/token'
|
25
|
+
require 'notes/options/remove_note'
|
26
|
+
require 'notes/options/tag'
|
27
|
+
require 'notes/options/update_note'
|
28
|
+
require 'notes/options/html'
|
29
|
+
require 'notes/options/csv'
|
30
|
+
require 'notes/options/console'
|
31
|
+
require 'notes/options/options'
|
32
|
+
require 'notes/options/current'
|
33
|
+
require 'notes/options/option_consts'
|
34
|
+
require 'notes/options/all'
|
35
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Notes
|
2
|
+
class CRUDController
|
3
|
+
def initialize collection_name
|
4
|
+
@db_controller = DatabaseController.new collection_name
|
5
|
+
end
|
6
|
+
|
7
|
+
def perform_action options
|
8
|
+
init options
|
9
|
+
if @options[Options::ADD_NOTE]
|
10
|
+
create
|
11
|
+
elsif @options[Options::REMOVE_NOTE]
|
12
|
+
remove
|
13
|
+
elsif @options[Options::UPDATE_NOTE]
|
14
|
+
update
|
15
|
+
elsif @options[Options::FIND_NOTE]
|
16
|
+
find
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def init options
|
22
|
+
@options = options
|
23
|
+
@criterias = get_criterias
|
24
|
+
if options[Options::CONSOLE_EXPORT]
|
25
|
+
@export = ConsoleExport.new
|
26
|
+
elsif options[Options::HTML_EXPORT]
|
27
|
+
@export = HtmlExport.new options[Options::HTML_EXPORT]
|
28
|
+
elsif options[Options::CSV_EXPORT]
|
29
|
+
@export = CsvExport.new options[Options::CSV_EXPORT]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def create
|
34
|
+
@db_controller.create Note.new @options
|
35
|
+
end
|
36
|
+
|
37
|
+
def remove
|
38
|
+
@db_controller.remove @criterias
|
39
|
+
end
|
40
|
+
|
41
|
+
def update
|
42
|
+
@db_controller.update Note.new(@options), @criterias
|
43
|
+
end
|
44
|
+
|
45
|
+
def find
|
46
|
+
result_collection = @db_controller.find @criterias
|
47
|
+
if result_collection
|
48
|
+
puts @export.export_notes result_collection
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_criterias
|
53
|
+
result = []
|
54
|
+
criterias = [Options::TAG, Options::DESCRIPTION, Options::CURRENT, Options::DUE_DATE, Options::TOKEN , Options::ALL]
|
55
|
+
@options.each do |key, value|
|
56
|
+
if criterias.include?(key) && @options[key] && @options[key] != []
|
57
|
+
result << Criteria.new(key.to_s, value)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
result
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Notes
|
2
|
+
class DatabaseController
|
3
|
+
def initialize collection_name
|
4
|
+
@collection = Mongo::Connection.new('localhost').db('notes')[collection_name]
|
5
|
+
end
|
6
|
+
|
7
|
+
def find criterias
|
8
|
+
if should_apply_to_all?(criterias)
|
9
|
+
return get_all_records
|
10
|
+
end
|
11
|
+
@collection.find({'$and' => get_criterias_as_hashes(criterias)})
|
12
|
+
end
|
13
|
+
|
14
|
+
def find_one criterias
|
15
|
+
@collection.find_one({'$and' => get_criterias_as_hashes(criterias)})
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_all_records
|
19
|
+
@collection.find
|
20
|
+
end
|
21
|
+
|
22
|
+
def create note
|
23
|
+
@collection.insert(note.to_hash)
|
24
|
+
end
|
25
|
+
|
26
|
+
def remove criterias
|
27
|
+
if should_apply_to_all?(criterias)
|
28
|
+
remove_all
|
29
|
+
return
|
30
|
+
end
|
31
|
+
@collection.remove({'$and' => get_criterias_as_hashes(criterias)})
|
32
|
+
end
|
33
|
+
|
34
|
+
def remove_all
|
35
|
+
@collection.remove({})
|
36
|
+
end
|
37
|
+
|
38
|
+
def update note, criterias
|
39
|
+
criterias.each do |criteria|
|
40
|
+
@collection.update({'token' => note.token}, {'$set' => {criteria.name => criteria.value}})
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def should_apply_to_all?(criterias)
|
46
|
+
criteria_hashes = get_criterias_as_hashes criterias
|
47
|
+
criteria_hashes.each do |criteria|
|
48
|
+
if criteria[Notes::Options::ALL.to_s]
|
49
|
+
return true
|
50
|
+
end
|
51
|
+
end
|
52
|
+
false
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_criterias_as_hashes criterias
|
56
|
+
result = []
|
57
|
+
criterias.each do |criteria|
|
58
|
+
result << criteria.to_hash
|
59
|
+
end
|
60
|
+
result
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Notes
|
2
|
+
module Options
|
3
|
+
class OptionParser
|
4
|
+
def initialize(args)
|
5
|
+
@parsed_options = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def parse
|
9
|
+
@parsed_options = Trollop::options do
|
10
|
+
Options.get_options.each do |option|
|
11
|
+
opt option.name, option.description, option.options
|
12
|
+
end
|
13
|
+
end
|
14
|
+
@parsed_options
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Notes
|
2
|
+
class BaseExport
|
3
|
+
|
4
|
+
TOKEN = 'TOKEN'
|
5
|
+
TAG = 'TAG'
|
6
|
+
DESCRIPTION = 'DESCRIPTION'
|
7
|
+
DUE_DATE = 'DUE DATE'
|
8
|
+
|
9
|
+
private
|
10
|
+
def get_tags_string tags
|
11
|
+
if tags.kind_of? Array
|
12
|
+
return Notes::ArrayUtil.array_to_string tags
|
13
|
+
end
|
14
|
+
tags
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Notes
|
2
|
+
class ConsoleExport < BaseExport
|
3
|
+
TOKEN_FIELD_LENGTH = 20
|
4
|
+
TAG_FIELD_LENGTH = 25
|
5
|
+
DESCRIPTION_FIELD_LENGTH = 50
|
6
|
+
DUE_DATE_FIELD_LENGTH = 14
|
7
|
+
HEADER = '+' + '-' * (TOKEN_FIELD_LENGTH + TAG_FIELD_LENGTH + DESCRIPTION_FIELD_LENGTH + DUE_DATE_FIELD_LENGTH + 3) + '+'
|
8
|
+
|
9
|
+
def export_notes note_list
|
10
|
+
result = HEADER + "\n"
|
11
|
+
heading = center_in_container(TOKEN_FIELD_LENGTH, TOKEN) + center_in_container(TAG_FIELD_LENGTH, TAG)
|
12
|
+
heading += center_in_container(DESCRIPTION_FIELD_LENGTH, DESCRIPTION) + center_in_container(DUE_DATE_FIELD_LENGTH, DUE_DATE)
|
13
|
+
result += heading + '|' + "\n"
|
14
|
+
result += HEADER + "\n"
|
15
|
+
note_list.each do |note|
|
16
|
+
result += export_note note
|
17
|
+
result += "\n"
|
18
|
+
end
|
19
|
+
result += HEADER + "\n"
|
20
|
+
result
|
21
|
+
end
|
22
|
+
|
23
|
+
def export_note(note_hash)
|
24
|
+
result = center_in_container(TOKEN_FIELD_LENGTH, note_hash[Options::TOKEN.to_s])
|
25
|
+
result += center_in_container(TAG_FIELD_LENGTH, get_tags_string(note_hash[Options::TAG.to_s]))
|
26
|
+
result += center_in_container(DESCRIPTION_FIELD_LENGTH, note_hash[Options::DESCRIPTION.to_s])
|
27
|
+
result += center_in_container(DUE_DATE_FIELD_LENGTH, note_hash[Options::DUE_DATE.to_s])
|
28
|
+
result += '|'
|
29
|
+
result
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def center_in_container(container_length, content)
|
34
|
+
'|' + ' ' * ((container_length - content.length)/2.0).floor + content + ' ' * ((container_length - content.length)/2.0).ceil
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Notes
|
2
|
+
class CsvExport < BaseExport
|
3
|
+
CSV_VALUE_SEPARATOR = ', '
|
4
|
+
|
5
|
+
def initialize output_file_path
|
6
|
+
@file = File.new output_file_path, 'w+'
|
7
|
+
end
|
8
|
+
|
9
|
+
def export_notes notes_list
|
10
|
+
res = TOKEN + CSV_VALUE_SEPARATOR + TAG + CSV_VALUE_SEPARATOR + DESCRIPTION + CSV_VALUE_SEPARATOR + DUE_DATE + "\n"
|
11
|
+
notes_list.each do |note|
|
12
|
+
res += export_note note
|
13
|
+
res += "\n"
|
14
|
+
end
|
15
|
+
res.chomp! "\n"
|
16
|
+
@file.puts res
|
17
|
+
@file.close
|
18
|
+
end
|
19
|
+
|
20
|
+
def export_note note_hash
|
21
|
+
res = note_hash[Options::TOKEN.to_s] + CSV_VALUE_SEPARATOR + get_tags_string(note_hash[Options::TAG.to_s])
|
22
|
+
res += CSV_VALUE_SEPARATOR + '"' + note_hash[Options::DESCRIPTION.to_s] + '"'
|
23
|
+
res += CSV_VALUE_SEPARATOR + note_hash[Options::DUE_DATE.to_s]
|
24
|
+
res
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Notes
|
2
|
+
class HtmlExport < BaseExport
|
3
|
+
DOCTYPE = '<!DOCTYPE html>'
|
4
|
+
HTML = '<html>'
|
5
|
+
HEAD = '<head>'
|
6
|
+
BODY = '<body>'
|
7
|
+
ENCODING = '<meta http-equiv="content-type" content="text/html; charset=utf-8" />'
|
8
|
+
TITLE = '<title>Notes</title>'
|
9
|
+
TABLE = '<table style="text-align: center;">'
|
10
|
+
TABLE_ROW = '<tr>'
|
11
|
+
TABLE_CELL = '<td>'
|
12
|
+
|
13
|
+
def initialize output_file_path
|
14
|
+
@file = File.new output_file_path , 'w+'
|
15
|
+
end
|
16
|
+
|
17
|
+
def export_notes notes_list
|
18
|
+
res = TABLE + TABLE_ROW + TABLE_CELL + TOKEN + get_closing_tag(TABLE_CELL) + TABLE_CELL + TAG + get_closing_tag(TABLE_CELL)
|
19
|
+
res += TABLE_CELL + DESCRIPTION + get_closing_tag(TABLE_CELL) + TABLE_CELL + DUE_DATE + get_closing_tag(TABLE_CELL)
|
20
|
+
res += get_closing_tag(TABLE_ROW)
|
21
|
+
notes_list.each do |note|
|
22
|
+
res += export_note note
|
23
|
+
end
|
24
|
+
res += get_closing_tag TABLE
|
25
|
+
@file.puts(attach_headers res)
|
26
|
+
@file.close
|
27
|
+
end
|
28
|
+
|
29
|
+
def export_note note_hash
|
30
|
+
res = TABLE_ROW
|
31
|
+
res += TABLE_CELL + note_hash[Notes::Options::TOKEN.to_s] + get_closing_tag(TABLE_CELL)
|
32
|
+
res += TABLE_CELL + get_tags_string(note_hash[Notes::Options::TAG.to_s]) + get_closing_tag(TABLE_CELL)
|
33
|
+
res += TABLE_CELL + note_hash[Notes::Options::DESCRIPTION.to_s] + get_closing_tag(TABLE_CELL)
|
34
|
+
res += TABLE_CELL + note_hash[Notes::Options::DUE_DATE.to_s] + get_closing_tag(TABLE_CELL)
|
35
|
+
res += get_closing_tag(TABLE_ROW)
|
36
|
+
res
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def attach_headers html_body
|
41
|
+
res = DOCTYPE + HTML + HEAD + ENCODING
|
42
|
+
res += TITLE + get_closing_tag(HEAD)
|
43
|
+
res += BODY + html_body + get_closing_tag(BODY) + get_closing_tag(HTML)
|
44
|
+
res
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_closing_tag opening_tag
|
48
|
+
opening_tag.sub("<","</")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Notes
|
2
|
+
class Criteria
|
3
|
+
attr_reader :name, :value
|
4
|
+
def initialize(name, value)
|
5
|
+
@name = name
|
6
|
+
@value = value
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_hash
|
10
|
+
if @value.kind_of? Array
|
11
|
+
return {@name => (Notes::ArrayUtil.array_to_string @value)}
|
12
|
+
end
|
13
|
+
{@name => @value}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Notes
|
2
|
+
class Note
|
3
|
+
attr_reader :tag, :description, :due_date, :token
|
4
|
+
|
5
|
+
def initialize options
|
6
|
+
@tag = (options[Options::TAG] && options[Options::TAG] != [])? options[Options::TAG] : 'unspecified'
|
7
|
+
@description = options[Options::DESCRIPTION] ? options[Options::DESCRIPTION] : 'unspecified'
|
8
|
+
@due_date = options[Options::DUE_DATE] ? options[Options::DUE_DATE] : 'unspecified'
|
9
|
+
@token = options[Options::TOKEN] ? options[Options::TOKEN] : 'unspecified'
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_hash
|
13
|
+
result_hash = {}
|
14
|
+
result_hash[Options::TAG.to_s] = @tag
|
15
|
+
result_hash[Options::DESCRIPTION.to_s] = @description
|
16
|
+
result_hash[Options::DUE_DATE.to_s] = @due_date
|
17
|
+
result_hash[Options::TOKEN.to_s] = get_token.to_s
|
18
|
+
result_hash
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_token
|
22
|
+
@tag.hash * 1231 + @description.hash * 1237 + @due_date.hash * 37
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Notes
|
2
|
+
module Options
|
3
|
+
class CSV < BaseOption
|
4
|
+
def name
|
5
|
+
Notes::Options::CSV_EXPORT
|
6
|
+
end
|
7
|
+
|
8
|
+
def options
|
9
|
+
{ :type => String, :multi => false}
|
10
|
+
end
|
11
|
+
|
12
|
+
def description
|
13
|
+
'The result of this operation(if any) will be printed in the file specified in csv format.'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Notes
|
2
|
+
module Options
|
3
|
+
class Current < BaseOption
|
4
|
+
def name
|
5
|
+
Notes::Options::CURRENT
|
6
|
+
end
|
7
|
+
|
8
|
+
def description
|
9
|
+
'Specified action will only be applied to notes with due date that is after or coincides with the current day.'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Notes
|
2
|
+
module Options
|
3
|
+
class Description < BaseOption
|
4
|
+
def name
|
5
|
+
Notes::Options::DESCRIPTION
|
6
|
+
end
|
7
|
+
|
8
|
+
def description
|
9
|
+
'Specify the description for the note'
|
10
|
+
end
|
11
|
+
|
12
|
+
def options
|
13
|
+
{:type => String, :multi => false }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Notes
|
2
|
+
module Options
|
3
|
+
class DueDate < BaseOption
|
4
|
+
def name
|
5
|
+
Notes::Options::DUE_DATE
|
6
|
+
end
|
7
|
+
|
8
|
+
def description
|
9
|
+
'Specify the due date for the note'
|
10
|
+
end
|
11
|
+
|
12
|
+
def options
|
13
|
+
{:type => String, :multi => false }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Notes
|
2
|
+
module Options
|
3
|
+
class Html < BaseOption
|
4
|
+
def name
|
5
|
+
Notes::Options::HTML_EXPORT
|
6
|
+
end
|
7
|
+
|
8
|
+
def options
|
9
|
+
{ :type => String, :multi => false }
|
10
|
+
end
|
11
|
+
|
12
|
+
def description
|
13
|
+
'The result of this operation(if any) will be saved into the file given in html format'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Notes
|
2
|
+
module Options
|
3
|
+
ADD_NOTE = :add_note
|
4
|
+
REMOVE_NOTE = :remove_note
|
5
|
+
UPDATE_NOTE = :update_note
|
6
|
+
FIND_NOTE = :find_note
|
7
|
+
TAG = :tag
|
8
|
+
DESCRIPTION = :description
|
9
|
+
DUE_DATE = :due_date
|
10
|
+
TOKEN = :token
|
11
|
+
CONSOLE_EXPORT = :console
|
12
|
+
CSV_EXPORT = :csv
|
13
|
+
HTML_EXPORT = :html
|
14
|
+
CURRENT = :current
|
15
|
+
ALL = :all
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Notes
|
2
|
+
module Options
|
3
|
+
class Options
|
4
|
+
def self.get_options
|
5
|
+
options = [AddNote.new, RemoveNote.new, FindNote.new, UpdateNote.new]
|
6
|
+
options += [Tag.new, Description.new, DueDate.new, Token.new, Html.new]
|
7
|
+
options += [CSV.new, Console.new, Current.new, All.new]
|
8
|
+
options
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Notes
|
2
|
+
module Options
|
3
|
+
class Tag < BaseOption
|
4
|
+
def name
|
5
|
+
Notes::Options::TAG
|
6
|
+
end
|
7
|
+
|
8
|
+
def description
|
9
|
+
'Specified action only takes effect on notes with the tag given.'
|
10
|
+
end
|
11
|
+
|
12
|
+
def options
|
13
|
+
{:type => String, :multi => true}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Notes
|
2
|
+
module Options
|
3
|
+
class Token < BaseOption
|
4
|
+
def name
|
5
|
+
Notes::Options::TOKEN
|
6
|
+
end
|
7
|
+
|
8
|
+
def description
|
9
|
+
'Specified action is only applied to the note with the id given.'
|
10
|
+
end
|
11
|
+
|
12
|
+
def options
|
13
|
+
{ :type => String, :multi => false}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
describe Notes::Criteria do
|
2
|
+
describe 'initialization' do
|
3
|
+
it 'correctly initializes fields' do
|
4
|
+
criteria = Notes::Criteria.new 'name', 'value'
|
5
|
+
criteria.name.should eq 'name'
|
6
|
+
criteria.value.should eq 'value'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'to_hash' do
|
11
|
+
it 'correctly creates corresponding hash' do
|
12
|
+
criteria = Notes::Criteria.new 'name', 'value'
|
13
|
+
criteria.to_hash.should eq ({'name' => 'value'})
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/spec/crud_spec.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
describe Notes::CRUDController do
|
2
|
+
describe 'it correctly performs action' do
|
3
|
+
|
4
|
+
before do
|
5
|
+
@crud_controller = Notes::CRUDController.new 'test'
|
6
|
+
@db_controller = Notes::DatabaseController.new 'test'
|
7
|
+
end
|
8
|
+
|
9
|
+
after do
|
10
|
+
@crud_controller.perform_action({Notes::Options::REMOVE_NOTE => true, Notes::Options::ALL => true})
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'creates new note' do
|
14
|
+
@crud_controller.perform_action({Notes::Options::ADD_NOTE => true})
|
15
|
+
result_count = 0
|
16
|
+
@db_controller.get_all_records.each do |record|
|
17
|
+
result_count += 1
|
18
|
+
end
|
19
|
+
result_count.should eq 1
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'removes note' do
|
23
|
+
@crud_controller.perform_action({Notes::Options::ADD_NOTE => true, Notes::Options::TAG => 'to do'})
|
24
|
+
@crud_controller.perform_action({Notes::Options::REMOVE_NOTE => true, Notes::Options::TAG => 'to do'})
|
25
|
+
result_count = 0
|
26
|
+
@db_controller.get_all_records.each do |record|
|
27
|
+
result_count += 1
|
28
|
+
end
|
29
|
+
result_count.should eq 0
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/db_spec.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
describe 'works with db' do
|
2
|
+
|
3
|
+
before do
|
4
|
+
@db_controller = Notes::DatabaseController.new 'spec'
|
5
|
+
test_hash = {}
|
6
|
+
test_hash[Notes::Options::TAG] = 'to do'
|
7
|
+
test_hash[Notes::Options::DESCRIPTION] = 'description'
|
8
|
+
test_hash[Notes::Options::DUE_DATE] = '16.02.2012'
|
9
|
+
@note = Notes::Note.new(test_hash)
|
10
|
+
@db_controller.create @note
|
11
|
+
end
|
12
|
+
|
13
|
+
after do
|
14
|
+
@db_controller.remove_all
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'can insert and find notes notes successfully' do
|
18
|
+
search_criteria = [Notes::Criteria.new(Notes::Options::TAG.to_s, 'to do')]
|
19
|
+
search_criteria << Notes::Criteria.new(Notes::Options::DESCRIPTION.to_s, 'description')
|
20
|
+
search_criteria << Notes::Criteria.new(Notes::Options::DUE_DATE.to_s, '16.02.2012')
|
21
|
+
query_result = @db_controller.find search_criteria
|
22
|
+
result_length = 0
|
23
|
+
query_result.each do |result|
|
24
|
+
result_length += 1
|
25
|
+
end
|
26
|
+
result_length.should eq 1
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'can update note' do
|
30
|
+
inserted_note = @db_controller.find_one([Notes::Criteria.new(Notes::Options::TAG.to_s, 'to do')])
|
31
|
+
update_criteria = [Notes::Criteria.new(Notes::Options::TAG.to_s, 'other')]
|
32
|
+
@db_controller.update(Notes::Note.new({Notes::Options::TOKEN => inserted_note[Notes::Options::TOKEN.to_s]}), update_criteria)
|
33
|
+
query_result = @db_controller.find update_criteria
|
34
|
+
result_length = 0
|
35
|
+
query_result.each do |result|
|
36
|
+
result_length +=1
|
37
|
+
end
|
38
|
+
result_length.should eq 1
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'can remove note' do
|
42
|
+
criterias = [Notes::Criteria.new(Notes::Options::TAG.to_s, 'to do')]
|
43
|
+
@db_controller.remove criterias
|
44
|
+
query_result = @db_controller.find criterias
|
45
|
+
result_length = 0
|
46
|
+
query_result.each do |result|
|
47
|
+
result_length +=1
|
48
|
+
end
|
49
|
+
result_length.should eq 0
|
50
|
+
end
|
51
|
+
end
|
data/spec/export_spec.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
describe 'export' do
|
2
|
+
|
3
|
+
before do
|
4
|
+
@test_hash = {}
|
5
|
+
@test_hash[Notes::Options::TOKEN.to_s] = '1963438776275'
|
6
|
+
@test_hash[Notes::Options::TAG.to_s] = 'unspecified'
|
7
|
+
@test_hash[Notes::Options::DESCRIPTION.to_s] = 'asd'
|
8
|
+
@test_hash[Notes::Options::DUE_DATE.to_s] = 'unspecified'
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'console export' do
|
12
|
+
it 'renders properly single line output'do
|
13
|
+
console_export = Notes::ConsoleExport.new
|
14
|
+
console_export.export_notes([@test_hash]).should eq <<EXPORT
|
15
|
+
+----------------------------------------------------------------------------------------------------------------+
|
16
|
+
| TOKEN | TAG | DESCRIPTION | DUE DATE |
|
17
|
+
+----------------------------------------------------------------------------------------------------------------+
|
18
|
+
| 1963438776275 | unspecified | asd | unspecified |
|
19
|
+
+----------------------------------------------------------------------------------------------------------------+
|
20
|
+
EXPORT
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'renders properly complex output' do
|
24
|
+
console_export = Notes::ConsoleExport.new
|
25
|
+
console_export.export_notes([@test_hash,@test_hash,@test_hash]).should eq <<EXPORT
|
26
|
+
+----------------------------------------------------------------------------------------------------------------+
|
27
|
+
| TOKEN | TAG | DESCRIPTION | DUE DATE |
|
28
|
+
+----------------------------------------------------------------------------------------------------------------+
|
29
|
+
| 1963438776275 | unspecified | asd | unspecified |
|
30
|
+
| 1963438776275 | unspecified | asd | unspecified |
|
31
|
+
| 1963438776275 | unspecified | asd | unspecified |
|
32
|
+
+----------------------------------------------------------------------------------------------------------------+
|
33
|
+
EXPORT
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'html export' do
|
38
|
+
after do
|
39
|
+
File.delete "./out.html"
|
40
|
+
end
|
41
|
+
it 'renders properly single line output' do
|
42
|
+
html_export = Notes::HtmlExport.new './out.html'
|
43
|
+
html_export.export_notes [@test_hash]
|
44
|
+
IO.read("./out.html").should eq <<EXPORT
|
45
|
+
<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>Notes</title></head><body><table style="text-align: center;"><tr><td>TOKEN</td><td>TAG</td><td>DESCRIPTION</td><td>DUE DATE</td></tr><tr><td>1963438776275</td><td>unspecified</td><td>asd</td><td>unspecified</td></tr></table style="text-align: center;"></body></html>
|
46
|
+
EXPORT
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'renders complex output correctly' do
|
50
|
+
html_export = Notes::HtmlExport.new './out.html'
|
51
|
+
html_export.export_notes [@test_hash,@test_hash,@test_hash]
|
52
|
+
IO.read("./out.html").should eq <<EXPORT
|
53
|
+
<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>Notes</title></head><body><table style="text-align: center;"><tr><td>TOKEN</td><td>TAG</td><td>DESCRIPTION</td><td>DUE DATE</td></tr><tr><td>1963438776275</td><td>unspecified</td><td>asd</td><td>unspecified</td></tr><tr><td>1963438776275</td><td>unspecified</td><td>asd</td><td>unspecified</td></tr><tr><td>1963438776275</td><td>unspecified</td><td>asd</td><td>unspecified</td></tr></table style="text-align: center;"></body></html>
|
54
|
+
EXPORT
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'csv export' do
|
59
|
+
|
60
|
+
after do
|
61
|
+
File.delete './out.csv'
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'renders properly single line output' do
|
65
|
+
csv_export = Notes::CsvExport.new './out.csv'
|
66
|
+
csv_export.export_notes [@test_hash]
|
67
|
+
IO.read("./out.csv").should eq <<EXPORT
|
68
|
+
TOKEN, TAG, DESCRIPTION, DUE DATE
|
69
|
+
1963438776275, unspecified, "asd", unspecified
|
70
|
+
EXPORT
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'renders complex output correctly' do
|
74
|
+
csv_export = Notes::CsvExport.new './out.csv'
|
75
|
+
csv_export.export_notes [@test_hash, @test_hash, @test_hash]
|
76
|
+
IO.read("./out.csv").should eq <<EXPORT
|
77
|
+
TOKEN, TAG, DESCRIPTION, DUE DATE
|
78
|
+
1963438776275, unspecified, "asd", unspecified
|
79
|
+
1963438776275, unspecified, "asd", unspecified
|
80
|
+
1963438776275, unspecified, "asd", unspecified
|
81
|
+
EXPORT
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/spec/note_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
describe Notes::Note do
|
2
|
+
before do
|
3
|
+
@test_hash = {}
|
4
|
+
end
|
5
|
+
|
6
|
+
describe 'initialization' do
|
7
|
+
it 'can be initialized with default args' do
|
8
|
+
note = Notes::Note.new(@test_hash)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'gets default values if none were specified ' do
|
12
|
+
note = Notes::Note.new(@test_hash)
|
13
|
+
note.tag.should eq 'unspecified'
|
14
|
+
note.description.should eq 'unspecified'
|
15
|
+
note.due_date.should eq 'unspecified'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'correctly reads passed values' do
|
19
|
+
@test_hash[Notes::Options::TAG] = ['to do']
|
20
|
+
@test_hash[Notes::Options::DESCRIPTION] = 'description'
|
21
|
+
@test_hash[Notes::Options::DUE_DATE] = '18.02.2012'
|
22
|
+
note = Notes::Note.new @test_hash
|
23
|
+
note.tag.should eq ['to do']
|
24
|
+
note.description.should eq 'description'
|
25
|
+
note.due_date.should eq '18.02.2012'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'to_hash' do
|
30
|
+
it 'correctly creates corresponding hash when no parameters are specified' do
|
31
|
+
note = Notes::Note.new(@test_hash)
|
32
|
+
@test_hash[Notes::Options::TAG.to_s] = 'unspecified'
|
33
|
+
@test_hash[Notes::Options::DESCRIPTION.to_s] = 'unspecified'
|
34
|
+
@test_hash[Notes::Options::DUE_DATE.to_s] = 'unspecified'
|
35
|
+
@test_hash[Notes::Options::TOKEN.to_s] = note.get_token.to_s
|
36
|
+
note.to_hash.should eq @test_hash
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'correctly creates corresponding has when parameters are given' do
|
40
|
+
@test_hash[Notes::Options::TAG] = ['to do']
|
41
|
+
@test_hash[Notes::Options::DESCRIPTION] = 'description'
|
42
|
+
@test_hash[Notes::Options::DUE_DATE] = '18.02.2012'
|
43
|
+
note = Notes::Note.new @test_hash
|
44
|
+
test_against_hash = {}
|
45
|
+
test_against_hash[Notes::Options::TAG.to_s] = ['to do']
|
46
|
+
test_against_hash[Notes::Options::DESCRIPTION.to_s] = 'description'
|
47
|
+
test_against_hash[Notes::Options::DUE_DATE.to_s] = '18.02.2012'
|
48
|
+
test_against_hash[Notes::Options::TOKEN.to_s] = note.get_token.to_s
|
49
|
+
note.to_hash.should eq test_against_hash
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
describe 'Options' do
|
2
|
+
describe 'initialization' do
|
3
|
+
it 'returns name, description and type correctly' do
|
4
|
+
add_note = Notes::Options::AddNote.new
|
5
|
+
add_note.name.should eq Notes::Options::ADD_NOTE
|
6
|
+
add_note.description.should eq 'Add a new note with the parameters specified'
|
7
|
+
add_note.options.should eq ({:type => nil, :multi => false})
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'returns name, description and type correctly' do
|
11
|
+
add_note = Notes::Options::Console.new
|
12
|
+
add_note.name.should eq Notes::Options::CONSOLE_EXPORT
|
13
|
+
add_note.description.should eq 'The result of this operation(if any) will be printed in the console'
|
14
|
+
add_note.options.should eq ({:type => nil, :multi => false})
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'returns name, description and type correctly' do
|
18
|
+
add_note = Notes::Options::CSV.new
|
19
|
+
add_note.name.should eq Notes::Options::CSV_EXPORT
|
20
|
+
add_note.description.should eq 'The result of this operation(if any) will be printed in the file specified in csv format.'
|
21
|
+
add_note.options.should eq ({:type => String, :multi => false})
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns name, description and type correctly' do
|
25
|
+
add_note = Notes::Options::Current.new
|
26
|
+
add_note.name.should eq Notes::Options::CURRENT
|
27
|
+
add_note.description.should eq 'Specified action will only be applied to notes with due date that is after or coincides with the current day.'
|
28
|
+
add_note.options.should eq ({:type => nil, :multi => false})
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns name, description and type correctly' do
|
32
|
+
add_note = Notes::Options::Description.new
|
33
|
+
add_note.name.should eq Notes::Options::DESCRIPTION
|
34
|
+
add_note.description.should eq 'Specify the description for the note'
|
35
|
+
add_note.options.should eq ({:type => String, :multi => false})
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'returns name, description and type correctly' do
|
39
|
+
add_note = Notes::Options::DueDate.new
|
40
|
+
add_note.name.should eq Notes::Options::DUE_DATE
|
41
|
+
add_note.description.should eq 'Specify the due date for the note'
|
42
|
+
add_note.options.should eq ({:type => String, :multi => false})
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'returns name, description and type correctly' do
|
46
|
+
add_note = Notes::Options::FindNote.new
|
47
|
+
add_note.name.should eq Notes::Options::FIND_NOTE
|
48
|
+
add_note.description.should eq 'Finds the note specified.'
|
49
|
+
add_note.options.should eq ({:type => nil, :multi => false})
|
50
|
+
end
|
51
|
+
it 'returns name, description and type correctly' do
|
52
|
+
add_note = Notes::Options::Html.new
|
53
|
+
add_note.name.should eq Notes::Options::HTML_EXPORT
|
54
|
+
add_note.description.should eq 'The result of this operation(if any) will be saved into the file given in html format'
|
55
|
+
add_note.options.should eq ({:type => String, :multi => false})
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'returns name, description and type correctly' do
|
59
|
+
add_note = Notes::Options::Token.new
|
60
|
+
add_note.name.should eq Notes::Options::TOKEN
|
61
|
+
add_note.description.should eq 'Specified action is only applied to the note with the id given.'
|
62
|
+
add_note.options.should eq ({:type => String, :multi => false})
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'returns name, description and type correctly' do
|
66
|
+
add_note = Notes::Options::RemoveNote.new
|
67
|
+
add_note.name.should eq Notes::Options::REMOVE_NOTE
|
68
|
+
add_note.description.should eq 'Removes the specified note'
|
69
|
+
add_note.options.should eq ({:type => nil, :multi => false})
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'returns name, description and type correctly' do
|
73
|
+
add_note = Notes::Options::Tag.new
|
74
|
+
add_note.name.should eq Notes::Options::TAG
|
75
|
+
add_note.description.should eq 'Specified action only takes effect on notes with the tag given.'
|
76
|
+
add_note.options.should eq ({:type => String, :multi => true})
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'returns name, description and type correctly' do
|
80
|
+
add_note = Notes::Options::UpdateNote.new
|
81
|
+
add_note.name.should eq Notes::Options::UPDATE_NOTE
|
82
|
+
add_note.description.should eq 'Update the note given'
|
83
|
+
add_note.options.should eq ({:type => nil, :multi => false})
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-notes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Martin Asenov
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-02-17 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Ruby notes is a console lne tool that let you keep reminders/notes and afterwards search within them and export the result to various formats.
|
22
|
+
email: asenov.m@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- bin/notes.rb
|
31
|
+
- lib/notes.rb
|
32
|
+
- lib/notes/controller/crud_controller.rb
|
33
|
+
- lib/notes/controller/db_controller.rb
|
34
|
+
- lib/notes/controller/option_parser.rb
|
35
|
+
- lib/notes/export/base_export.rb
|
36
|
+
- lib/notes/export/console_export.rb
|
37
|
+
- lib/notes/export/csv_export.rb
|
38
|
+
- lib/notes/export/html_export.rb
|
39
|
+
- lib/notes/model/note.rb
|
40
|
+
- lib/notes/model/criteria.rb
|
41
|
+
- lib/notes/options/add_note.rb
|
42
|
+
- lib/notes/options/all.rb
|
43
|
+
- lib/notes/options/base_option.rb
|
44
|
+
- lib/notes/options/console.rb
|
45
|
+
- lib/notes/options/csv.rb
|
46
|
+
- lib/notes/options/current.rb
|
47
|
+
- lib/notes/options/description.rb
|
48
|
+
- lib/notes/options/due_date.rb
|
49
|
+
- lib/notes/options/find_note.rb
|
50
|
+
- lib/notes/options/html.rb
|
51
|
+
- lib/notes/options/option_consts.rb
|
52
|
+
- lib/notes/options/options.rb
|
53
|
+
- lib/notes/options/remove_note.rb
|
54
|
+
- lib/notes/options/tag.rb
|
55
|
+
- lib/notes/options/token.rb
|
56
|
+
- lib/notes/options/update_note.rb
|
57
|
+
- spec/criteria_spec.rb
|
58
|
+
- spec/crud_spec.rb
|
59
|
+
- spec/db_spec.rb
|
60
|
+
- spec/export_spec.rb
|
61
|
+
- spec/note_spec.rb
|
62
|
+
- spec/options_spec.rb
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: https://github.com/asenovm/Ruby-Notes
|
65
|
+
licenses: []
|
66
|
+
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
requirements: []
|
89
|
+
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.3.7
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: console line tool that let you easily keep reminders/notes.
|
95
|
+
test_files:
|
96
|
+
- spec/criteria_spec.rb
|
97
|
+
- spec/crud_spec.rb
|
98
|
+
- spec/db_spec.rb
|
99
|
+
- spec/export_spec.rb
|
100
|
+
- spec/note_spec.rb
|
101
|
+
- spec/options_spec.rb
|