Module: RubyHome::Persistable::ClassMethods
- Defined in:
- lib/ruby_home/persistable.rb
Instance Method Summary collapse
- #create(**options) ⇒ Object
- #file_exists? ⇒ Boolean
- #persisted ⇒ Object
- #read ⇒ Object
- #truncate ⇒ Object
- #write(collection) ⇒ Object
Instance Method Details
#create(**options) ⇒ Object
15 16 17 |
# File 'lib/ruby_home/persistable.rb', line 15 def create(**) new(**).tap(&:save) end |
#file_exists? ⇒ Boolean
35 36 37 |
# File 'lib/ruby_home/persistable.rb', line 35 def file_exists? File.exists?(source) end |
#persisted ⇒ Object
9 10 11 12 13 |
# File 'lib/ruby_home/persistable.rb', line 9 def persisted if yaml = read new(yaml) end end |
#read ⇒ Object
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 |
#truncate ⇒ Object
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 |