Skip to content

Commit

Permalink
Provides metatags listing functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
marano committed May 4, 2013
1 parent 9ff4cde commit 90f83f0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/rollin.rb
Expand Up @@ -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
11 changes: 7 additions & 4 deletions lib/rollin/article.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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}"
Expand Down
15 changes: 15 additions & 0 deletions lib/rollin/blog.rb
Expand Up @@ -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 })
Expand Down
7 changes: 7 additions & 0 deletions lib/rollin/metatag_key.rb
@@ -0,0 +1,7 @@
class Rollin::MetatagKey
attr_reader :label, :values

def initialize(label, values)
@label, @values = label, values
end
end
7 changes: 7 additions & 0 deletions lib/rollin/metatag_value.rb
@@ -0,0 +1,7 @@
class Rollin::MetatagValue
attr_reader :content, :articles

def initialize(content, articles)
@content, @articles = content, articles
end
end
45 changes: 40 additions & 5 deletions spec/rollin_spec.rb
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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)) }
Expand Down

0 comments on commit 90f83f0

Please sign in to comment.