Module: Marvin::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/marvin/util.rb

Constant Summary collapse

GLOB_PATTERN_MAP =
{
  '*' => '.*',
  '?' => '.',
  '[' => '[',
  ']' => ']'
}

Instance Method Summary collapse

Instance Method Details

#arguments(input) ⇒ Object



20
21
22
23
24
25
# File 'lib/marvin/util.rb', line 20

def arguments(input)
  prefix, ending = input.split(":", 2)
  prefix = prefix.split(" ")
  prefix << ending unless ending.blank?
  return prefix
end

#channel_name(name) ⇒ Object Also known as: chan

Return the channel-name version of a string by appending “#” to the front if it doesn’t already start with it.



14
15
16
17
# File 'lib/marvin/util.rb', line 14

def channel_name(name)
  name = name.to_s
  name =~ /^\#/ ? name : "##{name}"
end

#glob2pattern(glob_string) ⇒ Object

Converts a glob-like pattern into a regular expression for easy / fast matching. Code is from PLEAC at pleac.sourceforge.net/pleac_ruby/patternmatching.html



44
45
46
47
48
49
# File 'lib/marvin/util.rb', line 44

def glob2pattern(glob_string)
    inner_pattern = glob_string.gsub(/(.)/) do |c|
      GLOB_PATTERN_MAP[c] || Regexp::escape(c)
    end
  return Regexp.new("^#{inner_pattern}$")
end

#last_param(section, ignore_prefix = true) ⇒ Object Also known as: lp

Specifies the last parameter of a response, used to specify parameters which have spaces etc (for example, the actual message part of a response).



30
31
32
33
34
35
36
37
38
# File 'lib/marvin/util.rb', line 30

def last_param(section, ignore_prefix = true)
  content = section.to_s.strip
  return if content.blank?
  if content =~ /\s+/ && (ignore_prefix || content !~ /^:/)
    ":#{content}"
  else
    content
  end
end