Class: YamlSmith::AddKey

Inherits:
FileCommand show all
Defined in:
lib/yaml_smith/add_key.rb

Overview

Adds a top-level key to a YAML file.

Instance Method Summary collapse

Methods inherited from FileCommand

call, #initialize

Constructor Details

This class inherits a constructor from YamlSmith::FileCommand

Instance Method Details

#callObject

Raises:



6
7
8
9
10
11
12
13
14
15
# File 'lib/yaml_smith/add_key.rb', line 6

def call
  document = load_file(@path)
  value = load(@value)

  ensure_mapping(document)
  raise Error, "key already exists: #{@key}" if document.key?(@key)

  document[@key] = value
  write(Psych::Pure.dump(document))
end