Module: RubyHome::Persistable::ClassMethods

Defined in:
lib/ruby_home/persistable.rb

Instance Method Summary collapse

Instance Method Details

#create(**options) ⇒ Object



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

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

#persistedObject



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

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

#readObject



25
26
27
28
29
30
31
# File 'lib/ruby_home/persistable.rb', line 25

def read
  return false unless File.exist?(source)

  YAML.load_file(source)
rescue Errno::EBADF
  return false
end

#write(collection) ⇒ Object



21
22
23
# File 'lib/ruby_home/persistable.rb', line 21

def write(collection)
  File.open(source, 'w') {|f| f.write(collection.to_yaml) }
end