Class: HTM::Models::FileSource
- Inherits:
-
Object
- Object
- HTM::Models::FileSource
- Defined in:
- lib/htm/models/file_source.rb
Overview
FileSource model - tracks loaded source files
Represents a file that has been loaded into HTM with its metadata. Each file can have multiple associated nodes (chunks).
Constant Summary collapse
- DELTA_TIME =
Tolerance for mtime comparison to avoid false positives from precision differences between filesystem and database timestamps
5
Instance Method Summary collapse
-
#author ⇒ String?
Get author from frontmatter.
-
#chunks ⇒ Array<Node>
Get ordered chunks from this file.
-
#chunks_dataset ⇒ Sequel::Dataset
Alias for nodes_dataset - used for consistency with “chunks” terminology.
-
#frontmatter_tags ⇒ Array<String>
Extract tags from frontmatter.
-
#needs_sync?(current_mtime = nil) ⇒ Boolean
Check if file needs re-sync based on mtime.
-
#soft_delete_chunks! ⇒ Integer
Soft delete all chunks from this file.
-
#title ⇒ String?
Get title from frontmatter.
-
#validate ⇒ Object
Validations.
Instance Method Details
#author ⇒ String?
Get author from frontmatter
97 98 99 100 |
# File 'lib/htm/models/file_source.rb', line 97 def return nil unless frontmatter_hash? frontmatter['author'] || frontmatter[:author] end |
#chunks ⇒ Array<Node>
Get ordered chunks from this file
61 62 63 |
# File 'lib/htm/models/file_source.rb', line 61 def chunks nodes_dataset.order(:chunk_position).all end |
#chunks_dataset ⇒ Sequel::Dataset
Alias for nodes_dataset - used for consistency with “chunks” terminology
69 70 71 |
# File 'lib/htm/models/file_source.rb', line 69 def chunks_dataset nodes_dataset end |
#frontmatter_tags ⇒ Array<String>
Extract tags from frontmatter
77 78 79 80 81 82 |
# File 'lib/htm/models/file_source.rb', line 77 def return [] unless frontmatter_hash? = frontmatter['tags'] || frontmatter[:tags] || [] Array().map(&:to_s) end |
#needs_sync?(current_mtime = nil) ⇒ Boolean
Check if file needs re-sync based on mtime
49 50 51 52 53 54 55 |
# File 'lib/htm/models/file_source.rb', line 49 def needs_sync?(current_mtime = nil) return true if mtime.nil? return true unless File.exist?(file_path) current_mtime ||= File.mtime(file_path) (current_mtime.to_i - mtime.to_i).abs > DELTA_TIME end |
#soft_delete_chunks! ⇒ Integer
Soft delete all chunks from this file
106 107 108 |
# File 'lib/htm/models/file_source.rb', line 106 def soft_delete_chunks! nodes_dataset.update(deleted_at: Time.now) end |
#title ⇒ String?
Get title from frontmatter
88 89 90 91 |
# File 'lib/htm/models/file_source.rb', line 88 def title return nil unless frontmatter_hash? frontmatter['title'] || frontmatter[:title] end |
#validate ⇒ Object
Validations
23 24 25 26 27 |
# File 'lib/htm/models/file_source.rb', line 23 def validate super validates_presence :file_path validates_unique :file_path end |