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
17 18 19 |
# File 'lib/ruby_home/persistable.rb', line 17 def create(**) new(**).tap(&:save) end |
#file_exists? ⇒ Boolean
37 38 39 |
# File 'lib/ruby_home/persistable.rb', line 37 def file_exists? File.exists?(source) end |
#persisted ⇒ Object
11 12 13 14 15 |
# File 'lib/ruby_home/persistable.rb', line 11 def persisted if yaml = read new(yaml) end end |
#read ⇒ Object
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 |
#truncate ⇒ Object
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 |