class MatchLength
Attributes
Public Class Methods
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 11 def initialize(exp, opts = {}) self.exp_class = exp.class self.min_rep = exp.repetitions.min self.max_rep = exp.repetitions.max if (base = opts[:base]) self.base_min = base self.base_max = base self.reify = ->{ '.' * base } else self.base_min = opts.fetch(:base_min) self.base_max = opts.fetch(:base_max) self.reify = opts.fetch(:reify) end end
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 6 def self.of(obj) exp = obj.is_a?(Regexp::Expression::Base) ? obj : Regexp::Parser.parse(obj) exp.match_length end
Public Instance Methods
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 26 def each(opts = {}) return enum_for(__method__, opts) unless block_given? limit = opts[:limit] || 1000 yielded = 0 (min..max).each do |num| next unless include?(num) yield(num) break if (yielded += 1) >= limit end end
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 37 def endless_each return enum_for(__method__) unless block_given? (min..max).each { |num| yield(num) if include?(num) } end
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 46 def fixed? min == max end
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 42 def include?(length) test_regexp.match?('X' * length) end
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 62 def inspect type = exp_class.name.sub('Regexp::Expression::', '') "#<#{self.class}<#{type}> min=#{min} max=#{max}>" end
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 54 def max max_rep * base_max end
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 50 def min min_rep * base_min end
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 58 def minmax [min, max] end
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 67 def to_re /(?:#{reify.call}){#{min_rep},#{max_rep unless max_rep == Float::INFINITY}}/ end
Private Instance Methods
Source
# File lib/regexp_parser/expression/methods/match_length.rb, line 76 def test_regexp @test_regexp ||= /^#{to_re}$/ end