File: history.rb

package info (click to toggle)
ruby-ramaze 2012.12.08-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,060 kB
  • ctags: 1,200
  • sloc: ruby: 10,446; makefile: 8
file content (38 lines) | stat: -rw-r--r-- 698 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'ramaze/gestalt'

class History
  def initialize(size = 13)
    @size = size
    @history = []
  end

  def write(nick, text)
    text.strip!
    return if text.empty?
    @history.shift until @history.size < @size
    @history << Message.new(nick, text, Time.now)
    true
  end

  def to_html
    g = Ramaze::Gestalt.new

    each do |message|
      g.div(:class => :message) do
        g.span(:class => :time){ message[:time].strftime('%X') }
        g.span(:class => :nick){ message[:nick] }
        g.span(:class => :text){ message[:text] }
      end
    end

    g.to_s
  end

  include Enumerable

  def each
    @history.sort.each do |message|
      yield message
    end
  end
end