what_now 0.0.4 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22e94ada9391a63cbcf79bb1d3a4ca9f6f6962e2
4
- data.tar.gz: ad69ca1e8fac29ee9e76c27177e98c082a27b6b2
3
+ metadata.gz: c129b4ea1aed4491608b1aa54462eb469f06f69b
4
+ data.tar.gz: 25a68c0f5b0fd9048c495cdb18447050f846a43f
5
5
  SHA512:
6
- metadata.gz: 48e5808a0a2fa8f506ebc1db6ea30db35d0061393aba017dcde97c21b8986326eb750edefcbcb8fbffae8310d042aa082db65fb882f99b7d4a2341db4d80af4d
7
- data.tar.gz: 9bd24a4355ee83214fcc4a68c1a5e266844e2ed574f19678ae30c95b754de423b40aa2e5ab3a1ed75e948e5fd911614308155781912f0bb00f72abaf07417d3d
6
+ metadata.gz: 16dec7d805e9ba4759b3b2835b597e7e31f57bed8828b74dbd3d00d8a8116db1e0d1425e881e12a1027c06aa962ea51c73ad3386abdf8fb3b5748107cab4652a
7
+ data.tar.gz: 73ef733a215b66af92f7d3fadbc9b9deaed33be14f273480a4601273d2650a289ae05a29f17969e510e9e4a6855e08afb9c61203466ee2706fb8846cb1acb739
data/bin/wnow CHANGED
@@ -13,13 +13,12 @@ class Wnow < Thor
13
13
  def find
14
14
  dir = options[:dir] || Dir.pwd
15
15
  ext = options[:ext] ? "/**/*.#{options[:ext]}" : '/**/*.*'
16
- WhatNow.ignorecase if options[:ignorecase]
17
- todos = WhatNow.search_directory(dir + ext)
18
- todos.each do |t|
19
- printable_path = options[:dir] ? t.path : t.path[Dir.pwd.length+1..t.path.length]
20
- puts t.text.red.bold
21
- puts "at line #{t.line.to_s.blue} in #{printable_path.blue.underline}"
22
- puts
16
+ creator = TodoCreator.new(
17
+ ignorecase: options[:ignorecase],
18
+ pretty: STDOUT.tty?)
19
+ TodoFinder.new(dir + ext, creator).find.each do |todo|
20
+ puts todo
21
+ puts if STDOUT.tty?
23
22
  end
24
23
  end
25
24
  end
data/lib/todo.rb ADDED
@@ -0,0 +1,24 @@
1
+ # Contains the abstractions related
2
+ # to todo's objects
3
+ require 'colorize'
4
+
5
+ class Todo
6
+ attr_reader :text, :path, :line
7
+
8
+ def initialize text, path, line
9
+ @text = text
10
+ @path = path
11
+ @line = line
12
+ end
13
+
14
+ def to_s
15
+ "#{@text} at line #{@line} in #{@path}"
16
+ end
17
+ end
18
+
19
+ class PrettyTodo < Todo
20
+ def to_s
21
+ "#{@text.red.bold}\nat line #{@line.to_s.blue} in #{@path.blue.underline}"
22
+ end
23
+ end
24
+
data/lib/what_now.rb CHANGED
@@ -1,34 +1,45 @@
1
1
  # What_now definitions
2
2
 
3
3
  require 'ptools'
4
+ require 'todo'
4
5
 
5
- # TODO this struct should know how to print itself
6
- Todo = Struct.new :text, :path, :line
6
+ class TodoCreator
7
+ def initialize(opts={})
8
+ pretty = opts.fetch(:pretty, true)
9
+ @ignorecase = opts[:ignorecase]
10
+ @todo_class = pretty ? PrettyTodo : Todo
11
+ end
7
12
 
8
- module WhatNow
9
- class << self
13
+ def match(line, path, line_number)
14
+ regex = @ignorecase ? /TODO:?\s*(.+)$/i : /TODO:?\s*(.+)$/
15
+ text = regex.match(line)
16
+ @todo_class.new(text[1], shortened_path(path), line_number) if text
17
+ rescue ArgumentError
18
+ nil
19
+ end
10
20
 
