class Compath::YamlFormatter

Constants

QUOTED_KEY_REGEXP

Attributes

yaml[RW]

Public Class Methods

new(yaml) click to toggle source
# File lib/compath/yaml_formatter.rb, line 5
def initialize(yaml)
  @yaml = yaml
end

Public Instance Methods

format() click to toggle source
# File lib/compath/yaml_formatter.rb, line 9
def format
  formatters.each do |formatter|
    self.yaml = formatter.call(yaml)
  end
  yaml
end
formatters() click to toggle source
# File lib/compath/yaml_formatter.rb, line 16
def formatters
  @formatters ||= [
    remove_trailing_space_formatter,
    remove_quotes_from_keys_formatter,
  ]
end
remove_quotes_from_keys_formatter() click to toggle source
# File lib/compath/yaml_formatter.rb, line 30
def remove_quotes_from_keys_formatter
  lambda do |yaml|
    lines = yaml.lines.map do |line|
      if m = QUOTED_KEY_REGEXP.match(line)
        "#{m[:key]}: #{m[:value]}".rstrip
      else
        line.chomp
      end
    end

    lines.map {|x| "#{x}\n" }.join
  end
end
remove_trailing_space_formatter() click to toggle source
# File lib/compath/yaml_formatter.rb, line 23
def remove_trailing_space_formatter
  lambda do |yaml|
    yaml.lines.map(&:rstrip).map {|x| "#{x}\n" }.join
  end
end