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
95 96 97 |
# File 'lib/serverkit/resources/line.rb', line 95 def initialize(raw) @raw = raw end |
Instance Method Details
#append(line) ⇒ Serverkit::Resources::Line::Content
101 102 103 |
# File 'lib/serverkit/resources/line.rb', line 101 def append(line) self.class.new([*lines, line, ""].join("\n")) end |
#delete(line) ⇒ Serverkit::Resources::Line::Content
107 108 109 |
# File 'lib/serverkit/resources/line.rb', line 107 def delete(line) self.class.new(@raw.gsub(/^#{line}$/, "")) end |
#insert_after(regexp, line) ⇒ Serverkit::Resources::Line::Content
Insert the line after the last matched line or EOF
115 116 117 118 119 120 121 |
# File 'lib/serverkit/resources/line.rb', line 115 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
127 128 129 130 131 132 133 |
# File 'lib/serverkit/resources/line.rb', line 127 def insert_before(regexp, line) if index = lines.rindex { |line| line =~ regexp } insert(index, line) else prepend(line) end end |
#match(pattern) ⇒ false, true
137 138 139 |
# File 'lib/serverkit/resources/line.rb', line 137 def match(pattern) lines.lazy.grep(pattern).any? end |
#to_s ⇒ Object
Note:
Override
142 143 144 |
# File 'lib/serverkit/resources/line.rb', line 142 def to_s @raw.dup end |