grit-ext 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1 +1,14 @@
1
1
  # grit-ext
2
+
3
+ ## Resources
4
+
5
+ * [grit](https://github.com/mojombo/grit)
6
+ * [grit-ext on RubyGems](https://rubygems.org/gems/grit-ext)
7
+
8
+ ## How to Use
9
+
10
+ To use grit-ext, after installing, just require 'grit-ext':
11
+
12
+ require 'grit-ext'
13
+
14
+ All of the extensions, additionally to normal grit classes, will be available to you.
data/lib/grit/ext/diff.rb CHANGED
@@ -4,7 +4,7 @@ module Grit
4
4
  lines = diff.split("\n")
5
5
  lines.each_with_index
6
6
  .find_all { |line, index| line.start_with?('@@') }
7
- .map { |line, index| [DiffHunkHeader.new(line), index] }
7
+ .map { |line, index| [DiffHeader.new(line), index] }
8
8
  .map { |header, index|
9
9
  DiffHunk.new(header, lines[index, header.count])
10
10
  }
@@ -0,0 +1,26 @@
1
+ module Grit
2
+ class DiffHeader
3
+ attr_reader :removed_start, :removed_count,
4
+ :added_start, :added_count
5
+
6
+ alias_method :start, :added_start
7
+ alias_method :count, :added_count
8
+
9
+ def initialize(hunk_header)
10
+ groups = hunk_header.split('-').last.split('+')
11
+
12
+ removed = groups.first.strip
13
+ added = groups.last.sub(' @@', '').strip
14
+
15
+ @removed_start, @removed_count = get_numbers(removed)
16
+ @added_start, @added_count = get_numbers(added)
17
+ end
18
+
19
+ private
20
+
21
+ def get_numbers(s)
22
+ start, count = s.split(',')
23
+ [start.to_i, count.to_i]
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,7 @@
1
1
  module Grit
2
2
  class DiffHunk
3
+ attr_reader :lines
4
+
3
5
  def initialize(header, diff_hunk)
4
6
  @lines = diff_hunk.each_with_index.map do |line, index|
5
7
  content = line[1..line.length - 1]
@@ -8,34 +10,22 @@ module Grit
8
10
  when '-' then :removed
9
11
  when ' ' then :unchanged
10
12
  end
11
- {
12
- content: content,
13
- status: status,
14
- position: header.start + index
15
- }
16
- end
17
- end
13
+ position = header.start + index
18
14
 
19
- def lines
20
- @lines
15
+ DiffLine.new(content, status, position)
16
+ end
21
17
  end
22
18
 
23
19
  def added
24
- lines_by_status(:added)
20
+ @lines.find_all { |line| line.added? }
25
21
  end
26
22
 
27
23
  def removed
28
- lines_by_status(:removed)
24
+ @lines.find_all { |line| line.removed? }
29
25
  end
30
26
 
31
27
  def unchanged
32
- lines_by_status(:unchanged)
33
- end
34
-
35
- private
36
-
37
- def lines_by_status(status)
38
- @lines.find_all { |line| line[:status] == status }
28
+ @lines.find_all { |line| line.unchanged? }
39
29
  end
40
30
  end
41
31
  end
@@ -0,0 +1,15 @@
1
+ module Grit
2
+ class DiffLine < Struct.new(:content, :status, :position)
3
+ def added?
4
+ status == :added
5
+ end
6
+
7
+ def removed?
8
+ status == :removed
9
+ end
10
+
11
+ def unchanged?
12
+ status == :unchanged
13
+ end
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  module Grit
2
2
  module Ext
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
data/lib/grit-ext.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'grit'
2
2
  require 'grit/ext/diff'
3
3
  require 'grit/ext/diff_hunk'
4
- require 'grit/ext/diff_hunk_header'
4
+ require 'grit/ext/diff_header'
5
+ require 'grit/ext/diff_line'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grit-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -68,8 +68,9 @@ extra_rdoc_files: []
68
68
  files:
69
69
  - lib/grit/ext/commit.rb
70
70
  - lib/grit/ext/diff.rb
71
+ - lib/grit/ext/diff_header.rb
71
72
  - lib/grit/ext/diff_hunk.rb
72
- - lib/grit/ext/diff_hunk_header.rb
73
+ - lib/grit/ext/diff_line.rb
73
74
  - lib/grit/ext/version.rb
74
75
  - lib/grit-ext.rb
75
76
  - LICENSE
@@ -1,11 +0,0 @@
1
- module Grit
2
- class DiffHunkHeader
3
- attr_reader :start, :count
4
-
5
- def initialize(hunk_header)
6
- start, count = hunk_header.split('+').last.sub(' @@', '').split(',')
7
- @start = start.to_i
8
- @count = count.to_i
9
- end
10
- end
11
- end