Class: Coffeebrew::Jekyll::Archives::Page

Inherits:
Jekyll::Page
  • Object
show all
Defined in:
lib/coffeebrew_jekyll_archives/page.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

HIERARCHY =
{
  "root" => "year",
  "year" => "month",
  "month" => "day"
}.freeze
DEPTHS =
{
  "root" => 0,
  "year" => 1,
  "month" => 2,
  "day" => 3
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, site, config, parent, collection, depths, year:, month:, day:) ⇒ Page

rubocop:disable Lint/MissingSuper, Metrics/ParameterLists



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 22

def initialize(type, site, config, parent, collection, depths, year:, month:, day:) # rubocop:disable Lint/MissingSuper, Metrics/ParameterLists
  @type = type
  @site = site
  @config = config
  @base = site.source
  @date = Date.new(year.to_i, month.to_i, day.to_i)
  @ext = ".html"
  @parent = parent
  @collection = collection
  @depths = depths
  build_data
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



20
21
22
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 20

def collection
  @collection
end

#dateObject (readonly)

Returns the value of attribute date.



20
21
22
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 20

def date
  @date
end

#parentObject (readonly)

Returns the value of attribute parent.



20
21
22
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 20

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



20
21
22
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 20

def type
  @type
end

Instance Method Details

#ancestorsObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 56

def ancestors
  @ancestors ||= begin
    arr = []
    current_parent = parent
    while current_parent
      arr.unshift(current_parent)
      current_parent = current_parent.parent
    end
    arr
  end
end

#basenameObject



100
101
102
103
104
105
106
107
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 100

def basename
  @basename ||= case type.to_sym
                when :root
                  @config["root_basename"]
                else
                  @config["index_basename"]
                end
end

#dayObject



87
88
89
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 87

def day
  @day ||= date.strftime("%d")
end

#dirObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 68

def dir
  @dir ||= begin
    format = @config.dig("permalink", type.to_s)
    ::Jekyll::URL.new(
      template: format,
      placeholders: dir_placeholders,
      permalink: nil
    ).to_s
  end
end

#monthObject



83
84
85
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 83

def month
  @month ||= date.strftime("%m")
end

#nameObject



109
110
111
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 109

def name
  @name ||= "#{basename}#{ext}"
end

#sub_pagesObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 35

def sub_pages
  @sub_pages ||= if current_depth < @depths
                   child_type = HIERARCHY[type.to_s].to_sym
                   group_collection_by(collection, child_type).each_with_object([]) do |(attr, items), pages|
                     fields = date_fields.merge(child_type => attr)
                     child_page = Page.new(child_type, @site, @config, self, items, @depths, **fields)
                     add_child_page(pages, child_page)
                   end
                 else
                   []
                 end
end

#titleObject



91
92
93
94
95
96
97
98
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 91

def title
  @title ||= case title_format_type
             when :date
               date.strftime(title_format_style)
             when :string
               format(title_format_style, year: year, month: month, day: day)
             end
end

#url_placeholdersObject



48
49
50
51
52
53
54
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 48

def url_placeholders
  {
    path: dir,
    basename: basename,
    output_ext: output_ext
  }
end

#yearObject



79
80
81
# File 'lib/coffeebrew_jekyll_archives/page.rb', line 79

def year
  @year ||= date.strftime("%Y")
end