Class: Usher::Delimiters

Inherits:
Array
  • Object
show all
Defined in:
lib/usher/delimiters.rb

Overview

Array of delimiters with convenience methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ary) ⇒ Delimiters

Creates a list of delimiters

Parameters:

  • ary (Array<String>)

    delimters to use



9
10
11
12
13
14
# File 'lib/usher/delimiters.rb', line 9

def initialize(ary)
  super ary
  @unescaped = self.map do |delimiter|
    (delimiter[0] == ?\\) ? delimiter[1..-1] : delimiter
  end
end

Instance Attribute Details

#unescapedObject (readonly)

Returns the value of attribute unescaped.



5
6
7
# File 'lib/usher/delimiters.rb', line 5

def unescaped
  @unescaped
end

Instance Method Details

#first_in(array) ⇒ nil, String

Finds the first occurrance of a delimiter in an array

Parameters:

  • array (Array<String>)

    Array to search through

Returns:

  • (nil, String)

    The delimiter matched, or nil if none was found.



19
20
21
# File 'lib/usher/delimiters.rb', line 19

def first_in(array)
  array.find { |e| e if unescaped.any? { |delimiter| delimiter == e } }
end

#regexpRegexp

The regular expression to find the delimiters.

Returns:

  • (Regexp)

    The regular expression



25
26
27
# File 'lib/usher/delimiters.rb', line 25

def regexp
  @regexp ||= Regexp.new("(#{unescaped.collect{|d| Regexp.quote(d)}.join('|')})")
end

#regexp_char_classString

The regular expression expressed as a character class.

Returns:

  • (String)

    The regular expression as a string.



31
32
33
# File 'lib/usher/delimiters.rb', line 31

def regexp_char_class
  @regexp_char_class ||= collect{|d| Regexp.quote(d)}.join
end