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_tools-0.22.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.21.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.20.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.19.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.18.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.17.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.16.1 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.98.0 lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.16.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.15.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.14.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb
eac_ruby_utils-0.97.0 lib/eac_ruby_utils/regexp_parser.rb
eac_tools-0.13.0 sub/eac_ruby_utils/lib/eac_ruby_utils/regexp_parser.rb