Class: Object

Inherits:
BasicObject
Defined in:
lib/pleasant_path/json/object.rb,
lib/pleasant_path/yaml/object.rb

Instance Method Summary collapse

Instance Method Details

#write_to_json(file, options = {}) ⇒ self

Writes the Object serialized as JSON to the specified file, overwriting the file if it exists. Creates the file if it does not exist, including any necessary parent directories. Returns the Object, unmodified.

For information about options see +JSON.generate+. By default, this method uses the default options for +JSON.dump+.

Examples:

{ "key" => "value" }.write_to_json("file.json")  # == { "key" => "value" }
File.read("file.json")                           # == '{"key":"value"}'

Parameters:

  • (defaults to: {})

Returns:



21
22
23
24
# File 'lib/pleasant_path/json/object.rb', line 21

def write_to_json(file, options = {})
  file.to_pathname.write_text(JSON.dump(self, nil, nil, options))
  self
end

#write_to_yaml(file, options = {}) ⇒ self

Writes the Object serialized as YAML to the specified file, overwriting the file if it exists. Creates the file if it does not exist, including any necessary parent directories. Returns the Object, unmodified.

For information about options see +YAML.dump+.

Examples:

{ "key" => "value" }.write_to_yaml("file.yaml")  # == { "key" => "value" }
File.read("file.yaml")                           # == "---\nkey: value\n"

Parameters:

  • (defaults to: {})

Returns:



21
22
23
24
25
26
# File 'lib/pleasant_path/yaml/object.rb', line 21

def write_to_yaml(file, options = {})
  file.to_pathname.make_dirname.open("w") do |f|
    YAML.dump(self, f, options)
  end
  self
end