Module: RubyHome::Persistable::ClassMethods

Defined in:
lib/ruby_home/persistable.rb

Instance Method Summary collapse

Instance Method Details

#create(**options) ⇒ Object



15
16
17
# File 'lib/ruby_home/persistable.rb', line 15

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

#file_exists?Boolean

Returns:

  • (Boolean)


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

def file_exists?
  File.exists?(source)
end

#persistedObject



9
10
11
12
13
# File 'lib/ruby_home/persistable.rb', line 9

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

#readObject



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

def read
  return false unless file_exists?

  YAML.load_file(source)
end

#truncateObject



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

def truncate
  return false unless file_exists?

  File.truncate(source, 0)
end

#write(collection) ⇒ Object



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

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