Class: Configural::Autosave
- Inherits:
-
Object
- Object
- Configural::Autosave
- Defined in:
- lib/configural/autosave.rb
Instance Attribute Summary collapse
-
#enabled ⇒ Object
(also: #enabled?)
readonly
Returns the value of attribute enabled.
Class Method Summary collapse
- .instance ⇒ Object
-
.register(*args) ⇒ Object
Shorthand for Autosave.instance.register.
-
.unregister(*args) ⇒ Object
Shorthand for Autosave.instance.unregister.
Instance Method Summary collapse
- #disable ⇒ Object
- #enable ⇒ Object
-
#initialize ⇒ Autosave
constructor
A new instance of Autosave.
- #register(object, method = :save) ⇒ Object
- #save_all ⇒ Object
- #unregister(object) ⇒ Object
- #unregister_all ⇒ Object
Constructor Details
#initialize ⇒ Autosave
Returns a new instance of Autosave.
50 51 52 53 54 |
# File 'lib/configural/autosave.rb', line 50 def initialize @weakrefs = {} @enabled = true at_exit{ self.save_all } end |
Instance Attribute Details
#enabled ⇒ Object (readonly) Also known as: enabled?
Returns the value of attribute enabled.
75 76 77 |
# File 'lib/configural/autosave.rb', line 75 def enabled @enabled end |
Class Method Details
.instance ⇒ Object
35 36 37 |
# File 'lib/configural/autosave.rb', line 35 def self.instance @instance ||= new() end |
.register(*args) ⇒ Object
Shorthand for Autosave.instance.register
40 41 42 |
# File 'lib/configural/autosave.rb', line 40 def self.register(*args) instance.register(*args) end |
.unregister(*args) ⇒ Object
Shorthand for Autosave.instance.unregister
45 46 47 |
# File 'lib/configural/autosave.rb', line 45 def self.unregister(*args) instance.unregister(*args) end |
Instance Method Details
#disable ⇒ Object
83 84 85 86 |
# File 'lib/configural/autosave.rb', line 83 def disable @enabled = false self end |
#enable ⇒ Object
78 79 80 81 |
# File 'lib/configural/autosave.rb', line 78 def enable @enabled = true self end |
#register(object, method = :save) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/configural/autosave.rb', line 57 def register(object, method=:save) require 'weakref' weakref = WeakRef.new(object) @weakrefs[weakref] = method self end |
#save_all ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/configural/autosave.rb', line 89 def save_all return self unless @enabled @weakrefs.each do |weakref, method| if weakref.weakref_alive? and weakref.respond_to?(method) weakref.send(method) end end self end |
#unregister(object) ⇒ Object
64 65 66 67 |
# File 'lib/configural/autosave.rb', line 64 def unregister(object) @weakrefs.delete_if{ |k,v| k.__id__ == object.object_id } self end |
#unregister_all ⇒ Object
69 70 71 72 |
# File 'lib/configural/autosave.rb', line 69 def unregister_all @weakrefs = {} self end |