Sha256: af9b32183fb4df8723affde1c759a034d2424ab3591fad4bcb323de436f6d35d

Contents?: true

Size: 688 Bytes

Versions: 111

Compression:

Stored size: 688 Bytes

Contents

# frozen_string_literal: true

module EacRubyUtils
  class RegexpParser
    attr_reader :pattern, :builder_proc

    def initialize(pattern, &builder_proc)
      @pattern = pattern
      @builder_proc = builder_proc
    end

    def parse(string)
      internal_parse(string)[1]
    end

    def parse!(string)
      match, result = internal_parse(string)
      return result if match

      raise ::ArgumentError, "String \"#{string}\" does not match pattern \"#{pattern}\""
    end

    private

    def internal_parse(string)
      m = pattern.match(string)
      if m
        [true, builder_proc ? builder_proc.call(m) : m]
      else
        [false, nil]
      end
    end
  end
end

Version data entries

111 entries across 111 versions & 4 rubygems

Version Path
eac_ruby_utils-0.96.1 lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.12.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.11.1 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.11.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.96.0 lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.10.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.9.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.95.3 lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.8.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.7.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.6.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.5.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.95.2 lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.4.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
avm-tools-0.120.2 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
avm-tools-0.120.1 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
avm-tools-0.120.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
avm-tools-0.119.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
avm-tools-0.118.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
avm-tools-0.117.3 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb