Class: SemiStatic::Tags
Overview
A list of all tag in the site.
Instance Attribute Summary collapse
-
#output_dir ⇒ Object
readonly
The output directory for the tag index pages.
-
#uri ⇒ Object
readonly
The URI for the collection’s index page.
Class Method Summary collapse
-
.slugize(name) ⇒ Object
Convert the given display name to a URL-ified one.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Find the tag with the given name.
-
#each(options = {}, &block) ⇒ Object
Iterate over all tags.
-
#initialize(slug) ⇒ Tags
constructor
Initializes a new collection with the given name.
-
#names ⇒ Object
List all tag’ display names, sorted alphabetically.
-
#nested_values ⇒ Object
Use each tag’s name as a path, and arrange them into tree.
-
#slugs ⇒ Object
List all tag’ URL-ified names, sorted alphabetically.
Methods included from CoreExt::Hash
Constructor Details
#initialize(slug) ⇒ Tags
Initializes a new collection with the given name
57 58 59 60 61 |
# File 'lib/semi-static/tags.rb', line 57 def initialize(slug) super() @output_dir = slug.to_s @uri = "/#{output_dir}/" end |
Instance Attribute Details
#output_dir ⇒ Object (readonly)
The output directory for the tag index pages.
47 48 49 |
# File 'lib/semi-static/tags.rb', line 47 def output_dir @output_dir end |
#uri ⇒ Object (readonly)
The URI for the collection’s index page.
43 44 45 |
# File 'lib/semi-static/tags.rb', line 43 def uri @uri end |
Class Method Details
.slugize(name) ⇒ Object
Convert the given display name to a URL-ified one.
51 52 53 |
# File 'lib/semi-static/tags.rb', line 51 def self.slugize(name) name.to_s.gsub(/ /, '-').downcase.to_sym end |
Instance Method Details
#[](name) ⇒ Object
Find the tag with the given name.
name: The Tag’s name (either its dipslay name or URL-ified name).
67 68 69 70 71 72 73 74 75 |
# File 'lib/semi-static/tags.rb', line 67 def [](name) slug = Tags.slugize name tag = super(slug) if tag.nil? tag = Tag.new self, name self[slug] = tag end return tag end |
#each(options = {}, &block) ⇒ Object
Iterate over all tags
order can be:
+:name+: Sorted alphabetically by display name
+:count+: Sorted by post count, largest to smallest
+:tree+: Sorted alphabetically bo display name and arranged into a tree
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/semi-static/tags.rb', line 121 def each(={}, &block) list = case [:order] when :name, nil values.sort { |l,r| l.name.casecmp r.name } when :count values.sort { |l,r| r.count <=> l.count } when :tree nested_values else raise ArgumentError, "Unknown order: #{[:order]}" end list.each(&block) end |
#names ⇒ Object
List all tag’ display names, sorted alphabetically.
85 86 87 |
# File 'lib/semi-static/tags.rb', line 85 def names values.collect { |c| c.name }.sort { |l,r| l.to_s <=> r.to_s } end |
#nested_values ⇒ Object
Use each tag’s name as a path, and arrange them into tree.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/semi-static/tags.rb', line 91 def nested_values result = Hash.new { |hash,key| hash[key] = Hash.new } values.each do |item| parent, child = item.slug.to_s.split '/' if child.nil? result[parent.to_sym][nil] = item else result[parent.to_sym][child.to_sym] = item end end result.collect do |parent_slug,items| parent = items.delete(nil) children = items.values.sort { |l,r| l.name.casecmp r.name } class << parent attr_accessor :slug, :children end parent.children = children parent end.sort { |l,r| l.name.casecmp r.name } end |
#slugs ⇒ Object
List all tag’ URL-ified names, sorted alphabetically.
79 80 81 |
# File 'lib/semi-static/tags.rb', line 79 def slugs keys.sort { |l,r| l.to_s <=> r.to_s } end |