Module: Roject::LoadSaveable::ClassMethods

Defined in:
lib/loadsaveable.rb

Overview

Methods that are implemented at the class level

Author: Anshul Kharbanda Created: 7 - 10 - 2016

Instance Method Summary collapse

Instance Method Details

#load(filename) ⇒ Object

Loads an object from a file of the given filename, parsed in the appropriate format based on the extension

Parameter: filename - the name of the file

Return: object loaded from the file



65
66
67
# File 'lib/loadsaveable.rb', line 65

def load filename
  self.new Parsers.get(filename).parse IO.read filename
end

#open(filename, &block) ⇒ Object

Opens the object from a file, evaluates the given block in the context of the object, and saves the object. If no block is given, returns the object (alias for load)

Parameter: filename - the name of the file to open Parameter: block - the block to evaluate within the context of the object

Return: the object loaded from the file (if no block given)



49
50
51
52
53
54
55
56
57
# File 'lib/loadsaveable.rb', line 49

def open filename, &block
  obj = load(filename)
  if block.nil?
    return obj
  else
    obj.instance_exec(&block)
    obj.save(filename)
  end
end