Class: Kotoshu::Readers::CompoundRule
- Inherits:
-
Object
- Object
- Kotoshu::Readers::CompoundRule
- Defined in:
- lib/kotoshu/readers/aff_data.rb
Overview
Compound rule pattern.
Instance Attribute Summary collapse
-
#flags ⇒ Set<String>
Flags in this rule.
-
#re ⇒ Regexp
Compiled regex for full matching.
-
#text ⇒ String
The rule text.
Instance Method Summary collapse
-
#fullmatch(flag_sets) ⇒ Boolean
Check if flag sets fully match this rule.
-
#initialize(text) ⇒ CompoundRule
constructor
Create a new compound rule.
Constructor Details
#initialize(text) ⇒ CompoundRule
Create a new compound rule.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/kotoshu/readers/aff_data.rb', line 207 def initialize(text) @text = text # Parse flags from rule text if text.include?('(') @flags = text.scan(/\((.+?)\)/).flatten.to_set parts = text.scan(/\([^*?]+?\)[*?]?/) else @flags = text.gsub(/[*?]/, '').chars.to_set # Handle ) as a flag character (used in sv dictionaries) parts = text.gsub(/(?<=[^*?])\)/, '\\)').gsub(/([^*?])/, '\1') .scan(/[^*?][*?]?/) end @re = Regexp.new(parts.join) end |
Instance Attribute Details
#flags ⇒ Set<String>
Flags in this rule
201 202 203 |
# File 'lib/kotoshu/readers/aff_data.rb', line 201 def flags @flags end |
#re ⇒ Regexp
Compiled regex for full matching
201 202 203 |
# File 'lib/kotoshu/readers/aff_data.rb', line 201 def re @re end |
#text ⇒ String
The rule text
201 202 203 |
# File 'lib/kotoshu/readers/aff_data.rb', line 201 def text @text end |
Instance Method Details
#fullmatch(flag_sets) ⇒ Boolean
Check if flag sets fully match this rule.
227 228 229 230 231 232 233 |
# File 'lib/kotoshu/readers/aff_data.rb', line 227 def fullmatch(flag_sets) relevant_flags = flag_sets.map { |f| @flags.intersection(f).to_a } # Try all combinations of relevant flags relevant_flags[0].product(*relevant_flags[1..]).any? do |fc| @re.match?(fc.join) end end |