Class: Deris::File

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'index', partials = {}, depth = nil) ⇒ File

Returns a new instance of File.



7
8
9
10
11
# File 'lib/file.rb', line 7

def initialize(name = 'index', partials = {}, depth = nil)
  @name = name
  @partials = partials
  @depth = depth || 0
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#partial(template) ⇒ Object



31
32
33
34
35
36
# File 'lib/file.rb', line 31

def partial(template)
  if template.is_a? Symbol
    template = @partials[template] || ''
  end
  render(template)
end

#render(template = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/file.rb', line 21

def render(template = nil)
  if template.nil?
    template = @partials[:layout]
  else
    template = ::File.new(template).read if ::File.exist?(template)
  end
  haml_engine = Haml::Engine.new(template)
  haml_engine.render(self)
end

#url(path) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/file.rb', line 38

def url(path)
  sections = []
  @depth.times do
    sections << '..'
  end
  sections << path
  
  sections.join('/')
end

#write(directory) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/file.rb', line 13

def write(directory)
  FileUtils.mkdir(directory) unless ::File.exist? directory
  output_path = ::File.join(directory, "#{@name}.html")
  ::File.open(output_path, 'w+') do |file|
    file.write render
  end
end