Method: VR.load_yaml

Defined in:
lib/SavableClass.rb

.load_yaml(klass, file_name, *args) ⇒ Object

Loads or creates a yaml file with a given object type (class). If the file exists already, the object will be returned. If not, a new instance will be returned, and the file will instantly be saved.

Parameters:

  • klass (Class)

    The class of the object to be loaded or created

  • file_name (String)

    File name to save. Should be named .yaml

  • args (Splat)

    Optional arguments to pass to contructor of class. Not used often.

Returns:

  • (Object)

    Object of type klass that was loaded or created.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/SavableClass.rb', line 10

def self.load_yaml(klass, file_name, *args)
  me = nil
  if File.file?(file_name) 
    me = YAML.load(File.open(file_name).read)
  else 
    me = klass.new(*args)
  end
  file_name = File.expand_path(file_name)
  me.instance_variable_set(:@vr_yaml_file, file_name)
  me.defaults() if me.respond_to?(:defaults)
  VR::save_yaml(me)
  return me
end