Class: HTM::Models::FileSource

Inherits:
Object
  • Object
show all
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

Instance Method Details

#authorString?

Get author from frontmatter

Returns:

  • (String, nil)

    Author from frontmatter



97
98
99
100
# File 'lib/htm/models/file_source.rb', line 97

def author
  return nil unless frontmatter_hash?
  frontmatter['author'] || frontmatter[:author]
end

#chunksArray<Node>

Get ordered chunks from this file

Returns:

  • (Array<Node>)

    Nodes ordered by chunk_position



61
62
63
# File 'lib/htm/models/file_source.rb', line 61

def chunks
  nodes_dataset.order(:chunk_position).all
end

#chunks_datasetSequel::Dataset

Alias for nodes_dataset - used for consistency with “chunks” terminology

Returns:

  • (Sequel::Dataset)

    Dataset of nodes from this file



69
70
71
# File 'lib/htm/models/file_source.rb', line 69

def chunks_dataset
  nodes_dataset
end

#frontmatter_tagsArray<String>

Extract tags from frontmatter

Returns:

  • (Array<String>)

    Tag names from frontmatter ‘tags’ field



77
78
79
80
81
82
# File 'lib/htm/models/file_source.rb', line 77

def frontmatter_tags
  return [] unless frontmatter_hash?

  tags = frontmatter['tags'] || frontmatter[:tags] || []
  Array(tags).map(&:to_s)
end

#needs_sync?(current_mtime = nil) ⇒ Boolean

Check if file needs re-sync based on mtime

Parameters:

  • current_mtime (Time, nil) (defaults to: nil)

    Current file modification time

Returns:

  • (Boolean)

    true if file needs re-sync



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

Returns:

  • (Integer)

    Number of chunks soft-deleted



106
107
108
# File 'lib/htm/models/file_source.rb', line 106

def soft_delete_chunks!
  nodes_dataset.update(deleted_at: Time.now)
end

#titleString?

Get title from frontmatter

Returns:

  • (String, nil)

    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

#validateObject

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