Class: Deris::File
- Inherits:
-
Object
- Object
- Deris::File
- Defined in:
- lib/file.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name = 'index', partials = {}, depth = nil) ⇒ File
constructor
A new instance of File.
- #partial(template) ⇒ Object
- #render(template = nil) ⇒ Object
- #url(path) ⇒ Object
- #write(directory) ⇒ Object
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
#name ⇒ Object (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 |