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

#file_exists?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ruby_home/persistable.rb', line 37

def file_exists?
  File.exists?(source)
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
# File 'lib/ruby_home/persistable.rb', line 25

def read
  return false unless file_exists?

  YAML.load_file(source)
end

#truncateObject



31
32
33
34
35
# File 'lib/ruby_home/persistable.rb', line 31

def truncate
  return false unless file_exists?

  File.truncate(source, 0)
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