class Serverkit::Resources::Line::Content
Wrapper class to easily manage lines in remote file content.
Public Class Methods
Source
# File lib/serverkit/resources/line.rb, line 113 def initialize(raw) @raw = raw end
@param [String] raw
Public Instance Methods
Source
# File lib/serverkit/resources/line.rb, line 119 def append(line) self.class.new([*lines, line, ""].join("\n")) end
@param [String] line @return [Serverkit::Resources::Line::Content]
Source
# File lib/serverkit/resources/line.rb, line 125 def delete(line) self.class.new(@raw.gsub(/^#{Regexp.escape(line)}[\n$]/, "")) end
@param [String] line @return [Serverkit::Resources::Line::Content]
Source
# 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 the line after the last matched line or EOF @param [Regexp] regexp @param [String] line @return [Serverkit::Resources::Line::Content]
Source
# 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
Insert the line before the last matched line or BOF @param [Regexp] regexp @param [String] line @return [Serverkit::Resources::Line::Content]
Source
# File lib/serverkit/resources/line.rb, line 155 def match(pattern) lines.lazy.grep(pattern).any? end
@param [Regexp, String] pattern @return [false, true] True if any line matches given pattern
Source
# File lib/serverkit/resources/line.rb, line 160 def to_s @raw.dup end
@note Override
Private Instance Methods
Source
# File lib/serverkit/resources/line.rb, line 169 def insert(index, line) self.class.new([*lines.dup.insert(index, line), ""].join("\n")) end
@param [Integer] index @param [String] line @return [Serverkit::Resources::Line::Content]
Source
# File lib/serverkit/resources/line.rb, line 174 def lines @lines ||= @raw.each_line.map do |line| line.gsub(/\n$/, "") end end
@return [Array<String>]
Source
# File lib/serverkit/resources/line.rb, line 182 def prepend(line) self.class.new("#{line}\n#{@raw}") end
@param [String] line @return [Serverkit::Resources::Line::Content]