Class: Kuromd::Journal::File

Inherits:
BaseFile show all
Includes:
Configurable, Fileable
Defined in:
lib/kuromd/journal/file.rb

Overview

Represents a file for a Journal process method will organize the note into it’s base folder

Constant Summary collapse

NOTE_TYPE =
'Journal file'

Instance Attribute Summary collapse

Attributes inherited from BaseFile

#note_type

Instance Method Summary collapse

Methods included from Fileable

#create_journal_folder, #find_date_from_filename, #move

Methods included from Configurable

#configure, #get_config, #set_config

Methods inherited from BaseFile

assign_file_objs, cleanup_types

Constructor Details

#initialize(params = {}) ⇒ File

Returns a new instance of File.



20
21
22
23
24
25
26
27
28
29
# File 'lib/kuromd/journal/file.rb', line 20

def initialize(params = {})
  super

  @config = Kuromd::Configurable.get_config
  @base_folder = @config.params['journal']['base_folder']

  @file_path = params[:full_path]
  @file_date = find_date_from_filename(@file_path)
  Kuromd.logger.info "Initialized Journal File #{@file_path}, #{@file_date}"
end

Instance Attribute Details

#base_folderObject

Returns the value of attribute base_folder.



18
19
20
# File 'lib/kuromd/journal/file.rb', line 18

def base_folder
  @base_folder
end

#file_dateObject

Returns the value of attribute file_date.



18
19
20
# File 'lib/kuromd/journal/file.rb', line 18

def file_date
  @file_date
end

#file_pathObject

Returns the value of attribute file_path.



18
19
20
# File 'lib/kuromd/journal/file.rb', line 18

def file_path
  @file_path
end

Instance Method Details

#cleanup_filenameObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kuromd/journal/file.rb', line 31

def cleanup_filename
  filename = ::File.basename(@file_path)
  date_position = filename.index(@file_date)

  if date_position.zero?
    filename.delete_prefix!("#{@file_date} - ")
  else
    extname = ::File.extname(filename)
    filename.delete_suffix!(" - #{@file_date}#{extname}")
    filename += extname
  end

  filename
end

#processObject



55
56
57
58
59
60
61
62
# File 'lib/kuromd/journal/file.rb', line 55

def process
  return unless valid?

  journal_folder = create_journal_folder(base_path: @base_folder, date: @file_date)
  move({ dest_folder: journal_folder,
         source_path: @file_path,
         rename_to: cleanup_filename })
end

#valid?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
# File 'lib/kuromd/journal/file.rb', line 46

def valid?
  # assume valid unless date doesn't exist
  is_valid = true
  is_valid = false if @file_date.nil?
  Kuromd.logger.info "Journal File object valid? #{is_valid}"

  is_valid
end