class AutoSerializer

Public Class Methods

auto(klass_or_method, *arguments) click to toggle source

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

# 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

Private Class Methods

create_name(klass_or_method, arguments) click to toggle source

Create file name based on class/method name and its (constructor) arguments.

# File lib/auto_serializer.rb, line 24
def self.create_name(klass_or_method, arguments)
  '%s-%s.marshal' % [klass_or_method.to_s, arguments.map { |argument| argument.to_s.gsub('/', '_')[0..100] }.join(';')]
end