Class: Serverkit::Resources::Line::Content
- Inherits:
-
Object
- Object
- Serverkit::Resources::Line::Content
- Defined in:
- lib/serverkit/resources/line.rb
Overview
Wrapper class to easily manage lines in remote file content.
Instance Method Summary collapse
- #append(line) ⇒ Serverkit::Resources::Line::Content
- #delete(line) ⇒ Serverkit::Resources::Line::Content
-
#initialize(raw) ⇒ Content
constructor
A new instance of Content.
-
#insert_after(regexp, line) ⇒ Serverkit::Resources::Line::Content
Insert the line after the last matched line or EOF.
-
#insert_before(regexp, line) ⇒ Serverkit::Resources::Line::Content
Insert the line before the last matched line or BOF.
-
#match(pattern) ⇒ false, true
True if any line matches given pattern.
- #to_s ⇒ Object
Constructor Details
#initialize(raw) ⇒ Content
Returns a new instance of Content.
111 112 113 |
# File 'lib/serverkit/resources/line.rb', line 111 def initialize(raw) @raw = raw end |
Instance Method Details
#append(line) ⇒ Serverkit::Resources::Line::Content
117 118 119 |
# File 'lib/serverkit/resources/line.rb', line 117 def append(line) self.class.new([*lines, line, ""].join("\n")) end |
#delete(line) ⇒ Serverkit::Resources::Line::Content
123 124 125 |
# File 'lib/serverkit/resources/line.rb', line 123 def delete(line) self.class.new(@raw.gsub(/^#{Regexp.escape(line)}[\n$]/, "")) end |
#insert_after(regexp, line) ⇒ Serverkit::Resources::Line::Content
Insert the line after the last matched line or EOF
131 132 133 134 135 136 137 |
# File 'lib/serverkit/resources/line.rb', line 131 def insert_after(regexp, line) if index = lines.rindex { |line| line =~ regexp } insert(index + 1, line) else append(line) end end |
#insert_before(regexp, line) ⇒ Serverkit::Resources::Line::Content
Insert the line before the last matched line or BOF
143 144 145 146 147 148 149 |
# File 'lib/serverkit/resources/line.rb', line 143 def insert_before(regexp, line) if index = lines.rindex { |line| line =~ regexp } insert(index, line) else prepend(line) end end |
#match(pattern) ⇒ false, true
Returns True if any line matches given pattern.
153 154 155 |
# File 'lib/serverkit/resources/line.rb', line 153 def match(pattern) lines.lazy.grep(pattern).any? end |
#to_s ⇒ Object
Note:
Override
158 159 160 |
# File 'lib/serverkit/resources/line.rb', line 158 def to_s @raw.dup end |