Module: Cinch::ModeParser Private

Defined in:
lib/cinch/mode_parser.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.parse_modes(modes, params, param_modes = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cinch/mode_parser.rb', line 4

def self.parse_modes(modes, params, param_modes = {})
  if modes.size == 0
    raise InvalidModeString, 'Empty mode string'
  end

  if modes[0] !~ /[+-]/
    raise InvalidModeString, "Malformed modes string: %s" % modes
  end

  changes = []

  direction = nil
  count = -1

  modes.each_char do |ch|
    if ch =~ /[+-]/
      if count == 0
        raise InvalidModeString, 'Empty mode sequence: %s' % modes
      end

      direction = case ch
                  when "+"
                    :add
                  when "-"
                    :remove
                  end
      count = 0
    else
      param = nil
      if param_modes[direction].include?(ch)
        if params.size > 0
          param = params.shift
        else
          raise InvalidModeString, 'Not enough parameters: %s' % ch.inspect
        end
      end
      changes << [direction, ch, param]
      count += 1
    end
  end

  if params.size > 0
    raise InvalidModeString, 'Too many parameters: %s %s' % [modes, params].inspect
  end

  if count == 0
    raise InvalidModeString, 'Empty mode sequence: %r' % modes
  end

  return changes
end