Sha256: b7f8221f20d1cf985a755b5f1bcbd1e9e067e6c0f4f9d5b66c0f4ed4341c2578

Contents?: true

Size: 977 Bytes

Versions: 2

Compression:

Stored size: 977 Bytes

Contents

class LooseTightDictionary
  module ExtractRegexp #:nodoc: all
    def extract_regexp(regexp_or_str)
      case regexp_or_str
      when ::Regexp
        regexp_or_str
      when ::String
        regexp_from_string regexp_or_str
      else
        raise ::ArgumentError, "Expected regexp or string"
      end
    end
    
    REGEXP_DELIMITERS = {
      '%r{' => '}',
      '/' => '/'
    }
    def regexp_from_string(str)
      delim_start, delim_end = REGEXP_DELIMITERS.detect { |k, v| str.start_with? k }.map { |delim| ::Regexp.escape delim }
      %r{\A#{delim_start}(.*)#{delim_end}([^#{delim_end}]*)\z} =~ str.strip
      content = $1
      options = $2
      content.gsub! '\\/', '/'
      ignore_case = options.include?('i') ? ::Regexp::IGNORECASE : nil
      multiline = options.include?('m') ? ::Regexp::MULTILINE : nil
      extended = options.include?('x') ? ::Regexp::EXTENDED : nil
      ::Regexp.new content, (ignore_case||multiline||extended)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
loose_tight_dictionary-0.1.1 lib/loose_tight_dictionary/extract_regexp.rb
loose_tight_dictionary-0.1.0 lib/loose_tight_dictionary/extract_regexp.rb