Class: Regexp

Inherits:
Object
  • Object
show all
Defined in:
lib/bblib/core/util/regexp.rb,
lib/bblib/core/util/regexp.rb

Constant Summary collapse

EXTENDED =
2

Class Method Summary collapse

Class Method Details

.from_s(str, *options, ignore_invalid: false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bblib/core/util/regexp.rb', line 25

def self.from_s(str, *options, ignore_invalid: false)
  opt_map = options.map { |o| BBLib::REGEXP_OPTIONS.find { |k, v| o == k || o == k.to_s || v.include?(o) || v.include?(o.to_s.to_sym) }.first }.compact
  return Regexp.new(str, opt_map.inject(0) { |s, x| s += BBLib::REGEXP_MODE_HASH[x] }) if str.encap_by?('(') || !str.start_with?('/')
  str += opt_map.join
  mode = 0
  unless str.end_with?('/')
    str.split('/').last.chars.uniq.each do |l|
      raise ArgumentError, "Invalid Regexp mode: '#{l}'" unless ignore_invalid || BBLib::REGEXP_MODE_HASH[l.to_sym]
      mode += (BBLib::REGEXP_MODE_HASH[l.to_sym] || 0)
    end
    str = str[0..(str.rindex('/') || -1)]
  end
  Regexp.new(str.uncapsulate('/', limit: 1), mode)
end