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)


111
112
113
# File 'lib/serverkit/resources/line.rb', line 111

def initialize(raw)
  @raw = raw
end

Instance Method Details

#append(line) ⇒ Serverkit::Resources::Line::Content

Parameters:

  • line (String)

Returns:



117
118
119
# File 'lib/serverkit/resources/line.rb', line 117

def append(line)
  self.class.new([*lines, line, ""].join("\n"))
end

#delete(line) ⇒ Serverkit::Resources::Line::Content

Parameters:

  • line (String)

Returns:



123
124
125
# 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) ⇒ Serverkit::Resources::Line::Content

Insert the line after the last matched line or EOF

Parameters:

  • regexp (Regexp)
  • line (String)

Returns:



131
132
133
134
135
136
137
# 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) ⇒ Serverkit::Resources::Line::Content

Insert the line before the last matched line or BOF

Parameters:

  • regexp (Regexp)
  • line (String)

Returns:



143
144
145
146
147
148
149
# 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) ⇒ false, true

Returns True if any line matches given pattern.

Parameters:

  • pattern (Regexp, String)

Returns:

  • (false, true)

    True if any line matches given pattern



153
154
155
# File 'lib/serverkit/resources/line.rb', line 153

def match(pattern)
  lines.lazy.grep(pattern).any?
end

#to_sObject

Note:

Override



158
159
160
# File 'lib/serverkit/resources/line.rb', line 158

def to_s
  @raw.dup
end