module SingletonStorer
Mixin module, adds the ability to persist a Singleton class in a local file and also de ability to load the Singleton class from that file. In order to work properply the class that include this modules must also include the ruby module Singleton
Public Class Methods
included(base)
click to toggle source
# File lib/singleton_storer.rb, line 19 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
_dump()
click to toggle source
# File lib/singleton_storer.rb, line 32 def _dump File.write(persistence_file_name, Marshal.dump(attrs_to_hash)) end
_load()
click to toggle source
# File lib/singleton_storer.rb, line 23 def _load return unless File.exist?(persistence_file_name) attributes = Marshal.load(File.read(persistence_file_name)) attributes.each do |attr, value| instance_variable_set("@#{attr}".to_sym, value) end end
attrs_to_hash()
click to toggle source
# File lib/singleton_storer.rb, line 40 def attrs_to_hash attrs = {} self.class::PERSISTED_ATTRIBUTES.each do |attr| attrs[attr] = send(attr) end attrs end
persistence_file_name()
click to toggle source
# File lib/singleton_storer.rb, line 36 def persistence_file_name "#{self.class.name.downcase}.mp" end