Class: Cinch::Pattern Private

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch/pattern.rb

Overview

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

Since:

  • 1.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, pattern, suffix) ⇒ Pattern

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.

Returns a new instance of Pattern.

Since:

  • 1.1.0


46
47
48
# File 'lib/cinch/pattern.rb', line 46

def initialize(prefix, pattern, suffix)
  @prefix, @pattern, @suffix = prefix, pattern, suffix
end

Instance Attribute Details

#patternObject (readonly)

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.

Since:

  • 1.1.0


45
46
47
# File 'lib/cinch/pattern.rb', line 45

def pattern
  @pattern
end

#prefixObject (readonly)

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.

Since:

  • 1.1.0


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

def prefix
  @prefix
end

#suffixObject (readonly)

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.

Since:

  • 1.1.0


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

def suffix
  @suffix
end

Class Method Details

.generate(type, argument) ⇒ 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.

Since:

  • 1.1.0


34
35
36
37
38
39
40
41
# File 'lib/cinch/pattern.rb', line 34

def self.generate(type, argument)
  case type
  when :ctcp
    Pattern.new(/^/, /#{Regexp.escape(argument.to_s)}(?:$| .+)/, nil)
  else
    raise ArgumentError, "Unsupported type: #{type.inspect}"
  end
end

.obj_to_r(obj, anchor = nil) ⇒ Regexp?

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.

Parameters:

  • obj (String, Regexp, NilClass, Proc, #to_s)

    The object to convert to a regexp

Returns:

  • (Regexp, nil)

Since:

  • 1.1.0


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cinch/pattern.rb', line 9

def self.obj_to_r(obj, anchor = nil)
  case obj
  when Regexp, NilClass
    return obj
  else
    escaped = Regexp.escape(obj.to_s)
    case anchor
    when :start
      return Regexp.new("^" + escaped)
    when :end
      return Regexp.new(escaped + "$")
    when nil
      return Regexp.new(Regexp.escape(obj.to_s))
    end
  end
end

.resolve_proc(obj, msg = nil) ⇒ 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.

Since:

  • 1.1.0


26
27
28
29
30
31
32
# File 'lib/cinch/pattern.rb', line 26

def self.resolve_proc(obj, msg = nil)
  if obj.is_a?(Proc)
    return resolve_proc(obj.call(msg), msg)
  else
    return obj
  end
end

Instance Method Details

#to_r(msg = nil) ⇒ 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.

Since:

  • 1.1.0


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cinch/pattern.rb', line 50

def to_r(msg = nil)
  pattern = Pattern.resolve_proc(@pattern, msg)

  case pattern
  when Regexp, NilClass
    prefix  = Pattern.obj_to_r(Pattern.resolve_proc(@prefix, msg), :start)
    suffix  = Pattern.obj_to_r(Pattern.resolve_proc(@suffix, msg), :end)
    /#{prefix}#{pattern}#{suffix}/
  else
    prefix  = Pattern.obj_to_r(Pattern.resolve_proc(@prefix, msg))
    suffix  = Pattern.obj_to_r(Pattern.resolve_proc(@suffix, msg))
    /^#{prefix}#{Pattern.obj_to_r(pattern)}#{suffix}$/
  end
end