Class: Dimples::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/dimples/document.rb

Direct Known Subclasses

Page, Post, Template

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Document

Returns a new instance of Document.



7
8
9
10
11
12
13
14
15
16
# File 'lib/dimples/document.rb', line 7

def initialize(path = nil)
  @path = path

  if @path
    @metadata, @contents = Dimples::FrontMatter.parse(File.read(path))
  else
    @metadata = {}
    @contents = ''
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



57
58
59
60
61
62
63
# File 'lib/dimples/document.rb', line 57

def method_missing(method_name, *args, &block)
  if @metadata.key?(method_name)
    @metadata[method_name]
  else
    nil
  end
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



5
6
7
# File 'lib/dimples/document.rb', line 5

def contents
  @contents
end

#metadataObject

Returns the value of attribute metadata.



5
6
7
# File 'lib/dimples/document.rb', line 5

def 
  @metadata
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/dimples/document.rb', line 5

def path
  @path
end

#rendered_contentsObject

Returns the value of attribute rendered_contents.



5
6
7
# File 'lib/dimples/document.rb', line 5

def rendered_contents
  @rendered_contents
end

Instance Method Details

#basenameObject



22
23
24
# File 'lib/dimples/document.rb', line 22

def basename
  @metadata.fetch(:filename, 'index')
end

#extensionObject



26
27
28
# File 'lib/dimples/document.rb', line 26

def extension
  @metadata.fetch(:extension, 'html')
end

#filenameObject



18
19
20
# File 'lib/dimples/document.rb', line 18

def filename
  "#{basename}.#{extension}"
end

#layoutObject



30
31
32
# File 'lib/dimples/document.rb', line 30

def layout
  @metadata.fetch(:layout, nil)
end

#render(context = {}, content = '') ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/dimples/document.rb', line 34

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