Class: StaticCms::Page

Inherits:
Object
  • Object
show all
Extended by:
RubyPatch::AutoLoad
Defined in:
lib/static_cms/page.rb

Constant Summary collapse

PARAMS_DEFAULT =
{
  'visible' => true,
  'title' => '',
  'template' => 'default',
  'articles' => [],
  'statics' => [],
  'sources' => [],
  'base' => nil,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, name, params = {}) ⇒ Page

Returns a new instance of Page.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/static_cms/page.rb', line 20

def initialize(site, name, params = {})
  @site = site
  @name = name
  @dir = File.join("root", @name)
  params = PARAMS_DEFAULT.merge(params)
  @template = params.delete('template')
  @template_file = File.join('templates', @template) + '.html.haml'
  @articles = params.delete('articles').map{|name| ::StaticCms::Article.use(self, name)}
  @statics = params.delete('statics').map{|file| File.join('statics', file)}
  @sources = params.delete('sources').map{|file| File.join('sources', file)}
  @base = params.delete('base') || @site.www
  params.each{|k, v|
    instance_variable_set("@#{k}", v)
  }
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



17
18
19
# File 'lib/static_cms/page.rb', line 17

def dir
  @dir
end

#nameObject

Returns the value of attribute name.



17
18
19
# File 'lib/static_cms/page.rb', line 17

def name
  @name
end

#siteObject

Returns the value of attribute site.



17
18
19
# File 'lib/static_cms/page.rb', line 17

def site
  @site
end

#template_fileObject

Returns the value of attribute template_file.



17
18
19
# File 'lib/static_cms/page.rb', line 17

def template_file
  @template_file
end

Instance Method Details

#generateObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/static_cms/page.rb', line 36

def generate
  FileUtils.mkdir_p(@dir)
  FileUtils.chmod(0755, @dir)

  if @visible
    @statics.each{|file| cp_if_new(file)}
    @sources.each{|file| compile(file)}
    template_file = @template_file
  else
    @statics.each{|file| FileUtils.rm_rf(static_target(file))}
    @sources.each{|file| FileUtils.rm_rf(compile_target(file))}
    template_file = 'templates/moved.html.haml'
  end

  html = ::Haml::Engine.new(File.read(template_file)).render(self)
  target = File.join(@dir, 'index.html')
  open(target, 'w'){|io|
    io.write(html)
    io.flush
    io.fsync
  }
end