Class: Dimples::Page
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.
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/dimples/page.rb', line 11
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
59
60
61
62
63
64
65
66
67
|
# File 'lib/dimples/page.rb', line 59
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
#contents ⇒ Object
Returns the value of attribute contents.
7
8
9
|
# File 'lib/dimples/page.rb', line 7
def contents
@contents
end
|
Returns the value of attribute metadata.
8
9
10
|
# File 'lib/dimples/page.rb', line 8
def metadata
@metadata
end
|
#path ⇒ Object
Returns the value of attribute path.
9
10
11
|
# File 'lib/dimples/page.rb', line 9
def path
@path
end
|
Instance Method Details
#extension ⇒ Object
27
28
29
|
# File 'lib/dimples/page.rb', line 27
def extension
@metadata[:extension] || 'html'
end
|
#filename ⇒ Object
23
24
25
|
# File 'lib/dimples/page.rb', line 23
def filename
@metadata[:filename] || 'index'
end
|
#inspect ⇒ Object
49
50
51
|
# File 'lib/dimples/page.rb', line 49
def inspect
"#<#{self.class} @path=#{path}>"
end
|
#render(context = {}) ⇒ Object
31
32
33
34
35
36
37
38
|
# File 'lib/dimples/page.rb', line 31
def render(context = {})
metadata = @metadata.dup
metadata.merge!(context[:page]) if context[:page]
context[:page] = Hashie::Mash.new(metadata)
renderer.render(context)
end
|
#write(output_directory, context = {}) ⇒ Object
40
41
42
43
44
45
46
47
|
# File 'lib/dimples/page.rb', line 40
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
|