Class: Bloggit::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/bloggit/page.rb

Overview

Page

A Page is much like a Post (Bloggit::Post)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, filename, body, atts = {}) ⇒ Page

Returns a new instance of Page.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bloggit/page.rb', line 10

def initialize(site, filename, body, atts={})
  @site = site unless site.nil?
  @filename = filename
  @data = atts.clone
  @data.reverse_merge!(site.settings.pages) unless site.nil?
  
  @source = body

  @data.slug = @filename.split('_')[-1].gsub('.page', '') unless @data.has_key? 'slug'
  @data.title = "#{@data.slug.gsub('-', ' ')}".titleize unless @data.has_key? 'title'
  @data.status = 'publish' unless @data.has_key? 'status'

  if @data.has_key? 'tags'
    @data.tags.uniq!
    @data.tags.each { |tag| Tag.register_page( tag, self ) if self.is_publishable? }
  else
    @data.tags = []
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/bloggit/page.rb', line 65

def method_missing(name,*args)
  if @data.has_key? name.to_s
    @data[name.to_s]
  else
    super(name, *args)
  end
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



8
9
10
# File 'lib/bloggit/page.rb', line 8

def filename
  @filename
end

#siteObject (readonly)

Returns the value of attribute site.



8
9
10
# File 'lib/bloggit/page.rb', line 8

def site
  @site
end

#sourceObject (readonly)

Returns the value of attribute source.



8
9
10
# File 'lib/bloggit/page.rb', line 8

def source
  @source
end

Class Method Details

.find_by_tag(tag) ⇒ Object



76
77
78
# File 'lib/bloggit/page.rb', line 76

def find_by_tag(tag)
  find_by_tags( [tag] )
end

.find_by_tags(tags = []) ⇒ Object



80
81
82
# File 'lib/bloggit/page.rb', line 80

def find_by_tags(tags=[])
  
end

.from_file(path, site = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/bloggit/page.rb', line 84

def from_file(path, site=nil)
  path = File.expand_path(path)
  raise "File must exist" unless File.exists?( path )
  yml_docs = YAML::load_stream( File.open(path, 'r') )
  raise "page is missing a document part" if yml_docs.documents.length != 2
  raise "page format is invalid" unless yml_docs[0].is_a?(Hash) and yml_docs[1].is_a?(String)
  atts = yml_docs[0]
  body = yml_docs[1]
  new( site, File.basename(path), body, atts )
end

.to_file(title, site = nil, options = {}) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/bloggit/page.rb', line 95

def to_file(title, site=nil, options={})
  slug = title.to_s.to_slug
  
  path = "#{slug}.page"
  page = new( site, path, "New Page\n\n", 'slug'=>slug, 'title'=>title )
  File.open( File.join(Dir.getwd, 'pages', path), 'w' ) {|f| f.write page.to_yaml }
  page
end

Instance Method Details

#has_tags?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/bloggit/page.rb', line 38

def has_tags?
  @data.has_key? 'tags'
end

#is_draft?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/bloggit/page.rb', line 30

def is_draft?
  @data.status == 'draft'
end

#is_publishable?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/bloggit/page.rb', line 34

def is_publishable?
  !is_draft?
end


42
43
44
45
# File 'lib/bloggit/page.rb', line 42

def permalink
  # Get base folder from settings...
  "#{@data.slug}.html"
end

#render_content(template_binding = nil) ⇒ Object



47
48
49
50
51
52
# File 'lib/bloggit/page.rb', line 47

def render_content(template_binding=nil)
  src = @source
  src = Template.from_text(src).template.result(template_binding) unless template_binding.nil?
  format = @data.has_key?('format') ? @data['format'].to_s.downcase.to_sym : :simple
  TextFormatter.render(src, format)
end

#to_jsonObject



59
60
61
62
63
# File 'lib/bloggit/page.rb', line 59

def to_json
	data_hsh = @data.clone
  data_hsh['source'] = @source
  data_hsh.to_json
end

#to_yamlObject



54
55
56
57
# File 'lib/bloggit/page.rb', line 54

def to_yaml    
  yml_s = @data.to_yaml
  yml_s << @source.to_yaml
end