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

#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


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

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

#format=(new_format) ⇒ Object


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

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

#formatted_last_modified_dateObject


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

def formatted_last_modified_date
  last_modified_at_time.strftime(format)
end

#last_modified_at_timeObject


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

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


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

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


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

def to_liquid
  @to_liquid ||= last_modified_at_time
end

#to_sObject


42
43
44
# File 'lib/jekyll-last-modified-at/determinator.rb', line 42

def to_s
  @to_s ||= formatted_last_modified_date
end