Class: Jekyll::LastModifiedAt::Determinator

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-last-modified-at/determinator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_source, page_path, format = nil) ⇒ Determinator

Returns a new instance of Determinator.



9
10
11
12
13
# File 'lib/jekyll-last-modified-at/determinator.rb', line 9

def initialize(site_source, page_path, format = nil)
  @site_source = site_source
  @page_path   = page_path
  @format      = format || "%d-%b-%y"
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



7
8
9
# File 'lib/jekyll-last-modified-at/determinator.rb', line 7

def format
  @format
end

#page_pathObject (readonly)

Returns the value of attribute page_path.



6
7
8
# File 'lib/jekyll-last-modified-at/determinator.rb', line 6

def page_path
  @page_path
end

#site_sourceObject (readonly)

Returns the value of attribute site_source.



6
7
8
# File 'lib/jekyll-last-modified-at/determinator.rb', line 6

def site_source
  @site_source
end

Instance Method Details

#formatted_last_modified_dateObject



22
23
24
25
26
27
28
# File 'lib/jekyll-last-modified-at/determinator.rb', line 22

def formatted_last_modified_date
  return PATH_CACHE[page_path] unless PATH_CACHE[page_path].nil?

  last_modified = last_modified_at_time.strftime(@format)
  PATH_CACHE[page_path] = last_modified
  last_modified
end

#gitObject



15
16
17
18
19
20
# File 'lib/jekyll-last-modified-at/determinator.rb', line 15

def git
  return REPO_CACHE[site_source] unless REPO_CACHE[site_source].nil?

  REPO_CACHE[site_source] = Git.new(site_source)
  REPO_CACHE[site_source]
end

#last_modified_at_timeObject

Raises:

  • (Errno::ENOENT)


30
31
32
33
34
# File 'lib/jekyll-last-modified-at/determinator.rb', line 30

def last_modified_at_time
  raise Errno::ENOENT, "#{absolute_path_to_article} does not exist!" unless File.exist?(absolute_path_to_article)

  Time.at(last_modified_at_unix.to_i)
end

#last_modified_at_unixObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jekyll-last-modified-at/determinator.rb', line 36

def last_modified_at_unix
  if git.git_repo?
    last_commit_date = Executor.sh(
      "git",
      "--git-dir",
      git.top_level_directory,
      "log",
      "-n",
      "1",
      '--format="%ct"',
      "--",
      relative_path_from_git_dir,
    )[/\d+/]
    # last_commit_date can be nil iff the file was not committed.
    last_commit_date.nil? || last_commit_date.empty? ? mtime(absolute_path_to_article) : last_commit_date
  else
    mtime(absolute_path_to_article)
  end
end

#to_liquidObject



60
61
62
# File 'lib/jekyll-last-modified-at/determinator.rb', line 60

def to_liquid
  @to_liquid ||= last_modified_at_time
end

#to_sObject



56
57
58
# File 'lib/jekyll-last-modified-at/determinator.rb', line 56

def to_s
  @to_s ||= formatted_last_modified_date
end