Class: Dimples::Page
- Inherits:
-
Object
- Object
- Dimples::Page
- Defined in:
- lib/dimples/page.rb
Overview
A single page on a site.
Instance Attribute Summary collapse
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #extension ⇒ Object
- #filename ⇒ Object
-
#initialize(site, path = nil) ⇒ Page
constructor
A new instance of Page.
- #render(context = {}) ⇒ Object
- #write(output_directory, context = {}) ⇒ Object
Constructor Details
#initialize(site, path = nil) ⇒ Page
Returns a new instance of Page.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/dimples/page.rb', line 10 def initialize(site, path = nil) @site = site @path = path if @path data = File.read(@path) @contents, @metadata = FrontMatter.parse(data) 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)
55 56 57 58 59 60 61 62 63 |
# File 'lib/dimples/page.rb', line 55 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.
6 7 8 |
# File 'lib/dimples/page.rb', line 6 def contents @contents end |
#metadata ⇒ Object
Returns the value of attribute metadata.
7 8 9 |
# File 'lib/dimples/page.rb', line 7 def @metadata end |
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/dimples/page.rb', line 8 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 |
#render(context = {}) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/dimples/page.rb', line 31 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
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 |