Class: Plato::Document

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

Constant Summary collapse

RAW_TEXT =
%w(text txt raw)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category, attributes) ⇒ Document

Returns a new instance of Document.



8
9
10
11
# File 'lib/plato/document.rb', line 8

def initialize(category, attributes)
  @category = category
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(attr) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/plato/document.rb', line 52

def method_missing(attr)
  if date and date.respond_to? attr
    date.send(attr)
  else
    attributes[attr.to_s] or super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/plato/document.rb', line 6

def attributes
  @attributes
end

#categoryObject (readonly)

Returns the value of attribute category.



6
7
8
# File 'lib/plato/document.rb', line 6

def category
  @category
end

Instance Method Details

#body(context = nil) ⇒ Object



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

def body(context = nil)
  if RAW_TEXT.include? format
    attributes["body"]
  else
    @template ||= Tilt.new(format) { attributes["body"] }
    @template.render(context)
  end
end

#dateObject



29
30
31
# File 'lib/plato/document.rb', line 29

def date
  @date ||= (attributes["date"] ? Time.parse(attributes["date"]) : nil)
end

#formatObject



21
22
23
# File 'lib/plato/document.rb', line 21

def format
  attributes["format"] || 'text'
end

#next_documentObject



17
18
19
# File 'lib/plato/document.rb', line 17

def next_document
  category.documents.next(self)
end

#pathObject



25
26
27
# File 'lib/plato/document.rb', line 25

def path
  category.dest_path(self)
end

#previous_documentObject



13
14
15
# File 'lib/plato/document.rb', line 13

def previous_document
  category.documents.prev(self)
end

#respond_to?(attr) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/plato/document.rb', line 48

def respond_to?(attr)
  attributes.has_key? attr.to_s or super
end

#values_at(*keys) ⇒ Object



44
45
46
# File 'lib/plato/document.rb', line 44

def values_at(*keys)
  keys.map {|k| send(k) }
end