Class: Kind

Inherits:
Dynasnip show all
Defined in:
lib/vanilla/dynasnips/kind.rb

Instance Attribute Summary

Attributes inherited from Vanilla::Renderers::Base

#app

Instance Method Summary collapse

Methods inherited from Dynasnip

all, attribute, build_snip, #method_missing, persist!, persist_all!, snip_attributes, snip_name, usage

Methods inherited from Vanilla::Renderers::Base

escape_curly_braces, #include_snips, #initialize, #prepare, #process_text, #raw_content, render, #render, #render_without_including_snips, snip_regexp

Constructor Details

This class inherits a constructor from Vanilla::Renderers::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dynasnip

Instance Method Details

#handle(kind, limit = 10, as = :html) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/vanilla/dynasnips/kind.rb', line 6

def handle(kind, limit=10, as=:html)
  as = as.to_sym
  snips = Soup.sieve(:kind => kind)
  entries = snips.sort_by { |s| s.created_at || '' }.reverse[0...limit.to_i].map do |snip|
    render_entry_in_template(snip, as, kind)
  end
  render_entry_collection(snips, entries, as, kind)
end

#prepare_snip_contents(snip) ⇒ Object



37
38
39
40
41
# File 'lib/vanilla/dynasnips/kind.rb', line 37

def prepare_snip_contents(snip)
  rendered_snip = app.render(snip)
  # make all the links absolute
  rendered_snip.gsub(/href="\//, "href=\"http://#{domain}/")
end

#render_entry_collection(snips, entries, as, kind) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vanilla/dynasnips/kind.rb', line 43

def render_entry_collection(snips, entries, as, kind)
  case as
  when :html
    entries.join
  when :xml
    Atom::Feed.new do |f|
      f.title = feed_title
      f.updated = DateTime.parse(snips[0].updated_at)
      f.id = "tag:#{domain},2008-06-01:kind/#{kind}"
      f.entries = entries
    end.to_xml
  end
end

#render_entry_in_template(snip, as, kind) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vanilla/dynasnips/kind.rb', line 15

def render_entry_in_template(snip, as, kind)
  rendered_contents = prepare_snip_contents(snip)
  case as
  when :html
    snip_template.
      gsub('SNIP_KIND', kind).
      gsub('SNIP_NAME', snip.name).
      gsub('CREATED_AT', snip.created_at || '').
      gsub('SNIP_CONTENT', rendered_contents)
  when :xml
    Atom::Entry.new do |e|
      e.published = DateTime.parse(snip.created_at)
      e.updated = DateTime.parse(snip.updated_at || snip.created_at)
      e.content = Atom::Content::Html.new(rendered_contents)
      e.title = snip.name
      e.authors = [Atom::Person.new(:name => snip.author || domain)]
      e.links << Atom::Link.new(:href => "http://#{domain}#{Vanilla::Routes.url_to(snip.name)}")
      e.id = "tag:#{domain},#{(snip.created_at || Date.today.to_s).split[0]}:/#{snip.name}"
    end
  end
end