Skip to content

Commit

Permalink
Changes api method names and introduces search by date.
Browse files Browse the repository at this point in the history
  • Loading branch information
marano committed May 3, 2013
1 parent 0526b32 commit 345f777
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
26 changes: 19 additions & 7 deletions lib/rollin/blog.rb
Expand Up @@ -3,17 +3,21 @@ def initialize(options = {})
@articles_folder = options[:articles_folder] || 'articles'
end

def find(search)
articles.find { |article| article.matches?(search) }
def article(search)
read_articles.find { |article| article.matches?(search) }
end

def find_all(search)
articles.select { |article| article.matches?(search) }
def articles(search=nil)
if search.nil?
read_articles
else
read_articles.select { |article| article.matches?(search) }
end
end

def articles
Dir["#{@articles_folder}/**/*.mk"].sort.map do |article_source|
Rollin::Article.new(article_source)
def articles_by_publication(year, month=nil, day=nil)
articles.select do |article|
article.year == year && (month.nil? || article.month == month) && (day.nil? || day == article.day)
end
end

Expand All @@ -31,4 +35,12 @@ def monthly_archive
Rollin::MonthArchive.new(year, month, articles_for_month)
end
end

private

def read_articles
Dir["#{@articles_folder}/**/*.mk"].sort.map do |article_source|
Rollin::Article.new(article_source)
end
end
end
18 changes: 13 additions & 5 deletions spec/rollin_spec.rb
Expand Up @@ -38,18 +38,26 @@

context 'searching for articles' do
let (:first_article) { blog.articles.first }
let (:second_article) { blog.articles[1] }
let (:third_article) { blog.articles[2] }
let (:article_with_custom_metatags) { blog.articles[2] }

it 'searches by article id' do
blog.find('2013_05_01_My_first_post').should == first_article
blog.article('2013_05_01_My_first_post').should == first_article
end

it 'searches by metatags' do
blog.find(:tags => 'manero').should == article_with_custom_metatags
blog.find_all(:tags => 'manero').should == [ article_with_custom_metatags ]
blog.article(:tags => 'manero').should == article_with_custom_metatags
blog.articles(:tags => 'manero').should == [ article_with_custom_metatags ]

blog.find(:published => false).should == article_with_custom_metatags
blog.find_all(: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 ]
end

it 'searches by date' do
blog.articles_by_publication(2013).should include(first_article, second_article, third_article)
blog.articles_by_publication(2013, 5).should include(first_article, second_article)
blog.articles_by_publication(2013, 5, 1).should include(first_article)
end
end

Expand Down

0 comments on commit 345f777

Please sign in to comment.