Class: PatternParser
- Inherits:
-
Object
- Object
- PatternParser
- Defined in:
- lib/pattern_expander/pattern_parser.rb
Constant Summary collapse
- DEFAULT_SUBSTITUTES =
{ "+w" => ('a'..'z').to_a + ('0'..'9').to_a, "+d" => ('0'..'9').to_a, }
Instance Attribute Summary collapse
-
#delimiter ⇒ Object
readonly
Returns the value of attribute delimiter.
-
#substitutes ⇒ Object
readonly
Returns the value of attribute substitutes.
Instance Method Summary collapse
- #_parse_groups(string) ⇒ Object
- #_substitute(groups) ⇒ Object
-
#initialize(substitutes: DEFAULT_SUBSTITUTES) ⇒ PatternParser
constructor
A new instance of PatternParser.
- #parse(string) ⇒ Object
Constructor Details
#initialize(substitutes: DEFAULT_SUBSTITUTES) ⇒ PatternParser
Returns a new instance of PatternParser.
9 10 11 12 |
# File 'lib/pattern_expander/pattern_parser.rb', line 9 def initialize(substitutes: DEFAULT_SUBSTITUTES) @substitutes = substitutes @delimiter = '|' end |
Instance Attribute Details
#delimiter ⇒ Object (readonly)
Returns the value of attribute delimiter.
2 3 4 |
# File 'lib/pattern_expander/pattern_parser.rb', line 2 def delimiter @delimiter end |
#substitutes ⇒ Object (readonly)
Returns the value of attribute substitutes.
2 3 4 |
# File 'lib/pattern_expander/pattern_parser.rb', line 2 def substitutes @substitutes end |
Instance Method Details
#_parse_groups(string) ⇒ Object
21 22 23 24 25 |
# File 'lib/pattern_expander/pattern_parser.rb', line 21 def _parse_groups(string) string.scan( /([^\[\]]+|[\#{delimiter}]+)/ ).flatten end |
#_substitute(groups) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/pattern_expander/pattern_parser.rb', line 27 def _substitute(groups) groups.map do |group| group.inject([]) do |memo, item| memo += substitutes[item] || [item] end end end |
#parse(string) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/pattern_expander/pattern_parser.rb', line 14 def parse(string) parsed_groups = _parse_groups(string).map do |group| group.split(delimiter) end _substitute(parsed_groups) end |