Class: Mustermann::Composite
- Defined in:
- lib/mustermann/composite.rb
Overview
Class for pattern objects composed of multiple patterns using binary logic.
Constant Summary
Constants included from Mustermann
Instance Attribute Summary collapse
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Class Method Summary collapse
-
.new(*patterns, **options) ⇒ Mustermann::Pattern
A new composite pattern.
- .supported?(option, **options) ⇒ Boolean
Instance Method Summary collapse
- #==(pattern) ⇒ Object
- #===(string) ⇒ Object
-
#expand(behavior = nil, values = {}) ⇒ String
Expanding is supported by almost all patterns (notable execptions are Shell, Regular and Simple).
-
#initialize(patterns, operator: :|, **options) ⇒ Composite
constructor
A new instance of Composite.
- #match(string) ⇒ Object
- #params(string) ⇒ Object
-
#to_s ⇒ String
The string representation of the pattern.
-
#to_templates ⇒ String
Expanding is supported by almost all patterns (notable execptions are Shell, Regular and Simple).
Methods inherited from Pattern
#=~, #named_captures, #names, #peek, #peek_match, #peek_params, #peek_size, supported_options, #to_proc, #|
Methods included from Mustermann
Constructor Details
#initialize(patterns, operator: :|, **options) ⇒ Composite
Returns a new instance of Composite.
26 27 28 29 |
# File 'lib/mustermann/composite.rb', line 26 def initialize(patterns, operator: :|, **) @operator = operator.to_sym @patterns = patterns.flat_map { |p| patterns_from(p, **) } end |
Instance Attribute Details
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
7 8 9 |
# File 'lib/mustermann/composite.rb', line 7 def operator @operator end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
7 8 9 |
# File 'lib/mustermann/composite.rb', line 7 def patterns @patterns end |
Class Method Details
.new(*patterns, **options) ⇒ Mustermann::Pattern
Returns a new composite pattern.
17 18 19 20 21 22 23 24 |
# File 'lib/mustermann/composite.rb', line 17 def self.new(*patterns, **) patterns = patterns.flatten case patterns.size when 0 then raise ArgumentError, 'cannot create empty composite pattern' when 1 then patterns.first else super(patterns, **) end end |
.supported?(option, **options) ⇒ Boolean
11 12 13 14 |
# File 'lib/mustermann/composite.rb', line 11 def self.supported?(option, **) return true if super [:type] and Mustermann[[:type]].supported?(option, **) end |
Instance Method Details
#==(pattern) ⇒ Object
32 33 34 |
# File 'lib/mustermann/composite.rb', line 32 def ==(pattern) patterns == patterns_from(pattern) end |
#===(string) ⇒ Object
37 38 39 |
# File 'lib/mustermann/composite.rb', line 37 def ===(string) patterns.map { |p| p === string }.inject(operator) end |
#expand(behavior = nil, values = {}) ⇒ String
This method is only implemented by certain subclasses.
Expanding is supported by almost all patterns (notable execptions are Shell, Regular and Simple).
Union Mustermann::Composite patterns (with the | operator) support expanding if all patterns they are composed of also support it.
58 59 60 61 62 |
# File 'lib/mustermann/composite.rb', line 58 def (behavior = nil, values = {}) raise NotImplementedError, 'expanding not supported' unless respond_to? :expand @expander ||= Mustermann::Expander.new(*patterns) @expander.(behavior, values) end |
#match(string) ⇒ Object
47 48 49 |
# File 'lib/mustermann/composite.rb', line 47 def match(string) with_matching(string, :match) end |
#params(string) ⇒ Object
42 43 44 |
# File 'lib/mustermann/composite.rb', line 42 def params(string) with_matching(string, :params) end |
#to_s ⇒ String
Returns the string representation of the pattern.
71 72 73 |
# File 'lib/mustermann/composite.rb', line 71 def to_s simple_inspect end |
#to_templates ⇒ String
This method is only implemented by certain subclasses.
Expanding is supported by almost all patterns (notable execptions are Shell, Regular and Simple).
Union Mustermann::Composite patterns (with the | operator) support expanding if all patterns they are composed of also support it.
65 66 67 68 |
# File 'lib/mustermann/composite.rb', line 65 def to_templates raise NotImplementedError, 'template generation not supported' unless respond_to? :to_templates patterns.flat_map(&:to_templates).uniq end |