Class: Spandex::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/spandex/finder.rb

Defined Under Namespace

Classes: CaseInsensitiveHash

Instance Method Summary collapse

Constructor Details

#initialize(base_dir, render_options = {}) ⇒ Finder

Returns a new instance of Finder.



13
14
15
16
# File 'lib/spandex/finder.rb', line 13

def initialize(base_dir, render_options = {})
  @base_dir = base_dir
  @render_options = render_options
end

Instance Method Details

#all_articles(include_drafts = false) ⇒ Object



39
40
41
# File 'lib/spandex/finder.rb', line 39

def all_articles(include_drafts = false)
  find_articles(:include_drafts => include_drafts)
end

#all_pagesObject



29
30
31
32
33
34
35
36
37
# File 'lib/spandex/finder.rb', line 29

def all_pages
  roots = []
  @pages ||= CaseInsensitiveHash.new
  Dir.glob(File.join(@base_dir, "**/*"))
    .map{|path| Pathname.new(path)}
    .select{|path| Page.registered?(path)}
    .each{|path| load(path)}
  @pages.values
end

#atom_feed(count, title, author, root, path_to_xml) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/spandex/finder.rb', line 75

def atom_feed(count, title, author, root, path_to_xml)
  articles = all_articles.take(count)
  Atom::Feed.new do |f|
    f.id = root
    f.title = title
    f.links << Atom::Link.new(:href => "http://#{root}#{path_to_xml}", :rel => "self")
    f.links << Atom::Link.new(:href => "http://#{root}", :rel => "alternate")
    f.authors << Atom::Person.new(:name => author)
    f.updated = articles[0].date if articles[0]
    articles.each do |post|
      f.entries << post.to_atom_entry(root)
    end  
  end.to_xml
end

#find_articles(conditions) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spandex/finder.rb', line 43

def find_articles(conditions)
  #require dates - definition of articles
  real_conds = {:has_date => true}
  
  #by default, don't include drafts
  real_conds[:draft] = false unless conditions[:include_drafts]
  real_conds.merge!(conditions.reject{|k| k == :include_drafts})

  #articles should be sorted by date descending
  find_pages(real_conds).sort {|x, y| y.date <=> x.date }
end

#find_pages(conditions) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/spandex/finder.rb', line 55

def find_pages(conditions)
  output = all_pages
  conditions.each do |k, v|
    next if v.nil?
    cond = case k
           when :has_date then lambda {|p| p.date}
           when :draft then lambda {|p| p.draft? == v}
           when :tag then lambda {|p| p.tags.include?(v) }
           when :title then lambda {|p| p.title.match(v)}
           else lambda{|p| true}
           end
    output = output.select(&cond)
  end
  output
end

#get(path) ⇒ Object



18
19
20
21
22
23
# File 'lib/spandex/finder.rb', line 18

def get(path)
  path = Page.pathify path
  paths = Pathname.glob(File.join(@base_dir, "#{path}.*"))
  file = paths.select{|path| Page.registered?(path)}.first
  load(file, path)
end

#get_by_filename(filename) ⇒ Object



25
26
27
# File 'lib/spandex/finder.rb', line 25

def get_by_filename(filename)
  load(filename)
end

#tagsObject



71
72
73
# File 'lib/spandex/finder.rb', line 71

def tags
  @tags ||= all_pages.map{|p| p.tags}.flatten.uniq
end