Class: Henshin::Archive
- Inherits:
-
Object
- Object
- Henshin::Archive
- Defined in:
- lib/henshin/archive.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #add_post(post) ⇒ Object
-
#initialize(site) ⇒ Archive
constructor
A new instance of Archive.
-
#to_date_hash ⇒ Object
Creates a hash with posts separated by year, month then date.
-
#to_hash ⇒ Object
Turns the whole archive into a hash.
-
#to_month_hash ⇒ Object
Creates a hash with posts separated by year then month.
-
#to_year_hash ⇒ Object
Creates a hash with posts separated by year.
-
#write ⇒ Object
Writes the archive pages.
- #write_dates ⇒ Object
- #write_months ⇒ Object
- #write_years ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object
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_hash ⇒ Object
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_hash ⇒ Object
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_hash ⇒ Object
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_hash ⇒ Object
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 |
#write ⇒ Object
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_dates ⇒ Object
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_months ⇒ Object
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_years ⇒ Object
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 |