Class: Henshin::Archive

Inherits:
Hash
  • Object
show all
Defined in:
lib/henshin/archive.rb

Overview

This should really be a hash which just holds post objects, when #to_hash is called, each post object held is turned has Post#to_hash called and this is returned. It also controls writing of the archive pages.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#to_options

Constructor Details

#initialize(site) ⇒ Archive

Create a new instance of Archive



12
13
14
# File 'lib/henshin/archive.rb', line 12

def initialize(site)
  @site = site
end

Instance Attribute Details

#siteObject

Returns the value of attribute site.



9
10
11
# File 'lib/henshin/archive.rb', line 9

def site
  @site
end

Instance Method Details

#<<(post) ⇒ Object

Adds the post to the correct year array, month array and day array within the archive

Parameters:

  • post (Post)

    to be added to the archive



19
20
21
22
23
24
25
26
27
28
# File 'lib/henshin/archive.rb', line 19

def <<(post)
  return nil unless post.data['date']
  date = post.data['date']
  year, month, day = date.year, date.month, date.day
  
  self[year] ||= {}
  self[year][month] ||= {}
  self[year][month][day] ||= []
  self[year][month][day] << post
end

#to_hashHash

Converts the archive to a hash suitable for putting in a layout by calling #to_hash for all of the posts it contains

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/henshin/archive.rb', line 34

def to_hash
  r = {}
  self.each do |y, i|
    r[y] = {}
    i.each do |m, i|
      r[y][m] = {}
     i.each do |d, i|
       r[y][m][d] = []
       i.each do |j|
         r[y][m][d] << j.to_hash
       end
     end
    end
  end
  
  r
end

#writeObject

Writes the archives if the correct layouts are present



53
54
55
56
57
# File 'lib/henshin/archive.rb', line 53

def write
  self.write_years if @site.layouts['archive_year']
  self.write_months if @site.layouts['archive_month']
  self.write_dates if @site.layouts['archive_date']
end

#write_datesObject

Writes all of the archives for the days



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/henshin/archive.rb', line 106

def write_dates
  self.to_hash.each do |year, v|
    v.each do |month, v|
      v.each do |date, v|
        t = @site.root + year.to_s + month.to_s + date.to_s + 'index.html'
        
        payload = {
          :name => 'archive', 
          :payload => {
            'posts' => self.to_hash,
            'year' => year,
            'month' => month,
            'day' => date
          }
        }
        page = Gen.new(t, @site, payload)
        page.read 
        page.data['layout'] = @site.layouts['archive_date']
        
        page.render
        page.write
      end
    end
  end
end

#write_monthsObject

Writes all of the archives for the months



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/henshin/archive.rb', line 82

def write_months
  self.to_hash.each do |year, v|
    v.each do |month, v|          
      t = @site.root + year.to_s + month.to_s + 'index.html'

      payload = {
        :name => 'archive', 
        :payload => {
          'posts' => self.to_hash,
          'year' => year,
          'month' => month
        }
      }
      page = Gen.new(t, @site, payload)
      page.read
      page.data['layout'] = @site.layouts['archive_month']
      
      page.render
      page.write
    end
  end
end

#write_yearsObject

This writes all the archives for years



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/henshin/archive.rb', line 60

def write_years
  self.to_hash.each do |year, v|
    # need to fake the file loc so that gen automatically creates permalink
    t = @site.root + year.to_s + 'index.html'

    payload = {
      :name => 'archive', 
      :payload => {
        'posts' => self.to_hash,
        'year' => year
      }
    }
    page = Gen.new(t, @site, payload)
    page.read
    page.data['layout'] = @site.layouts['archive_year']
    
    page.render
    page.write
  end
end