11
- def ignorecase
12
- @ignorecase = true
13
- end
21
+ private
22
+ def shortened_path(path)
23
+ path[Dir.pwd.length+1..path.length]
24
+ end
25
+ end
14
26
 
15
- def search_line(line, line_number, path)
16
- regex = @ignorecase ? /TODO:?\s*(.+)$/i : /TODO:?\s*(.+)$/
17
- text = regex.match(line)
18
- Todo.new(text[1], line_number, path) if text
27
+ class TodoFinder
28
+ def initialize(pattern, creator)
29
+ @paths = Dir[pattern].delete_if do |path|
30
+ File.directory?(path) || File.binary?(path)
19
31
  end
32
+ @creator = creator
33
+ end
20
34
 
21
- def search_file(path)
22
- return [] if File.binary? path
23
- File.open(path).each_with_index.map do |line, i|
24
- search_line(line, path, i+1)
25
- end.delete_if { |l| l.nil? }
26
- end
35
+ def find
36
+ @paths.flat_map { |file| search_file(file) }
37
+ end
27
38
 
28
- def search_directory(pattern)
29
- Dir[pattern].flat_map do |file|
30
- search_file(file)
31
- end
32
- end
39
+ private
40
+ def search_file(path)
41
+ File.open(path).each_with_index.map do |line, i|
42
+ @creator.match(line, path, i+1)
43
+ end.delete_if { |l| l.nil? }
33
44
  end
34
45
  end
data/spec/todo_spec.rb ADDED
@@ -0,0 +1,33 @@
1
+ require_relative 'spec_helper'
2
+ require 'colorize'
3
+
4
+ describe Todo do
5
+ subject { Todo.new('do stuff', './stuff', 10) }
6
+
7
+ describe 'public interface' do
8
+ it { subject.must_respond_to :text }
9
+ it { subject.must_respond_to :path }
10
+ it { subject.must_respond_to :line }
11
+ end
12
+
13
+ describe '#to_s' do
14
+ it 'should respond with a simple task description' do
15
+ subject.to_s.must_equal "do stuff at line 10 in ./stuff"
16
+ end
17
+ end
18
+ end
19
+
20
+ describe PrettyTodo do
21
+ subject { PrettyTodo.new('do stuff', './stuff', 10) }
22
+
23
+ it 'shoul be a Todo' do
24
+ subject.must_be_kind_of Todo
25
+ end
26
+
27
+ it 'correctly formats the output' do
28
+ output = subject.text.red.bold + "\n"
29
+ output += "at line #{subject.line.to_s.blue} in #{subject.path.blue.underline}"
30
+ subject.to_s.must_equal output
31
+ end
32
+ end
33
+
@@ -1,57 +1,63 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
- describe Todo do
4
- describe 'public interface' do
5
- subject { Todo.new('do stuff', './stuff', 10) }
6
- it { subject.must_respond_to :text }
7
- it { subject.must_respond_to :path }
8
- it { subject.must_respond_to :line }
9
- end
10
- end
11
-
12
- describe WhatNow do
13
- describe '#search_line' do
3
+ describe TodoCreator do
4
+ describe '#match' do
14
5
  describe 'with case sensitivity' do
15
6
  subject do
16
- WhatNow.search_line('TODO this is a todo', 1, '.')
7
+ TodoCreator.new
17
8
  end
18
9
 
19
10
  it 'correctly extracts text from todo' do
20
- subject.text.must_match 'this is a todo'
11
+ subject.match('TODO this is a todo', '.', 1)
12
+ .text.must_match 'this is a todo'
21
13
  end
22
14
 
23
15
  it 'returns nil with a non todo line' do
24
- WhatNow.search_line('this is not a todo', 1, '.')
16
+ subject.match('this is not a todo', '.', 1)
25
17
  .must_be_nil
26
18
  end
27
19
 
