Permalink
Browse files

Provides metatags listing functionality.

  • Loading branch information...
1 parent 9ff4cde commit 90f83f09c33d0829037832343e9654d6bbf1389a @marano committed May 4, 2013
Showing with 78 additions and 9 deletions.
  1. +2 −0 lib/rollin.rb
  2. +7 −4 lib/rollin/article.rb
  3. +15 −0 lib/rollin/blog.rb
  4. +7 −0 lib/rollin/metatag_key.rb
  5. +7 −0 lib/rollin/metatag_value.rb
  6. +40 −5 spec/rollin_spec.rb
View
@@ -7,6 +7,8 @@
require "rollin/article"
require "rollin/month_archive"
require "rollin/year_archive"
+require "rollin/metatag_key"
+require "rollin/metatag_value"
module Rollin
end
View
@@ -12,13 +12,14 @@ def initialize(source_file)
end
def title
- metatags[:title] || @title_from_filename
+ metatags['title'] || @title_from_filename
end
def matches?(search)
- search = search.clone
-
return true if @id == search
+ return false if @searh.is_a? String
+
+ search = search.clone
if search.has_key?(:year)
return false if search.delete(:year) != @year
@@ -30,6 +31,8 @@ def matches?(search)
end
end
+ search = search.inject({}) { |memo, (k,v)| memo[k.to_s] = v; memo }
+
if search.keys.empty?
return true
else
@@ -47,7 +50,7 @@ def metatags
if content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
content = $POSTMATCH
- return YAML.safe_load($1).inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
+ return YAML.safe_load($1)
end
rescue SyntaxError => e
puts "YAML Exception reading #{File.join(@source_file)}: #{e.message}"
View
@@ -15,6 +15,21 @@ def articles(search=nil)
end
end
+ def metatags
+ metatag_labels = articles.map do |article|
+ article.metatags.keys
+ end.flatten.uniq
+
+ metatag_labels.map do |metatag_label|
+ values = articles.select { |article| article.metatags.has_key?(metatag_label) }.map do |article|
+ article.metatags[metatag_label]
+ end.flatten.uniq.map do |metatag_content|
+ Rollin::MetatagValue.new(metatag_content, articles(metatag_label => metatag_content))
+ end
+ Rollin::MetatagKey.new(metatag_label, values)
+ end
+ end
+
def annual_archive
monthly_archive.map { |month_archive| month_archive.year }.uniq.map do |year|
Rollin::YearArchive.new(year, monthly_archive.select { |aMonth| aMonth.year == year })
@@ -0,0 +1,7 @@
+class Rollin::MetatagKey
+ attr_reader :label, :values
+
+ def initialize(label, values)
+ @label, @values = label, values
+ end
+end
@@ -0,0 +1,7 @@
+class Rollin::MetatagValue
+ attr_reader :content, :articles
+
+ def initialize(content, articles)
+ @content, @articles = content, articles
+ end
+end
View
@@ -32,7 +32,7 @@
end
it 'exposes the list of defined metatags' do
- article_with_custom_metatags.metatags.should == { tags: ['manero', 'massa', 'bacana'], published: false }
+ article_with_custom_metatags.metatags.should == { 'tags' => ['manero', 'massa', 'bacana'], 'published' => false }
end
end
@@ -47,11 +47,17 @@
end
it 'searches by metatags' do
- blog.article(tags: 'manero').should == article_with_custom_metatags
- blog.articles(tags: 'manero').should == [ article_with_custom_metatags ]
+ blog.article(:tags => 'manero').should == article_with_custom_metatags
+ blog.article('tags' => 'manero').should == article_with_custom_metatags
- blog.article(published: false).should == article_with_custom_metatags
- blog.articles(published: false).should == [ article_with_custom_metatags ]
+ blog.articles(:tags => 'manero').should == [ article_with_custom_metatags ]
+ blog.articles('tags' => 'manero').should == [ article_with_custom_metatags ]
+
+ blog.article(:published => false).should == article_with_custom_metatags
+ blog.article('published' => false).should == article_with_custom_metatags
+
+ blog.articles(:published => false).should == [ article_with_custom_metatags ]
+ blog.articles('published' => false).should == [ article_with_custom_metatags ]
end
it 'searches by date' do
@@ -66,6 +72,35 @@
end
end
+ context 'inquiring metatags' do
+ let (:first_article) { blog.articles[0] }
+ let (:second_article) { blog.articles[1] }
+ let (:third_article) { blog.articles[2] }
+
+ it 'shows a list of existent metatags' do
+ blog.should have(3).metatags
+
+ blog.metatags[0].label.should == 'title'
+ blog.metatags[0].should have(1).values
+ blog.metatags[0].values[0].content.should == 'This is a super post!'
+ blog.metatags[0].values[0].articles.should == [ second_article ]
+
+ blog.metatags[1].label.should == 'tags'
+ blog.metatags[1].should have(3).values
+ blog.metatags[1].values[0].content.should == 'manero'
+ blog.metatags[1].values[0].articles.should == [ third_article ]
+ blog.metatags[1].values[1].content.should == 'massa'
+ blog.metatags[1].values[1].articles.should == [ third_article ]
+ blog.metatags[1].values[2].content.should == 'bacana'
+ blog.metatags[1].values[2].articles.should == [ third_article ]
+
+ blog.metatags[2].label.should == 'published'
+ blog.metatags[2].should have(1).values
+ blog.metatags[2].values[0].content.should == false
+ blog.metatags[2].values[0].articles.should == [ third_article ]
+ end
+ end
+
context 'listing articles' do
let (:first_article) { TestArticle.new(id: '2013_05_01_My_first_post', title: 'My first post', date: Date.new(2013, 5, 1)) }
let (:second_article) { TestArticle.new(id: '2013_05_02_My_second_post', title: 'This is a super post!', date: Date.new(2013, 5, 2)) }

0 comments on commit 90f83f0

Please sign in to comment.