Class: RubySlippers::Engine::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_slippers/site.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Returns a new instance of Site.



4
5
6
# File 'lib/ruby_slippers/site.rb', line 4

def initialize config
  @config = config
end

Instance Method Details

#/Object



55
56
57
# File 'lib/ruby_slippers/site.rb', line 55

def /
  self[:root]
end

#[](*args) ⇒ Object



8
9
10
# File 'lib/ruby_slippers/site.rb', line 8

def [] *args
  @config[*args]
end

#[]=(key, value) ⇒ Object



12
13
14
# File 'lib/ruby_slippers/site.rb', line 12

def []= key, value
  @config.set key, value
end

#archives(filter = "") ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_slippers/site.rb', line 30

def archives filter = ""
  entries = ! self.articles.empty??
    self.articles.select do |a|
      filter !~ /^\d{4}/ || File.basename(a) =~ /^#{filter}/
    end.reverse.map do |article|
      Article.new article, @config
    end : []

  return :archives => Archives.new(entries, @config)
end

#article(route) ⇒ Object



41
42
43
# File 'lib/ruby_slippers/site.rb', line 41

def article route
  Article.new("#{Paths[:articles]}/#{route.join('-')}.#{self[:ext]}", @config).load
end

#go(route, env = {}, type = :html) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruby_slippers/site.rb', line 59

def go route, env = {}, type = :html
  route << self./ if route.empty?
  type, path = type =~ /html|xml|json/ ? type.to_sym : :html, route.join('/')
  context = lambda do |data, page|
    Context.new(data, @config, path, env).render(page, type)
  end

  body, status = if Context.new.respond_to?(:"to_#{type}")
    if route.first =~ /\d{4}/
      case route.size
        when 1..3
          context[archives(route * '-'), :archives]
        when 4
          context[article(route), :article]
        else http 400
      end
    elsif route.first == "tagged"
      context[tagged(route.last), :tagged]
    elsif respond_to?(path)
      context[send(path, type), path.to_sym]
    elsif (repo = @config[:github][:repos].grep(/#{path}/).first) &&
          !@config[:github][:user].empty?
      context[Repo.new(repo, @config), :repo]
    else
      context[{}, path.to_sym]
    end
  else
    http 400
  end

rescue NoMethodError, Errno::ENOENT => e
  return :body => http(404).first, :type => :html, :status => 404
else
  return :body => body || "", :type => type, :status => status || 200
end

#index(type = :html) ⇒ Object



23
24
25
26
27
28
# File 'lib/ruby_slippers/site.rb', line 23

def index type = :html
  articles = type == :html ? self.articles.reverse : self.articles
  {:articles => articles.map do |article|
    Article.new article, @config
  end}.merge archives
end

#sitemap(type = :xml) ⇒ Object



16
17
18
19
20
21
# File 'lib/ruby_slippers/site.rb', line 16

def sitemap type = :xml
  articles = type == :html ? self.articles.reverse : self.articles
  {:articles => articles.map do |article|
    Article.new article, @config
  end}.merge archives
end

#tagged(tag) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/ruby_slippers/site.rb', line 45

def tagged tag
  articles = self.articles.collect do |article|
    Article.new article, @config
  end.select do |article|
    article[:tags].index(tag.humanize.downcase) if article[:tags]
  end

  {:articles => articles, :tagged => tag}
end