Sha256: a8f73ba4b892e6253439b24cfa18c1ea5ebea3904fa2b95c236146f508d81470

Contents?: true

Size: 779 Bytes

Versions: 133

Compression:

Stored size: 779 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

    # @return [Boolean]
    def parse?(string)
      internal_parse(string).first
    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

133 entries across 133 versions & 2 rubygems

Version Path
eac_ruby_utils-0.123.0 lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.121.0 lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.120.0 lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.76.1 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.76.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.75.2 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.119.2 lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.75.1 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.75.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.119.1 lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.74.1 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.74.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.73.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.119.0 lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.72.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.118.1 lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.118.0 lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.70.1 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.70.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.69.1 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb