Skip to content

Commit 0355b92

Browse files
committed
move IndentationHelper to concerns directory
1 parent 7b62c47 commit 0355b92

File tree

4 files changed

+48
-46
lines changed

4 files changed

+48
-46
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Style/GuardClause: { Exclude: [lib/bashly/config_validator.rb] }
2222
# False positive report of invalid use of a method that starts with `set_`
2323
Naming/AccessorMethodName:
2424
Exclude:
25-
- 'lib/bashly/indenter.rb'
25+
- 'lib/bashly/concerns/indentation_helper.rb'
2626

2727
# FIXME: The `Command` class is too long
2828
Metrics/ClassLength: { Exclude: [lib/bashly/script/command.rb] }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module Bashly
2+
# A helper class, used by the `Array#indent` extension.
3+
# It will return the array of strings with all strings prefixed by `indentation`
4+
# unless the line is within a heredoc block.
5+
class IndentationHelper
6+
attr_reader :marker, :indentation
7+
8+
def initialize(indentation)
9+
@indentation = indentation
10+
@marker = nil
11+
end
12+
13+
def indent(line)
14+
if inside_heredoc?
15+
reset_marker if heredoc_closed?(line)
16+
line
17+
else
18+
set_heredoc_state(line)
19+
"#{indentation}#{line}"
20+
end
21+
end
22+
23+
private
24+
25+
def reset_marker
26+
@marker = nil
27+
end
28+
29+
def inside_heredoc?
30+
!!marker
31+
end
32+
33+
def set_heredoc_state(line)
34+
@marker = extract_heredoc_marker(line)
35+
end
36+
37+
def extract_heredoc_marker(line)
38+
line =~ /<<-?\s*(\w+)/ ? $1 : nil
39+
end
40+
41+
def heredoc_closed?(line)
42+
inside_heredoc? && /^#{marker}\n?$/.match?(line)
43+
end
44+
end
45+
end

lib/bashly/extensions/array.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
require 'bashly/indenter'
1+
require 'bashly/concerns/indentation_helper'
22

33
class Array
44
def indent(offset)
55
return self unless offset.positive?
66

77
indentation = ' ' * offset
8-
indenter = Indenter.new indentation
8+
indenter = Bashly::IndentationHelper.new indentation
99

1010
map { |line| indenter.indent line }
1111
end

lib/bashly/indenter.rb

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)