Class: AutoSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/auto_serializer.rb

Class Method Summary collapse

Class Method Details

.auto(klass_or_method, *arguments) ⇒ Object

Create file name based on class/method name and its constructor arguments. Try deserialize from this file, otherwise initialize object and serialize to file.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/auto_serializer.rb', line 3

def self.auto(klass_or_method, *arguments) #TODO methods
  name = create_name(klass_or_method, arguments)
  if File.exist? name
    File.open(name) do |file|
      return Marshal.load(file)
    end
  end

  if klass_or_method.instance_of? Class
    object = klass_or_method.new(*arguments)
  else
    object = method(klass_or_method).call(*arguments)
  end
  File.open(name, 'w') do |output|
    Marshal.dump(object, output)
  end
  return object
end