Module: RubyHome::Persistable::ClassMethods

Defined in:
lib/ruby_home/persistable.rb

Instance Method Summary collapse

Instance Method Details

#create(**options) ⇒ Object



18
19
20
# File 'lib/ruby_home/persistable.rb', line 18

def create(**options)
  new(**options).tap(&:save)
end

#persistObject



29
30
31
# File 'lib/ruby_home/persistable.rb', line 29

def persist
  File.write(source, cache.to_yaml)
end

#persistedObject



12
13
14
15
16
# File 'lib/ruby_home/persistable.rb', line 12

def persisted
  if (yaml = read)
    new(**yaml)
  end
end

#readObject



39
40
41
# File 'lib/ruby_home/persistable.rb', line 39

def read
  self.cache ||= read_persisted
end

#read_persistedObject



33
34
35
36
37
# File 'lib/ruby_home/persistable.rb', line 33

def read_persisted
  YAML.load_file(source)
rescue Errno::ENOENT
  false
end

#resetObject



43
44
45
46
# File 'lib/ruby_home/persistable.rb', line 43

def reset
  self.cache = nil
  persist
end

#write(collection) ⇒ Object



22
23
24
25
26
27
# File 'lib/ruby_home/persistable.rb', line 22

def write(collection)
  self.cache = collection
  persist

  true
end