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 = {}) ⇒ File

Returns a new instance of File.



11
12
13
14
# File 'lib/file.rb', line 11

def initialize(name = 'index', partials = {})
  @name = name
  @partials = partials
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#partial(template) ⇒ Object



33
34
35
36
37
38
# File 'lib/file.rb', line 33

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

#render(template = nil) ⇒ Object



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

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

#write(directory) ⇒ Object



16
17
18
19
20
21
# File 'lib/file.rb', line 16

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