rtodotxt 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 21ba6fcc82ae122035a4bd878dba4c5d03602f0b
4
+ data.tar.gz: 526dc4fe246aedfb54ef95d00a899e33a1ffd5c4
5
+ SHA512:
6
+ metadata.gz: 1c005c02f8b20f03e8c6190571afffda2e30693f5decf1e31696fdfa3cdcba737d81de48c6a21ec579b86e526bbe72379c11e9cfe63b63baac1b3b5ddd6e0d86
7
+ data.tar.gz: 5cdb916685eb8568ce469696ba77f1ad5a587324eaa07d4a0cb8892921963b6a740f04396b06cd81b438a4d19c612adef91828344f2245b4d0e72645fe65c5a2
data/.autotest ADDED
@@ -0,0 +1,2 @@
1
+ require "autotest/growl"
2
+ require "autotest/fsevent"
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rtodotxt.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Philipp Fehre
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ Rtodotxt
2
+ ========
3
+ Work with [todo.txt](http://todotxt.com/) files from ruby.
4
+
5
+ Usage
6
+ -----
7
+ Simply work with any todo.txt formatted file
8
+
9
+ require 'rtodotxt'
10
+
11
+ list = Rtodotxt::List.new(File.read('./todo.txt')) # load list
12
+ list.first # first todo
13
+ list.first.done! # mark first todo done
14
+ list.first.done? # => true
15
+
16
+ list.map(&:contexts).flatten.uniq # get all contexts
17
+ list.map(&:projects).flatten.uniq # get all projects
18
+
19
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/rtodotxt.rb ADDED
@@ -0,0 +1,99 @@
1
+ require "rtodotxt/version"
2
+ require "date"
3
+
4
+ module Rtodotxt
5
+
6
+ class List < Array
7
+
8
+ def initialize list
9
+ super self.class.gen_list_from_string list
10
+ end
11
+
12
+ # Split a string by each line and generate an array of todos
13
+ def read str
14
+ super self.class.gen_list_from_string str
15
+ end
16
+
17
+ # Join the todo text together to return the list
18
+ def print
19
+ str = self.map { |t| t.text }.join "\n"
20
+ # the result must end with a newline!
21
+ str << "\n"
22
+ end
23
+
24
+ def self.gen_list_from_string str
25
+ str.split( "\n" ).map! do |line|
26
+ Todo.new line unless line.empty?
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ class Todo
33
+ include Comparable
34
+ attr_accessor :text
35
+
36
+ def initialize text
37
+ @text = text
38
+ end
39
+
40
+ # Compare todos, the order is reversed so that a printed list is in correct
41
+ # order
42
+ def <=> a_todo
43
+ if done?
44
+ a_todo.done? ? 0 : 1
45
+ elsif a_todo.prio.empty? || prio.empty?
46
+ if a_todo.prio.empty? && prio.empty?
47
+ 0
48
+ elsif a_todo.prio.empty?
49
+ -1
50
+ else
51
+ 1
52
+ end
53
+ else
54
+ prio <=> a_todo.prio
55
+ end
56
+ end
57
+
58
+ def done!
59
+ @text = "x #{Date.today.to_s} " + @text.gsub( /\(.\)\s/, '' ) unless done?
60
+ # return self to allow method chaining
61
+ self
62
+ end
63
+
64
+ def prio! p
65
+ if p == ""
66
+ # Remove the priority
67
+ @text.gsub! /\(.\)\s/, ''
68
+ elsif p.match /\A.\Z/
69
+ # Single char to be used as priority
70
+ @text = "(#{p.match(/\A.\Z/)[0].upcase}) #{@text}"
71
+ else
72
+ raise ArgumentError, "Illegal priority", caller
73
+ end
74
+ end
75
+
76
+ def prio
77
+ # Match the priority from the string
78
+ prio = @text.match(/\((.)\)/)
79
+ prio.nil? ? "" : prio[1]
80
+ end
81
+
82
+ def done?
83
+ # if todo text starts with x than it is done
84
+ @text.match( /\Ax\s/ ).nil? ? false : true
85
+ end
86
+
87
+ def contexts
88
+ contexts = @text.scan /@(\S+)/
89
+ contexts.uniq.flatten
90
+ end
91
+
92
+ def projects
93
+ projects = @text.scan /\+(\S+)/
94
+ projects.uniq.flatten
95
+ end
96
+
97
+ end
98
+
99
+ end
@@ -0,0 +1,3 @@
1
+ module Rtodotxt
2
+ VERSION = "0.0.2"
3
+ end
data/rtodotxt.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rtodotxt/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Philipp Fehre"]
6
+ gem.email = ["philipp.fehre@googlemail.com"]
7
+ gem.description = %q{Working with todo.txt file in ruby}
8
+ gem.summary = %q{Use a todotxt formatted file in ruby}
9
+ gem.homepage = "http://github.com/sideshowcoder/rtodotxt"
10
+
11
+ # Development dependencies
12
+ gem.add_development_dependency "rspec", "~> 2.13.0"
13
+
14
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ gem.files = `git ls-files`.split("\n")
16
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ gem.name = "rtodotxt"
18
+ gem.require_paths = ["lib"]
19
+ gem.version = Rtodotxt::VERSION
20
+ end
@@ -0,0 +1,150 @@
1
+ require "spec_helper"
2
+
3
+ a_list = "(A) Buy milk @supermarket +groceries
4
+ (C) Fix rtodotxt
5
+ (B) get phil to organize +bbq
6
+ (A) Buy bread @baker +groceries
7
+ Get some coffee +groceries
8
+ "
9
+
10
+ a_list_sorted = "(A) Buy milk @supermarket +groceries
11
+ (A) Buy bread @baker +groceries
12
+ (B) get phil to organize +bbq
13
+ (C) Fix rtodotxt
14
+ Get some coffee +groceries
15
+ "
16
+
17
+ b_list = "(C) Fix rtodotxt
18
+ x 2012-03-13 get phil to organize +bbq
19
+ (A) Buy bread @baker +groceries
20
+ x 2012-03-13 Buy milk @supermarket +groceries
21
+ Get some coffee +groceries
22
+ "
23
+
24
+ b_list_sorted = "(A) Buy bread @baker +groceries
25
+ (C) Fix rtodotxt
26
+ Get some coffee +groceries
27
+ x 2012-03-13 Buy milk @supermarket +groceries
28
+ x 2012-03-13 get phil to organize +bbq
29
+ "
30
+
31
+
32
+ a_todo = "Buy milk @supermarket +groceries"
33
+ a_todo_prio = "(A) Buy milk @supermarket +groceries"
34
+ a_todo_done = "x #{Date.today.to_s} Buy milk @supermarket +groceries"
35
+
36
+ b_todo = "Get some coffee +groceries"
37
+ b_todo_done = "x #{Date.today.to_s} Get some coffee +groceries"
38
+ b_todo_prio = "(A) Get some coffee +groceries"
39
+
40
+
41
+ describe Rtodotxt::List do
42
+
43
+ it "should read a string" do
44
+ tl = Rtodotxt::List.new a_list
45
+ tl.print.should eql a_list
46
+ end
47
+
48
+ it "should generate enumerable todos" do
49
+ tl = Rtodotxt::List.new a_list
50
+ tl.each do |t|
51
+ t.class.should eql Rtodotxt::Todo
52
+ end
53
+ end
54
+
55
+ it "should sort a list by priority" do
56
+ tl = Rtodotxt::List.new a_list
57
+ tl_sorted = Rtodotxt::List.new a_list_sorted
58
+ tl.sort.should == tl_sorted
59
+ end
60
+
61
+ it "should sort a list by done" do
62
+ tl = Rtodotxt::List.new b_list
63
+ tl_sorted = Rtodotxt::List.new b_list_sorted
64
+ tl.sort.should == tl_sorted
65
+ end
66
+
67
+ end
68
+
69
+ describe Rtodotxt::Todo do
70
+
71
+ it "should initialize with text" do
72
+ t = Rtodotxt::Todo.new a_todo
73
+ t.text.should eql a_todo
74
+ end
75
+
76
+ it "should mark as done" do
77
+ t = Rtodotxt::Todo.new b_todo
78
+ t.done!.text.should eql b_todo_done
79
+ end
80
+
81
+ it "should not change a done todo if mark is done is called" do
82
+ t = Rtodotxt::Todo.new b_todo_done
83
+ t.done!.text.should eql b_todo_done
84
+ end
85
+
86
+ it "should mark as done with priority removed" do
87
+ t = Rtodotxt::Todo.new a_todo_prio
88
+ t.done!.text.should eql a_todo_done
89
+ end
90
+
91
+ it "should set priority" do
92
+ t = Rtodotxt::Todo.new b_todo
93
+ t.prio!("A").should eql b_todo_prio
94
+ end
95
+
96
+ it "should unset priority" do
97
+ t = Rtodotxt::Todo.new b_todo_prio
98
+ t.prio!("").should eql b_todo
99
+ end
100
+
101
+ it "should raise Argument error on illegal priority" do
102
+ t = Rtodotxt::Todo.new b_todo_prio
103
+ lambda { t.prio!("ABC") }.should raise_error
104
+ end
105
+
106
+ it "should give its contexts" do
107
+ t = Rtodotxt::Todo.new "(C) +bbq @supermarket go +get some +bbq stuff @home for @home"
108
+ t.contexts.should eql ["supermarket", "home"]
109
+ end
110
+
111
+ it "should return its projects" do
112
+ t = Rtodotxt::Todo.new "(C) +bbq @supermarket go +get some +bbq stuff @home for @home"
113
+ t.projects.should eql ["bbq", "get"]
114
+ end
115
+
116
+ it "should return its priority" do
117
+ t = Rtodotxt::Todo.new "(C) something"
118
+ t.prio.should eql "C"
119
+ end
120
+
121
+ it "should return empty string for no priority" do
122
+ t = Rtodotxt::Todo.new "something"
123
+ t.prio.should eql ""
124
+ end
125
+
126
+
127
+ it "should determine if a todo is done" do
128
+ t = Rtodotxt::Todo.new a_todo_done
129
+ t.done?.should eql true
130
+ t = Rtodotxt::Todo.new a_todo
131
+ t.done?.should eql false
132
+ end
133
+
134
+ it "should evaluate lower prio to be smaller" do
135
+ t_c = Rtodotxt::Todo.new "(C) something"
136
+ t_b = Rtodotxt::Todo.new "(B) something"
137
+ t_c.should be > t_b
138
+ end
139
+
140
+ it "should evaluate lower equal and bigger accordingly" do
141
+ t_d = Rtodotxt::Todo.new a_todo_done
142
+ t_p = Rtodotxt::Todo.new a_todo_prio
143
+ t_n = Rtodotxt::Todo.new a_todo
144
+ t_d.should be > t_p
145
+ t_d.should be > t_n
146
+ t_p.should be < t_n
147
+ t_p.should be < t_d
148
+ end
149
+
150
+ end
@@ -0,0 +1,7 @@
1
+ require "bundler/setup"
2
+
3
+ require "rtodotxt"
4
+
5
+ RSpec.configure do |config|
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rtodotxt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Philipp Fehre
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.13.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.13.0
27
+ description: Working with todo.txt file in ruby
28
+ email:
29
+ - philipp.fehre@googlemail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .autotest
35
+ - .gitignore
36
+ - .rspec
37
+ - Gemfile
38
+ - LICENSE
39
+ - README.md
40
+ - Rakefile
41
+ - lib/rtodotxt.rb
42
+ - lib/rtodotxt/version.rb
43
+ - rtodotxt.gemspec
44
+ - spec/rtodotxt_spec.rb
45
+ - spec/spec_helper.rb
46
+ homepage: http://github.com/sideshowcoder/rtodotxt
47
+ licenses: []
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.0.0
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Use a todotxt formatted file in ruby
69
+ test_files:
70
+ - spec/rtodotxt_spec.rb
71
+ - spec/spec_helper.rb