Class: Woli::Repositories::Files

Inherits:
Object
  • Object
show all
Defined in:
lib/woli/repositories/files.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#configObject (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_datesObject



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 = (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 = (date)
  return nil unless File.exists? file_name

  text = File.read((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((entry.date), entry.text)
end