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.
113 114 115 |
# File 'lib/serverkit/resources/line.rb', line 113 def initialize(raw) @raw = raw end |
Instance Method Details
#append(line) ⇒ Serverkit::Resources::Line::Content
119 120 121 |
# File 'lib/serverkit/resources/line.rb', line 119 def append(line) self.class.new([*lines, line, ""].join("\n")) end |
#delete(line) ⇒ Serverkit::Resources::Line::Content
125 126 127 |
# File 'lib/serverkit/resources/line.rb', line 125 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
133 134 135 136 137 138 139 |
# File 'lib/serverkit/resources/line.rb', line 133 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
145 146 147 148 149 150 151 |
# File 'lib/serverkit/resources/line.rb', line 145 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.
155 156 157 |
# File 'lib/serverkit/resources/line.rb', line 155 def match(pattern) lines.lazy.grep(pattern).any? end |
#to_s ⇒ Object
Note:
Override
160 161 162 |
# File 'lib/serverkit/resources/line.rb', line 160 def to_s @raw.dup end |