Class: Rtlog::Page

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/rtlog/pages.rb

Direct Known Subclasses

DayPage, IndexPage, MonthPage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, log) ⇒ Page

Returns a new instance of Page.



14
15
16
17
# File 'lib/rtlog/pages.rb', line 14

def initialize config, log
  @config = config
  @log    = log
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/rtlog/pages.rb', line 10

def config
  @config
end

#logObject (readonly)

Returns the value of attribute log.



11
12
13
# File 'lib/rtlog/pages.rb', line 11

def log
  @log
end

#loggerObject



19
20
21
# File 'lib/rtlog/pages.rb', line 19

def logger
  defined?(@logger) ? @logger : Rtlog.logger
end

Instance Method Details

#config_dirObject



23
24
25
# File 'lib/rtlog/pages.rb', line 23

def config_dir
  File.expand_path( config['config_dir'] )
end

#generate(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rtlog/pages.rb', line 34

def generate options={}
  options = { :layout => 'layout.html.erb' }.merge(options)
  
  template = nil
  if options[:layout]
    template = File.join( config_dir, options[:layout] ) 
    template = File.join( Rtlog.root, 'lib', 'rtlog', 'example', 'config',  'layout.html.erb' ) unless File.exist?(template)
  end
  template = template_path unless template
  FileUtils.mkdir_p( File.dirname(file_path) ) unless File.exist?( File.dirname(file_path) )
  open(file_path, "w") do |io|  
    io.write(parse(template))
  end
  logger.debug("#{self.class} is generated: #{file_path}")
end

#index_urlObject



86
87
88
# File 'lib/rtlog/pages.rb', line 86

def index_url
  config['url_prefix']
end

#month_pagesObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rtlog/pages.rb', line 74

def month_pages
  unless defined?(@month_pages)
    @month_pages = []
    log.year_entries.each do |y|
      y.month_entries.each do |m|
        @month_pages << MonthPage.new(config, log, m)
      end
    end
  end
  @month_pages
end

#parse(file_name) ⇒ Object



27
28
29
30
31
32
# File 'lib/rtlog/pages.rb', line 27

def parse file_name
  # logger.debug("Template parsing: #{file_name}")
  open(file_name) { |io| ERB.new( io.read ) }.result(binding)
rescue => ex
  "<p>#{h(ex.to_s)}</p><pre class='error'>#{ex.backtrace.map{|m| h(m) }.join('<br />')}</pre>"
end

#sizeObject



50
51
52
53
54
# File 'lib/rtlog/pages.rb', line 50

def size
  config['pages'][page_name]['size'] || 7
rescue
  7
end

#template_file_nameObject



69
70
71
72
# File 'lib/rtlog/pages.rb', line 69

def template_file_name
  class_name = self.class.name.split('::').last.gsub('Page', '').downcase
  "#{class_name}.html.erb"
end

#template_pathObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rtlog/pages.rb', line 56

def template_path
  unless defined?(@template_path)
    @template_path = nil
    if config['pages'] && config['pages'][page_name] && config['pages'][page_name]['template']
      @template_path = ::File.join(config_dir, config['pages'][page_name]['template'])
    end
    unless @template_path && File.exist?(@template_path)
      @template_path = File.join( Rtlog.root, 'lib', 'rtlog', 'example', 'config', template_file_name )
    end
  end
  @template_path
end