Sha256: 2b81e1c1c581ebf857d816ef2f44c20a1fbaf604d8178567848fd0e590bf0be6

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

class Usher
  # Array of delimiters with convenience methods.
  class Delimiters < Array

    attr_reader :unescaped
    
    # Creates a list of delimiters
    # @param arr [Array<String>] delimters to use
    def initialize(ary)
      super ary
      @unescaped = self.map do |delimiter|
        (delimiter[0] == ?\\) ? delimiter[1..-1] : delimiter
      end
    end
  
    # Finds the first occurrance of a delimiter in an array
    # @param array [Array<String>] Array to search through
    # @return [nil, String] The delimiter matched, or nil if none was found.
    def first_in(array)
      array.find { |e| e if unescaped.any? { |delimiter| delimiter == e } }
    end

    # The regular expression to find the delimiters.
    # @return [Regexp] The regular expression
    def regexp
      @regexp ||= Regexp.new("(#{unescaped.collect{|d| Regexp.quote(d)}.join('|')})")
    end
    
    # The regular expression expressed as a character class.
    # @return [String] The regular expression as a string.
    def regexp_char_class
      @regexp_char_class ||= collect{|d| Regexp.quote(d)}.join
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
usher-0.7.4 lib/usher/delimiters.rb
usher-0.7.3 lib/usher/delimiters.rb
usher-0.7.2 lib/usher/delimiters.rb