module Incline::CliHelpers::Yaml

Adds YAML text helper methods.

Does not parse YAML files, but does allow for updating raw text files to be YAML compliant.

Protected Instance Methods

repair_yaml(filename, env_ref_sect = 'default', ensure_default = true, ensure_env = true, realign = true) { |contents| ... } click to toggle source

Repairs a YAML file, creating it if necessary.

The optional parameters define what all will be done by this method before yielding to a supplied block. The contents of the YAML file are yielded to the block and the block should return the modified contents.

repair_yaml "config/database.yml" do |contents|
  contents.add_key [ "test", "database" ], "db/test.sqlite3"
end

If 'env_ref_set' is set to a non-empty value, then it defines the anchor providing default values for the environment sections. The default value is 'default'.

If 'ensure_default' is set, then a 'default' section with a 'default' anchor will be created at the beginning of the file unless a 'default' section already exists in the file. The default value is 'true'.

If 'ensure_env' is set, then the environment sections will be created if missing. This ensures that a 'development', 'test', and 'production' section all exist. The default value is 'true'.

If 'realign' is set, then the entire file will be processed. All values will be aligned at each level and so will all comments. The default is 'true'.

The default options on an empty file will generate a 'default' section before yielding and then fill in the environment sections after the block returns.

default: &default

development:
  <<: *default

test:
  <<: *default

production:
  <<: *default
# File lib/incline/cli/helpers/yaml.rb, line 701
def repair_yaml(filename, env_ref_sect = 'default', ensure_default = true, ensure_env = true, realign = true) # :doc:
  dirty = false
  stat = :updated

  # ensure the file exists.
  unless File.exist?(filename)
    File.write filename, "# File created by Incline v#{Incline::VERSION}.\n"
    stat = :created
    dirty = true
  end

  orig_contents = File.read(filename)

  contents = YamlContents.new(orig_contents)

  if ensure_default
    contents.set_key %w(default), '&default', false
  end

  yield contents if block_given?

  if ensure_env
    %w(development test production).each do |sect|
      contents.add_key [ sect ], nil
    end
  end

  # Potential bug, and I'm not even sure if it would be or not.
  # But since we use relatively simple regular expressions to perform the set action,
  # the << "key" can only exist once and would be overridden.
  # That means that a section would not be able to include multiple anchors.
  # Like I said before, I'm not sure if that would actually be a bug or not.
  #
  # Luckily, the YAML files aren't meant to be overly complex so this shouldn't show
  # up regularly if at all.
  unless env_ref_sect.to_s.strip == ''
    %w(development test production).each do |sect|
      contents.set_key [ sect, '<<' ], '*' + env_ref_sect
    end
  end

  contents.realign! if realign

  unless dirty
    dirty = (orig_contents != contents.to_s)
  end

  if dirty
    File.write filename, contents.to_s
    say_status stat, filename, :green
  else
    say_status :unchanged, filename, :blue
  end
end