Class: Yuzu::Filters::PostDateFilter

Inherits:
Filter show all
Defined in:
lib/yuzu/filters/post_date.rb

Instance Attribute Summary

Attributes inherited from Filter

#directive, #name

Instance Method Summary collapse

Methods inherited from Filter

#filter_type, filters, #get_match, #match, #process, #regex, registry, #replacement, #value

Constructor Details

#initializePostDateFilter

Returns a new instance of PostDateFilter.



5
6
7
8
# File 'lib/yuzu/filters/post_date.rb', line 5

def initialize
  @directive = "DATE"
  @name = :post_date
end

Instance Method Details

#default(website_file) ⇒ Object



10
11
12
# File 'lib/yuzu/filters/post_date.rb', line 10

def default(website_file)
  website_file.modified_at
end

#extract_date_from_filename(website_file) ⇒ Object



31
32
33
34
35
# File 'lib/yuzu/filters/post_date.rb', line 31

def extract_date_from_filename(website_file)
  post_filename = website_file.filename
  m = post_filename.match(/[0-9]{4}\-[0-9]{2}\-[0-9]{2}/)
  m.nil? ? nil : Time.parse(m[0])
end

#extract_date_from_folder_structure(website_file) ⇒ Object



37
38
39
40
# File 'lib/yuzu/filters/post_date.rb', line 37

def extract_date_from_folder_structure(website_file)
  m = website_file.path.relative.match(/[0-9]{4}\/[0-9]{2}\/[0-9]{2}/)
  m.nil? ? nil : Time.parse(m[0].gsub("/", "-"))
end

#get_value(website_file) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yuzu/filters/post_date.rb', line 14

def get_value(website_file)
  date = match(website_file.raw_contents)

  if not date.nil?
    begin
        date = Time.parse(date)
    rescue
      date = nil
    end
  end

  date ||= extract_date_from_filename(website_file)
  date ||= extract_date_from_folder_structure(website_file)
  date ||= default(website_file)
  date
end