28
20
  it 'returns nil in a downcase todo' do
29
- WhatNow.search_line('todo this is not a todo', 1, '.')
21
+ subject.match('todo this is not a todo', '.', 1)
30
22
  .must_be_nil
31
23
  end
32
24
  end
33
25
 
34
26
  describe 'with case insensitivity' do
35
- before do
36
- WhatNow.ignorecase
27
+ subject do
28
+ TodoCreator.new ignorecase: true
37
29
  end
38
30
 
39
31
  it 'extracts the text from the TODO in uppercase' do
40
- WhatNow.search_line('TODO this is a todo', 1, '.')
32
+ subject.match('TODO this is a todo', '.', 1)
41
33
  .text.must_match 'this is a todo'
42
34
  end
43
35
 
44
36
  it 'extracts the text from the TODO in downcase' do
45
- WhatNow.search_line('todo this is a todo', 1, '.')
37
+ subject.match('todo this is a todo', '.', 1)
46
38
  .text.must_match 'this is a todo'
47
39
  end
48
40
  end
41
+
42
+ describe 'object type returned' do
43
+ it 'default should be pretty' do
44
+ TodoCreator.new.match('TODO this is a todo', '.', 1)
45
+ .must_be_instance_of PrettyTodo
46
+ end
47
+
48
+ it 'non pretty can be specified' do
49
+ TodoCreator.new(pretty: false).match('TODO this is a todo', '.', 1)
50
+ .must_be_instance_of Todo
51
+ end
52
+ end
49
53
  end
54
+ end
50
55
 
51
- describe '#search_file' do
56
+ describe TodoFinder do
57
+ describe 'search single file' do
52
58
  subject do
53
59
  path = File.dirname(__FILE__) + '/example_file.txt'
54
- WhatNow.search_file(path)
60
+ TodoFinder.new(path, TodoCreator.new).find
55
61
  end
56
62
 
57
63
  it 'found 2 todos' do
@@ -66,14 +72,18 @@ describe WhatNow do
66
72
  end
67
73
  end
68
74
 
69
- describe '#search_directory' do
75
+ describe 'search with a pattern' do
70
76
  it 'considers only specified pattern' do
71
- results = WhatNow.search_directory(File.dirname(__FILE__)+'/**/*.txt')
77
+ results = TodoFinder.new(
78
+ File.dirname(__FILE__)+'/**/*.txt',
79
+ TodoCreator.new).find
72
80
  results.length.must_equal 2
73
81
  end
74
82
 
75
83
  it 'returns empty array if nothing was found' do
76
- results = WhatNow.search_directory(File.dirname(__FILE__)+'/**/*.mooo')
84
+ results = TodoFinder.new(
85
+ File.dirname(__FILE__)+'/**/*.mooo',
86
+ TodoCreator.new).find
77
87
  results.must_equal []
78
88
  end
79
89
  end
data/what_now.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'what_now'
3
- s.version = '0.0.4'
3
+ s.version = '0.0.5'
4
4
  s.date = '2014-02-27'
5
5
  s.summary = 'Find todo comments in your code'
6
6
  s.description = 'Executable for finding todo comments on directories'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: what_now
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edgar Cabrera
@@ -90,9 +90,11 @@ files:
90
90
  - LICENSE
91
91
  - README.md
92
92
  - bin/wnow
93
+ - lib/todo.rb
93
94
  - lib/what_now.rb
94
95
  - spec/example_file.txt
95
96
  - spec/spec_helper.rb
97
+ - spec/todo_spec.rb
96
98
  - spec/what_now_spec.rb
97
99
  - what_now.gemspec
98
100
  homepage: https://github.com/aleandros/what_now
@@ -120,6 +122,7 @@ signing_key:
120
122
  specification_version: 4
121
123
  summary: Find todo comments in your code
122
124
  test_files:
123
- - spec/spec_helper.rb
124
125
  - spec/what_now_spec.rb
125
126
  - spec/example_file.txt
127
+ - spec/spec_helper.rb
128
+ - spec/todo_spec.rb