Class: Rufio::InfoNotice
- Inherits:
-
Object
- Object
- Rufio::InfoNotice
- Defined in:
- lib/rufio/info_notice.rb
Overview
Manages info notices from the info directory
Constant Summary collapse
- INFO_DIR =
File.join(File.dirname(__FILE__), '..', '..', 'info')
- NOTICE_TRACKING_DIR =
File.join(Dir.home, '.config', 'rufio', 'notices')
Instance Attribute Summary collapse
-
#info_dir ⇒ Object
Returns the value of attribute info_dir.
-
#tracking_dir ⇒ Object
Returns the value of attribute tracking_dir.
Instance Method Summary collapse
-
#extract_title(file_path) ⇒ String
Extract title from the first line of the file.
-
#initialize(info_dir: nil, tracking_dir: nil) ⇒ InfoNotice
constructor
A new instance of InfoNotice.
-
#mark_as_shown(file_path) ⇒ Object
Mark a notice as shown.
-
#read_content(file_path) ⇒ Array<String>
Read the content of a notice file.
-
#shown?(file_path) ⇒ Boolean
Check if a notice has been shown.
-
#unread_notices ⇒ Array<Hash>
Get all available notices that haven’t been shown.
Constructor Details
#initialize(info_dir: nil, tracking_dir: nil) ⇒ InfoNotice
Returns a new instance of InfoNotice.
13 14 15 16 17 |
# File 'lib/rufio/info_notice.rb', line 13 def initialize(info_dir: nil, tracking_dir: nil) @info_dir = info_dir || INFO_DIR @tracking_dir = tracking_dir || NOTICE_TRACKING_DIR ensure_tracking_directory end |
Instance Attribute Details
#info_dir ⇒ Object
Returns the value of attribute info_dir.
11 12 13 |
# File 'lib/rufio/info_notice.rb', line 11 def info_dir @info_dir end |
#tracking_dir ⇒ Object
Returns the value of attribute tracking_dir.
11 12 13 |
# File 'lib/rufio/info_notice.rb', line 11 def tracking_dir @tracking_dir end |
Instance Method Details
#extract_title(file_path) ⇒ String
Extract title from the first line of the file
54 55 56 57 58 59 60 |
# File 'lib/rufio/info_notice.rb', line 54 def extract_title(file_path) first_line = File.open(file_path, &:readline).strip # Remove markdown heading markers if present first_line.gsub(/^#+\s*/, '') rescue StandardError File.basename(file_path, '.txt') end |
#mark_as_shown(file_path) ⇒ Object
Mark a notice as shown
46 47 48 49 |
# File 'lib/rufio/info_notice.rb', line 46 def mark_as_shown(file_path) tracking_file = tracking_file_path(file_path) FileUtils.touch(tracking_file) end |
#read_content(file_path) ⇒ Array<String>
Read the content of a notice file
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rufio/info_notice.rb', line 65 def read_content(file_path) lines = File.readlines(file_path, chomp: true) # Skip the first line if it's a markdown heading (title) lines = lines.drop(1) if lines.first&.start_with?('#') # Add empty lines at the beginning and end for padding [''] + lines + ['', 'Press any key to continue...', ''] rescue StandardError => e [ '', "Error reading notice: #{e.}", '', 'Press any key to continue...', '' ] end |
#shown?(file_path) ⇒ Boolean
Check if a notice has been shown
39 40 41 42 |
# File 'lib/rufio/info_notice.rb', line 39 def shown?(file_path) tracking_file = tracking_file_path(file_path) File.exist?(tracking_file) end |
#unread_notices ⇒ Array<Hash>
Get all available notices that haven’t been shown
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rufio/info_notice.rb', line 21 def unread_notices return [] unless Dir.exist?(@info_dir) Dir.glob(File.join(@info_dir, '*.txt')).map do |file_path| next if shown?(file_path) { file: file_path, filename: File.basename(file_path), title: extract_title(file_path), content: read_content(file_path) } end.compact end |