Class: Object

Inherits:
BasicObject
Defined in:
lib/ytrbium/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#bare_yamlObject

Strip ‘—n` header and `n…n` trailer from yaml, handle different versions of libyaml, see tickets.puppetlabs.com/browse/PUP-9313 and related links

  • libyaml 0.1.7 emits a ‘n…n` end-of-stream trailer, but 0.2.1 does not

  • treat single-line scalars separately to trim the trailing newline only when the emitted yaml is a single line.



23
24
25
26
27
28
29
30
31
32
# File 'lib/ytrbium/core_ext.rb', line 23

def bare_yaml
  return "" unless present?
  yaml = to_yaml

  # Detect single-line libyaml 0.2.1 scalar and remove trailing newline
  return yaml.sub(/\A--- ([^\n]+)\n\Z/m, '\1') if yaml.single_line_ending_in_newline?

  yaml.sub(/\A---[ \n]/, "") # Handle header for multi-line yaml snippets
    .sub(/(\n\.\.\.\n)?\Z/m, "") # Handle libyaml 0.1.7 end of stream marker
end

#blank?Boolean

Returns:

  • (Boolean)


2
3
4
# File 'lib/ytrbium/core_ext.rb', line 2

def blank?
  respond_to?(:empty?) ? !!empty? : !self
end

#indented_yaml(preindent) ⇒ Object

Indents all but the first line in the emitted yaml by the specified number of spaces.



12
13
14
15
# File 'lib/ytrbium/core_ext.rb', line 12

def indented_yaml(preindent)
  return "" unless present?
  bare_yaml.indent_by(preindent)
end

#present?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/ytrbium/core_ext.rb', line 6

def present?
  !blank?
end