Class: Doculab::Doc

Inherits:
Object
  • Object
show all
Defined in:
app/models/doculab/doc.rb

Defined Under Namespace

Classes: FileNotFound

Constant Summary collapse

@@valid_attributes =
[:permalink, :file, :title, :content, :section]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Doc

Returns a new instance of Doc.



10
11
12
13
14
15
# File 'app/models/doculab/doc.rb', line 10

def initialize(attributes = {})
  attributes.assert_valid_keys(@@valid_attributes)
  attributes.each do |field, value|
    send("#{field}=", value)
  end
end

Class Method Details

.directoryObject



33
34
35
36
# File 'app/models/doculab/doc.rb', line 33

def self.directory
  @@directory ||= Rails.root.join('doculab', 'docs')
  Pathname.new(@@directory)
end

.filenamesObject

Returns an array of filenames for all files in the docs directory



39
40
41
# File 'app/models/doculab/doc.rb', line 39

def self.filenames
  filepaths.collect {|fn| File.basename(fn) }
end

.filepathsObject



43
44
45
# File 'app/models/doculab/doc.rb', line 43

def self.filepaths
  Dir.glob(directory.join("*.{#{extensions.join(',')}}"))
end

.find(permalink) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'app/models/doculab/doc.rb', line 22

def self.find(permalink)
  file = file_for_permalink(permalink)

  unless file && File.exist?(file)
    raise FileNotFound, "No file found for '#{permalink}'"
  end

  content = File.read(file)
  self.new(:permalink => permalink, :file => file, :content => content)
end

Instance Method Details

#renderObject



17
18
19
20
# File 'app/models/doculab/doc.rb', line 17

def render
  template = ::Tilt.new(file)
  template.render(self)
end