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, opts = {}) ⇒ Determinator

Returns a new instance of Determinator.



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

def initialize(site_source, page_path, opts = {})
  @site_source = site_source
  @page_path   = page_path
  @opts        = opts
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

#page_pathObject (readonly)

Returns the value of attribute page_path.



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

def page_path
  @page_path
end

#site_sourceObject (readonly)

Returns the value of attribute site_source.



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

def site_source
  @site_source
end

Instance Method Details

#formatObject



53
54
55
# File 'lib/jekyll-last-modified-at/determinator.rb', line 53

def format
  opts['format'] ||= "%d-%b-%y"
end

#format=(new_format) ⇒ Object



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

def format=(new_format)
  opts['format'] = new_format
end

#formatted_last_modified_dateObject



12
13
14
15
16
17
# File 'lib/jekyll-last-modified-at/determinator.rb', line 12

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

#last_modified_at_timeObject



19
20
21
22
23
24
25
# File 'lib/jekyll-last-modified-at/determinator.rb', line 19

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

  Time.at(last_modified_at_unix.to_i)
end

#last_modified_at_unixObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jekyll-last-modified-at/determinator.rb', line 27

def last_modified_at_unix
  if is_git_repo?(site_source)
    last_commit_date = Executor.sh(
      'git',
      '--git-dir',
      top_level_git_directory,
      'log',
      '--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



49
50
51
# File 'lib/jekyll-last-modified-at/determinator.rb', line 49

def to_liquid
  @to_liquid ||= last_modified_at_time
end

#to_sObject



45
46
47
# File 'lib/jekyll-last-modified-at/determinator.rb', line 45

def to_s
  @to_s ||= formatted_last_modified_date
end