Class: Henshin::Labels

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

Overview

This is basically a front for tags and categories, because they are so similar it makes sense to condense them into one class!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, site) ⇒ Labels

Creates a new instance of labels

Parameters:

  • base (String)

    the base part of the urls, eg. category, tag

  • site (Site)

    that the labels belong to



14
15
16
17
# File 'lib/henshin/labels.rb', line 14

def initialize(base, site)
  @base = base
  @site = site
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



8
9
10
# File 'lib/henshin/labels.rb', line 8

def base
  @base
end

#siteObject

Returns the value of attribute site.



8
9
10
# File 'lib/henshin/labels.rb', line 8

def site
  @site
end

Instance Method Details

#<<(post) ⇒ Object

TODO:

Make it a bit more abstract, actually hard coding stuff in will lead to problems!

Adds the given post to the correct category object in the array or creates the category and adds the post to that

Parameters:

  • post (Post)

    to be added

  • k (String, Array)

    label(s) to be added to



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/henshin/labels.rb', line 27

def <<(post)
  k = nil
  if base == 'tag'
    k = post.data['tags']
  elsif base == 'category'
    k = [post.data['category']]
  end
  
  k.each do |j|
    unless self.map{|i| i.name}.include?(j)
      super Henshin::Label.new(j, @base, @site)
    end
    i = self.find_index {|i| i.name == j}
    self[i].posts << post
  end
end

#fake_write_pathPathname

Need a fake path where the file would have been so as to trick the gen into constructing the correct paths

Returns:



67
68
69
# File 'lib/henshin/labels.rb', line 67

def fake_write_path
  @site.root + self.permalink[1..-1]
end

Returns permalink for label index.

Returns:

  • (String)

    permalink for label index



54
55
56
# File 'lib/henshin/labels.rb', line 54

def permalink
  File.join(@site.base, @base, "index.html")
end

#to_hashObject

Converts the labels to a hash for use in a layout parser



45
46
47
48
49
50
51
# File 'lib/henshin/labels.rb', line 45

def to_hash
  r = []
  self.each do |i|
    r << i.to_hash
  end
  r
end

#urlString

Returns base url for label.

Returns:

  • (String)

    base url for label



59
60
61
# File 'lib/henshin/labels.rb', line 59

def url
  File.join(@site.base, @base)
end

#writeObject

Writes the category index, then writes the individual category pages



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/henshin/labels.rb', line 73

def write
  if @site.layouts["#{@base}_index"]
    page = Gen.new(self.fake_write_path, @site)
    page.read
    page.data['layout'] = @site.layouts["#{@base}_index"]
    
    page.render
    page.write
  end
  if @site.layouts["#{@base}_page"]
    self.each {|label| label.write }
  end
end