Module: ToRegex::StringMixin
- Included in:
- String
- Defined in:
- lib/wayback_machine_downloader/to_regex.rb
Constant Summary collapse
- INLINE_OPTIONS =
/[imxnesu]*/i.freeze
- REGEXP_DELIMITERS =
{ '%r{' => '}'.freeze, '/' => '/'.freeze }.freeze
- REGEX_FLAGS =
{ ignore_case: Regexp::IGNORECASE, multiline: Regexp::MULTILINE, extended: Regexp::EXTENDED }.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#as_regexp(options = {}) ⇒ Object
Return arguments that can be passed to ‘Regexp.new`.
-
#to_regex(options = {}) ⇒ Object
Get a regex back.
Class Method Details
.literal?(str) ⇒ Boolean
18 19 20 |
# File 'lib/wayback_machine_downloader/to_regex.rb', line 18 def literal?(str) REGEXP_DELIMITERS.none? { |start, ending| str.start_with?(start) && str.match?(/#{ending}#{INLINE_OPTIONS}\z/) } end |
Instance Method Details
#as_regexp(options = {}) ⇒ Object
Return arguments that can be passed to ‘Regexp.new`
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/wayback_machine_downloader/to_regex.rb', line 40 def as_regexp( = {}) raise ArgumentError, '[to_regexp] Options must be a Hash' unless .is_a?(Hash) str = self return if [:detect] && str.empty? if should_treat_as_literal?(str, ) content = Regexp.escape(str) elsif (delim_set = extract_delimiters(str)) content, = parse_regexp_string(str, delim_set, ) return unless content else return end build_regexp_args(content, ) end |
#to_regex(options = {}) ⇒ Object
Get a regex back
Without :literal or :detect, ‘“foo”.to_regex` will return nil.
34 35 36 37 |
# File 'lib/wayback_machine_downloader/to_regex.rb', line 34 def to_regex( = {}) args = as_regexp() args ? Regexp.new(*args) : nil end |