Class: TamTam::MessageSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tam_tam/message_set.rb

Overview

A collection of messages. Responsible for filtering and manipulating messages.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ MessageSet

Returns a new instance of MessageSet.



12
13
14
# File 'lib/tam_tam/message_set.rb', line 12

def initialize(data)
  self.data = data.map { |data| Message.new(data) }
end

Instance Attribute Details

#data=(value) ⇒ Object

Sets the attribute data

Parameters:

  • value

    the value to set the attribute data to.



9
10
11
# File 'lib/tam_tam/message_set.rb', line 9

def data=(value)
  @data = value
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
# File 'lib/tam_tam/message_set.rb', line 45

def ==(other)
  data == other.data
end

#by_countObject



34
35
36
37
38
39
# File 'lib/tam_tam/message_set.rb', line 34

def by_count
  Hash[group_by { |message| message.text }.inject({}) { |counts, groups|
    counts[groups[0]] = groups[1].size
    counts
  }.sort_by { |key, value| -value }]
end

#eachObject



41
42
43
# File 'lib/tam_tam/message_set.rb', line 41

def each
  data.each { |message| yield message }
end

#including(phrase) ⇒ Object



16
17
18
19
20
# File 'lib/tam_tam/message_set.rb', line 16

def including(phrase)
  copy = dup
  copy.data = data.select { |message| message.text.include?(phrase) }
  copy
end

#matching(pattern) ⇒ Object



22
23
24
25
26
# File 'lib/tam_tam/message_set.rb', line 22

def matching(pattern)
  copy = dup
  copy.data = data.select { |message| message.text.match(pattern) }
  copy
end

#sent_by(sender) ⇒ Object



28
29
30
31
32
# File 'lib/tam_tam/message_set.rb', line 28

def sent_by(sender)
  copy = dup
  copy.data = data.select { |message| message.sender == sender }
  copy
end

#sizeObject Also known as: length



54
55
56
# File 'lib/tam_tam/message_set.rb', line 54

def size
  data.size
end

#to_sObject Also known as: inspect



49
50
51
# File 'lib/tam_tam/message_set.rb', line 49

def to_s
  %{#<#{self.class.name}:0x#{object_id.to_s(16)} size=#{size}>}
end