Class: RegexpExamples::BaseRepeater
- Inherits:
-
Object
- Object
- RegexpExamples::BaseRepeater
- Defined in:
- lib/regexp-examples/repeaters.rb
Overview
An abstract base class for all other repeater groups. Since all repeaters (quantifiers) are really just shorthand syntaxes for the generic: ‘/.a,b/`, the methods for generating “between `a` and `b` results” are fully generalised here.
Direct Known Subclasses
OneTimeRepeater, PlusRepeater, QuestionMarkRepeater, RangeRepeater, StarRepeater
Instance Attribute Summary collapse
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#max_repeats ⇒ Object
readonly
Returns the value of attribute max_repeats.
-
#min_repeats ⇒ Object
readonly
Returns the value of attribute min_repeats.
Instance Method Summary collapse
-
#initialize(group) ⇒ BaseRepeater
constructor
A new instance of BaseRepeater.
- #random_result ⇒ Object
- #result ⇒ Object
Constructor Details
#initialize(group) ⇒ BaseRepeater
Returns a new instance of BaseRepeater.
8 9 10 |
# File 'lib/regexp-examples/repeaters.rb', line 8 def initialize(group) @group = group end |
Instance Attribute Details
#group ⇒ Object (readonly)
Returns the value of attribute group.
7 8 9 |
# File 'lib/regexp-examples/repeaters.rb', line 7 def group @group end |
#max_repeats ⇒ Object (readonly)
Returns the value of attribute max_repeats.
7 8 9 |
# File 'lib/regexp-examples/repeaters.rb', line 7 def max_repeats @max_repeats end |
#min_repeats ⇒ Object (readonly)
Returns the value of attribute min_repeats.
7 8 9 |
# File 'lib/regexp-examples/repeaters.rb', line 7 def min_repeats @min_repeats end |
Instance Method Details
#random_result ⇒ Object
29 30 31 32 33 34 |
# File 'lib/regexp-examples/repeaters.rb', line 29 def random_result result = [] rand(min_repeats..max_repeats).times { result << group.random_result } result << [GroupResult.new('')] if result.empty? # in case of 0.times RegexpExamples.permutations_of_strings(result) end |
#result ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/regexp-examples/repeaters.rb', line 12 def result group_results = group.result.first(RegexpExamples::Config.max_group_results) results = [] max_results_limiter = MaxResultsLimiterBySum.new min_repeats.upto(max_repeats) do |repeats| result = if repeats.zero? [GroupResult.new('')] else RegexpExamples.permutations_of_strings( [group_results] * repeats ) end results << max_results_limiter.limit_results(result) end results.flatten.uniq end |