Class: Weechat::Line

Inherits:
Object show all
Defined in:
lib/weechat/line.rb

Overview

This class encapsulates lines like they’re printed in WeeChat.

A line usually consists of a prefix (doesn’t have to) and a text. One can access both parts individually, or call methods on both combined, which means that the method will be first called on the prefix and then on the text part.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, message, details = {}) ⇒ Line

Returns a new instance of Line.



44
45
46
# File 'lib/weechat/line.rb', line 44

def initialize(prefix, message, details = {})
  @prefix, @message, @details = prefix, message, details
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/weechat/line.rb', line 56

def method_missing(m, *args)
  rets = []
  [prefix, message].each do |var|
    rets << var.__send__(m, *args) rescue var
  end
  [rets[0].empty? ? nil : rets[0], rets[1]].compact.join("\t")
end

Instance Attribute Details

#detailsObject (readonly)

Returns the value of attribute details.



43
44
45
# File 'lib/weechat/line.rb', line 43

def details
  @details
end

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#prefixObject

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

Class Method Details

.from_hash(h) ⇒ Object



29
30
31
32
33
# File 'lib/weechat/line.rb', line 29

def from_hash(h)
  prefix  = h.delete(:prefix)
  message = h.delete(:message)
  new(prefix, message, h)
end

.parse(line) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/weechat/line.rb', line 10

def parse(line)
  parts = line.split("\t")
  case parts.size
  when 0
    parts = ["", ""]
  when 1
    if line =~ /\t(\t+)\Z/
      parts << $1
    else
      parts.unshift ""
    end
  when 2
  else
    parts = [parts[0], parts[1..-1].join("\t")]
  end

  new(*parts)
end

Instance Method Details

#join(delimiter) ⇒ Object



52
53
54
# File 'lib/weechat/line.rb', line 52

def join(delimiter)
  [prefix.empty? ? nil : prefix, message].compact.join(delimiter)
end

#to_sObject



48
49
50
# File 'lib/weechat/line.rb', line 48

def to_s
  join("\t")
end