Class: Ytrbium::String

Inherits:
String show all
Defined in:
lib/ytrbium/string.rb

Instance Method Summary collapse

Methods inherited from String

#indent_by, #single_line_ending_in_newline?

Constructor Details

#initializeString

Returns a new instance of String.



2
3
4
5
6
# File 'lib/ytrbium/string.rb', line 2

def initialize
  super
  @indent = 0
  @last_newline = 0
end

Instance Method Details

#<<(str) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ytrbium/string.rb', line 8

def <<(str)
  @indent = -1 if str.include?("\n")
  super.tap do
    if @indent == -1
      @last_newline = str.rindex("\n")
      @indent = 0
    end
    if @indent == 0 && self[@last_newline..-1] =~ /\n([- ]+)\Z/m
      @indent = $1.length
    end
  end
end

#indent_expr(obj) ⇒ Object



21
22
23
# File 'lib/ytrbium/string.rb', line 21

def indent_expr(obj)
  obj.bare_yaml.indent_by(@indent)
end

#to_sObject



25
26
27
28
29
30
31
# File 'lib/ytrbium/string.rb', line 25

def to_s
  if start_with?("---")
    self
  else
    "---\n#{self}"
  end
end