class Serverkit::Resources::Line::Content

Wrapper class to easily manage lines in remote file content.

Public Class Methods

new(raw) click to toggle source

@param [String] raw

# File lib/serverkit/resources/line.rb, line 111
def initialize(raw)
  @raw = raw
end

Public Instance Methods

append(line) click to toggle source

@param [String] line @return [Serverkit::Resources::Line::Content]

# File lib/serverkit/resources/line.rb, line 117
def append(line)
  self.class.new([*lines, line, ""].join("\n"))
end
delete(line) click to toggle source

@param [String] line @return [Serverkit::Resources::Line::Content]

# 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) click to toggle source

Insert the line after the last matched line or EOF @param [Regexp] regexp @param [String] line @return [Serverkit::Resources::Line::Content]

# 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) click to toggle source

Insert the line before the last matched line or BOF @param [Regexp] regexp @param [String] line @return [Serverkit::Resources::Line::Content]

# 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) click to toggle source

@param [Regexp, String] pattern @return [false, true] True if any line matches given pattern

# File lib/serverkit/resources/line.rb, line 153
def match(pattern)
  lines.lazy.grep(pattern).any?
end
to_s() click to toggle source

@note Override

# File lib/serverkit/resources/line.rb, line 158
def to_s
  @raw.dup
end

Private Instance Methods

insert(index, line) click to toggle source

@param [Integer] index @param [String] line @return [Serverkit::Resources::Line::Content]

# File lib/serverkit/resources/line.rb, line 167
def insert(index, line)
  self.class.new([*lines.dup.insert(index, line), ""].join("\n"))
end
lines() click to toggle source

@return [Array<String>]

# File lib/serverkit/resources/line.rb, line 172
def lines
  @lines ||= @raw.each_line.map do |line|
    line.gsub(/\n$/, "")
  end
end
prepend(line) click to toggle source

@param [String] line @return [Serverkit::Resources::Line::Content]

# File lib/serverkit/resources/line.rb, line 180
def prepend(line)
  self.class.new("#{line}\n#{@raw}")
end