Class: Henshin::Archive

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Archive

Returns a new instance of Archive.



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

def initialize( site )
  @archive = Hash.new {|h, k| h[k] = Hash.new {|h, k| h[k] = Hash.new {|h, k| h[k] = []} }}
  @site = site
  @config = site.config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/henshin/archive.rb', line 5

def config
  @config
end

Instance Method Details

#add_post(post) ⇒ Object



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

def add_post( post )
  date = post.date
  @archive[date.year.to_s][date.month.to_s][date.day.to_s] << post.to_hash
end

#to_date_hashObject

Creates a hash with posts separated by year, month then date



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/henshin/archive.rb', line 62

def to_date_hash
  r = Hash.new {|h, k| h[k] = Hash.new {|h, k| h[k] = Hash.new {|h, k| h[k] = []} }}
  @archive.each do |year, m|
    m.each do |month, d|
      d.each do |date, p|
        r[year][month][date] << p
        r[year][month][date].flatten!
      end
    end
  end
  r
end

#to_hashObject

Turns the whole archive into a hash. Probably the least efficient thing in the world, but it works



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/henshin/archive.rb', line 19

def to_hash
  if @hashed
    @hashed
  else
    @hashed = Hash.new do |h, k| 
      h[k] = Hash.new do |h, k| # years
        if k == 'posts'
          h[k] = []
        else
          h[k] = Hash.new do |h, k| # months
            if k == 'posts'
              h[k] = []
            else
              h[k] = Hash.new do |h, k| # days
                if k == 'posts'
                  h[k] = []
                else
                  h[k] = {}
                end
              end # /days
            end
          end # /months
        end
      end # /years
    end 
    @archive.each do |y, month|
      month.each do |m, date|
        date.each do |d, p|            
          @hashed[y]['posts'] << p
          @hashed[y][m]['posts'] << p
          @hashed[y][m][d]['posts'] << p
          
          @hashed[y]['posts'].flatten!
          @hashed[y][m]['posts'].flatten!
          @hashed[y][m][d]['posts'].flatten!
        end
      end
    end
    
  end
end

#to_month_hashObject

Creates a hash with posts separated by year then month



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/henshin/archive.rb', line 76

def to_month_hash
  r = Hash.new {|h, k| h[k] = Hash.new {|h, k| h[k] = []} }
  @archive.each do |year, m|
    m.each do |month, d|
      d.each do |date, p|
        r[year][month] << p
        r[year][month].flatten!
      end
    end
  end
  r
end

#to_year_hashObject

Creates a hash with posts separated by year



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/henshin/archive.rb', line 90

def to_year_hash
  r = Hash.new {|h, k| h[k] = []}
  @archive.each do |year, m|
    m.each do |month, d|
      d.each do |date, p|
        r[year] << p
        r[year].flatten!
      end
    end
  end
  r
end

#writeObject

Writes the archive pages



105
106
107
108
109
# File 'lib/henshin/archive.rb', line 105

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/henshin/archive.rb', line 142

def write_dates
  dates = self.to_date_hash
  dates.each do |year, posts|
    posts.each do |month, posts|
      posts.each do |date, posts|
        write_path = File.join( @config[:root], year, month, date, 'index.html' )
        t = Time.parse("#{year}/#{month}/#{date}")
        payload = {:name => 'archive', :payload => {'date' => t, 'posts' => dates[year][month][date]} }
        page = Gen.new( write_path, @site, payload )
        page.layout = @site.layouts['archive_date']
        
        page.render
        page.write
      end
    end
  end
end

#write_monthsObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/henshin/archive.rb', line 126

def write_months
  months = self.to_month_hash
  months.each do |year, posts|
    posts.each do |month, posts|
      write_path = File.join( @config[:root], year, month, 'index.html' )
      t = Time.parse("#{year}/#{month}/01")
      payload = {:name => 'archive', :payload => {'date' => t, 'posts' => months[year][month]} }
      page = Gen.new( write_path, @site, payload )
      page.layout = @site.layouts['archive_month']
      
      page.render
      page.write
    end
  end
end

#write_yearsObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/henshin/archive.rb', line 111

def write_years
  years = self.to_year_hash
  years.each do |year, posts|
    write_path = File.join( @config[:root], year, 'index.html' )
    # date should give the full date, as a Time object!! and the others
    t = Time.parse("#{year}/01/01")
    payload = {:name => 'archive', :payload => {'date' => t, 'posts' => years[year]} }
    page = Gen.new( write_path, @site, payload )
    page.layout = @site.layouts['archive_year']
    
    page.render
    page.write
  end
end