Class: Brite::Site

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

Overview

< Model

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location, options = {}) ⇒ Site

New Site model.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/brite/site.rb', line 18

def initialize(location, options={})
  @location = location
  @config   = Config.new(location)

  options.each do |k,v|
    __send__("#{k}=", v)
  end

  @pages   = []
  @posts   = []
  @parts   = []
  @layouts = []

  @tags         = nil
  @posts_by_tag = nil

  load_site
end

Instance Attribute Details

#configObject (readonly)

Access to site configuration.



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

def config
  @config
end

#layoutsObject (readonly)

Returns the value of attribute layouts.



53
54
55
# File 'lib/brite/site.rb', line 53

def layouts
  @layouts
end

#locationObject (readonly)

Returns the value of attribute location.



38
39
40
# File 'lib/brite/site.rb', line 38

def location
  @location
end

#pagesObject (readonly)

Returns and Array of Page instances.



44
45
46
# File 'lib/brite/site.rb', line 44

def pages
  @pages
end

#partsObject (readonly)

Returns the value of attribute parts.



50
51
52
# File 'lib/brite/site.rb', line 50

def parts
  @parts
end

#postsObject (readonly)

Returns and Array of Post instances.



47
48
49
# File 'lib/brite/site.rb', line 47

def posts
  @posts
end

#urlObject

Returns a String of the site’s URL.



56
57
58
# File 'lib/brite/site.rb', line 56

def url
  @url ||= config.url
end

Instance Method Details

#inspectObject



86
87
88
# File 'lib/brite/site.rb', line 86

def inspect
  "#<Brite::Site @url=#{url.inspect}>"
end

#load_siteObject

private



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/brite/site.rb', line 92

def load_site
  Dir.chdir(location) do
    files = Dir['**/*']
    files.each do |file|
      name = File.basename(file)
      ext  = File.extname(file)
      case ext
      when '.layout'
        layouts << Layout.new(self, file)
      when '.part'
        parts << Part.new(self, file)
      when '.page' #*%w{.markdown .rdoc .textile .whtml}
        page = Page.new(self, file)
        pages << page unless page.draft
      when '.post'
        post = Post.new(self, file)
        posts << post unless post.draft
      end
    end
  end

  @posts.sort!{ |a,b| b.date <=> a.date }
end

#lookup_layout(name) ⇒ Object

Lookup layout by name.



129
130
131
132
133
134
135
# File 'lib/brite/site.rb', line 129

def lookup_layout(name)
  layouts.find do |layout|
    config.layout_path.any? { |path|
      File.join(path, name) == layout.name 
    } or name == layout.name
  end
end

#lookup_partial(name) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/brite/site.rb', line 140

def lookup_partial(name)
  parts.find do |part|
    config.partial_path.any? { |path|
      File.join(path, name) == part.name 
    } or name == part.name
  end
end

#posts_by_tagObject

Returns a Hash posts indexed by tag.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/brite/site.rb', line 73

def posts_by_tag
  @posts_by_tag ||= (
    chart ||= Hash.new{|h,k|h[k]=[]}
    posts.each do |post|
      post.tags.each do |tag|
        chart[tag] << post
      end
    end
    chart
  )
end

#tagsObject

Returns an Array of all tags used in all posts.



64
65
66
67
68
69
70
# File 'lib/brite/site.rb', line 64

def tags
  @tags ||= (
    list = []
    posts.each{ |post| list.concat(post.tags) }
    list.uniq.sort
  )
end