Class: Brite::Site
- Inherits:
-
Object
- Object
- Brite::Site
- Defined in:
- lib/brite/site.rb
Overview
< Model
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Access to site configuration.
-
#layouts ⇒ Object
readonly
Returns the value of attribute layouts.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#pages ⇒ Object
readonly
Returns and Array of Page instances.
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#posts ⇒ Object
readonly
Returns and Array of Post instances.
-
#url ⇒ Object
Returns a String of the site’s URL.
Instance Method Summary collapse
-
#initialize(location, options = {}) ⇒ Site
constructor
New Site model.
- #inspect ⇒ Object
-
#load_site ⇒ Object
private.
-
#lookup_layout(name) ⇒ Object
Lookup layout by name.
- #lookup_partial(name) ⇒ Object
-
#posts_by_tag ⇒ Object
Returns a Hash posts indexed by tag.
-
#tags ⇒ Object
Returns an Array of all tags used in all posts.
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, ={}) @location = location @config = Config.new(location) .each do |k,v| __send__("#{k}=", v) end @pages = [] @posts = [] @parts = [] @layouts = [] = nil @posts_by_tag = nil load_site end |
Instance Attribute Details
#config ⇒ Object (readonly)
Access to site configuration.
41 42 43 |
# File 'lib/brite/site.rb', line 41 def config @config end |
#layouts ⇒ Object (readonly)
Returns the value of attribute layouts.
53 54 55 |
# File 'lib/brite/site.rb', line 53 def layouts @layouts end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
38 39 40 |
# File 'lib/brite/site.rb', line 38 def location @location end |
#pages ⇒ Object (readonly)
Returns and Array of Page instances.
44 45 46 |
# File 'lib/brite/site.rb', line 44 def pages @pages end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
50 51 52 |
# File 'lib/brite/site.rb', line 50 def parts @parts end |
#posts ⇒ Object (readonly)
Returns and Array of Post instances.
47 48 49 |
# File 'lib/brite/site.rb', line 47 def posts @posts end |
#url ⇒ Object
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
#inspect ⇒ Object
86 87 88 |
# File 'lib/brite/site.rb', line 86 def inspect "#<Brite::Site @url=#{url.inspect}>" end |
#load_site ⇒ Object
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_tag ⇒ Object
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..each do |tag| chart[tag] << post end end chart ) end |
#tags ⇒ Object
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 ||= ( list = [] posts.each{ |post| list.concat(post.) } list.uniq.sort ) end |