Class: Serverkit::Resources::Line::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/serverkit/resources/line.rb

Overview

Wrapper class to easily manage lines in remote file content.

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Content

Returns a new instance of Content.

Parameters:

  • raw (String)


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

Parameters:

  • line (String)

Returns:



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

Parameters:

  • line (String)

Returns:



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

Parameters:

  • regexp (Regexp)
  • line (String)

Returns:



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

Parameters:

  • regexp (Regexp)
  • line (String)

Returns:



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.

Parameters:

  • pattern (Regexp, String)

Returns:

  • (false, true)

    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_sObject

Note:

Override



160
161
162
# File 'lib/serverkit/resources/line.rb', line 160

def to_s
  @raw.dup
end