Class: Dimples::Page

Inherits:
Object
  • Object
show all
Includes:
Frontable
Defined in:
lib/dimples/page.rb

Overview

A single page on a site.

Direct Known Subclasses

Post

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Frontable

#read_with_front_matter

Constructor Details

#initialize(site, path = nil) ⇒ Page

Returns a new instance of Page.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dimples/page.rb', line 12

def initialize(site, path = nil)
  @site = site
  @path = path

  if @path
    @contents, @metadata = read_with_front_matter(@path)
  else
    @contents = ''
    @metadata = {}
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



60
61
62
63
64
65
66
67
68
# File 'lib/dimples/page.rb', line 60

def method_missing(method_name, *args, &block)
  if @metadata.key?(method_name)
    @metadata[method_name]
  elsif (matches = method_name.match(/([a-z_]+)=/))
    @metadata[matches[1].to_sym] = args[0]
  else
    super
  end
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



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

def contents
  @contents
end

#metadataObject

Returns the value of attribute metadata.



9
10
11
# File 'lib/dimples/page.rb', line 9

def 
  @metadata
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/dimples/page.rb', line 10

def path
  @path
end

Instance Method Details

#extensionObject



28
29
30
# File 'lib/dimples/page.rb', line 28

def extension
  @metadata[:extension] || 'html'
end

#filenameObject



24
25
26
# File 'lib/dimples/page.rb', line 24

def filename
  @metadata[:filename] || 'index'
end

#inspectObject



50
51
52
# File 'lib/dimples/page.rb', line 50

def inspect
  "#<#{self.class} @path=#{path}>"
end

#render(context = {}) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/dimples/page.rb', line 32

def render(context = {})
   = @metadata.dup
  .merge!(context[:page]) if context[:page]

  context[:page] = Hashie::Mash.new()

  renderer.render(context)
end

#write(output_directory, context = {}) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/dimples/page.rb', line 41

def write(output_directory, context = {})
  FileUtils.mkdir_p(output_directory) unless Dir.exist?(output_directory)
  output_path = File.join(output_directory, "#{filename}.#{extension}")

  File.write(output_path, render(context))
rescue SystemCallError => e
  raise PublishingError, "Failed to publish file at #{output_path} (#{e})"
end