Class: RSMP::Archive

Inherits:
Object
  • Object
show all
Defined in:
lib/rsmp/log/archive.rb

Overview

Archive of log items, which can be messages or info items. All items are timestamped, and stored chronologically.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max = 100) ⇒ Archive

Returns a new instance of Archive.



13
14
15
16
# File 'lib/rsmp/log/archive.rb', line 13

def initialize(max = 100)
  @items = []
  @max = max
end

Class Attribute Details

.indexObject

Returns the value of attribute index.



10
11
12
# File 'lib/rsmp/log/archive.rb', line 10

def index
  @index
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



5
6
7
# File 'lib/rsmp/log/archive.rb', line 5

def items
  @items
end

Class Method Details

.current_indexObject



39
40
41
# File 'lib/rsmp/log/archive.rb', line 39

def self.current_index
  index
end

.increase_indexObject



35
36
37
# File 'lib/rsmp/log/archive.rb', line 35

def self.increase_index
  self.index += 1
end

.prepare_item(item) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rsmp/log/archive.rb', line 22

def self.prepare_item(item)
  raise ArgumentError unless item.is_a? Hash

  cleaned = item.slice(:author, :level, :ip, :port, :site_id, :component, :text, :message, :exception)
  cleaned[:timestamp] = Clock.now
  if item[:message]
    cleaned[:direction] = item[:message].direction
    cleaned[:component] = item[:message].attributes['cId']
  end

  cleaned
end

Instance Method Details

#add(item) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/rsmp/log/archive.rb', line 51

def add(item)
  item[:index] = RSMP::Archive.increase_index
  @items << item
  return unless @items.size > @max

  @items.shift
end

#by_level(levels) ⇒ Object



43
44
45
# File 'lib/rsmp/log/archive.rb', line 43

def by_level(levels)
  items.select { |item| levels.include? item[:level] }
end

#inspectObject



18
19
20
# File 'lib/rsmp/log/archive.rb', line 18

def inspect
  "#<#{self.class.name}:#{object_id}, messages:#{@items}>"
end

#stringsObject



47
48
49
# File 'lib/rsmp/log/archive.rb', line 47

def strings
  items.map { |item| item[:str] }
end