Class: Woli::Repositories::Files
- Inherits:
-
Object
- Object
- Woli::Repositories::Files
- Defined in:
- lib/woli/repositories/files.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #all_entries_dates ⇒ Object
- #delete_entry(entry) ⇒ Object
-
#initialize(config) ⇒ Files
constructor
A new instance of Files.
- #load_entry(date) ⇒ Object
- #save_entry(entry) ⇒ Object
Constructor Details
#initialize(config) ⇒ Files
Returns a new instance of Files.
6 7 8 |
# File 'lib/woli/repositories/files.rb', line 6 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/woli/repositories/files.rb', line 4 def config @config end |
Instance Method Details
#all_entries_dates ⇒ Object
10 11 12 13 14 15 |
# File 'lib/woli/repositories/files.rb', line 10 def all_entries_dates all_entries_files.map { |filename| match_data = filename.match /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2}).#{Regexp.escape(config['entry_extension'])}\Z/ Date.new(match_data[:year].to_i, match_data[:month].to_i, match_data[:day].to_i) }.sort end |
#delete_entry(entry) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/woli/repositories/files.rb', line 29 def delete_entry(entry) file_name = path_for_entry_date(entry.date) return unless File.exists? file_name File.delete(file_name) end |
#load_entry(date) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/woli/repositories/files.rb', line 17 def load_entry(date) file_name = path_for_entry_date(date) return nil unless File.exists? file_name text = File.read(path_for_entry_date(date)) DiaryEntry.new(date, text, self) end |
#save_entry(entry) ⇒ Object
25 26 27 |
# File 'lib/woli/repositories/files.rb', line 25 def save_entry(entry) File.write(path_for_entry_date(entry.date), entry.text) end |