Class: Dimples::Document
- Inherits:
-
Object
- Object
- Dimples::Document
- Defined in:
- lib/dimples/document.rb
Constant Summary collapse
- FRONT_MATTER_PATTERN =
/^(-{3}\n.*?\n?)^(-{3}*$\n?)/m.freeze
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.
-
#rendered_contents ⇒ Object
Returns the value of attribute rendered_contents.
Instance Method Summary collapse
- #basename ⇒ Object
- #extension ⇒ Object
- #filename ⇒ Object
-
#initialize(path = nil) ⇒ Document
constructor
A new instance of Document.
- #layout ⇒ Object
- #render(context = {}, content = '') ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ Document
Returns a new instance of Document.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/dimples/document.rb', line 9 def initialize(path = nil) @path = path @metadata = {} if @path @contents = File.read(path) if matches = contents.match(FRONT_MATTER_PATTERN) @metadata = YAML.load(matches[1], symbolize_names: true) @contents = matches.post_match.strip end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (private)
62 63 64 65 66 67 68 |
# File 'lib/dimples/document.rb', line 62 def method_missing(method_name, *args, &block) if @metadata.key?(method_name) @metadata[method_name] else nil end end |
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
7 8 9 |
# File 'lib/dimples/document.rb', line 7 def contents @contents end |
#metadata ⇒ Object
Returns the value of attribute metadata.
7 8 9 |
# File 'lib/dimples/document.rb', line 7 def @metadata end |
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/dimples/document.rb', line 7 def path @path end |
#rendered_contents ⇒ Object
Returns the value of attribute rendered_contents.
7 8 9 |
# File 'lib/dimples/document.rb', line 7 def rendered_contents @rendered_contents end |
Instance Method Details
#basename ⇒ Object
27 28 29 |
# File 'lib/dimples/document.rb', line 27 def basename @metadata.fetch(:filename, 'index') end |
#extension ⇒ Object
31 32 33 |
# File 'lib/dimples/document.rb', line 31 def extension @metadata.fetch(:extension, 'html') end |
#filename ⇒ Object
23 24 25 |
# File 'lib/dimples/document.rb', line 23 def filename "#{basename}.#{extension}" end |
#layout ⇒ Object
35 36 37 |
# File 'lib/dimples/document.rb', line 35 def layout @metadata.fetch(:layout, nil) end |
#render(context = {}, content = '') ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/dimples/document.rb', line 39 def render(context = {}, content = '') context_obj = Object.new context.each do |key, value| context_obj.instance_variable_set("@#{key}", value) end @rendered_contents = renderer.render(context_obj) { content } end |