Class: RSMP::Archive

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

Constant Summary collapse

@@index =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inspect

#inspector

Constructor Details

#initialize(max = 100) ⇒ Archive

Returns a new instance of Archive.


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

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

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.


8
9
10
# File 'lib/rsmp/archive.rb', line 8

def items
  @items
end

Class Method Details

.current_indexObject


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

def self.current_index
  @@index
end

.increase_indexObject


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

def self.increase_index
  @@index += 1
end

.prepare_item(item) ⇒ Object

Raises:

  • (ArgumentError)

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

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

  cleaned = item.select { |k,v| [:author,:level,:ip,:port,:site_id,:component_id,:str,:message,:exception].include? k }
  cleaned[:timestamp] = Clock.now
  if item[:message]
    cleaned[:direction] = item[:message].direction 
    cleaned[:component_id] = item[:message].attributes['cId'] 
  end

  cleaned
end

Instance Method Details

#add(item) ⇒ Object


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

def add item
  item[:index] = RSMP::Archive.increase_index
  @items << item
  if @items.size > @max
    @items.shift
  end
end

#by_level(levels) ⇒ Object


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

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

#inspectObject


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

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

#stringsObject


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

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