class Mustermann::Composite
Class for pattern objects composed of multiple patterns using binary logic. @see Mustermann::Pattern#& @see Mustermann::Pattern#| @see Mustermann::Pattern#^
Attributes
operator[R]
patterns[R]
Public Class Methods
new(*patterns, **options)
click to toggle source
@return [Mustermann::Pattern] a new composite pattern
Calls superclass method
# File lib/mustermann/composite.rb, line 18 def self.new(*patterns, **options) patterns = patterns.flatten case patterns.size when 0 then raise ArgumentError, 'cannot create empty composite pattern' when 1 then patterns.first else super(patterns, **options) end end
new(patterns, operator: :|, **options)
click to toggle source
# File lib/mustermann/composite.rb, line 27 def initialize(patterns, operator: :|, **options) @operator = operator.to_sym @patterns = patterns.flat_map { |p| patterns_from(p, **options) } end
supported?(option, type: nil, **options)
click to toggle source
@see Mustermann::Pattern.supported?
Calls superclass method
# File lib/mustermann/composite.rb, line 12 def self.supported?(option, type: nil, **options) return true if super Mustermann[type || Mustermann::DEFAULT_TYPE].supported?(option, **options) end
Public Instance Methods
==(pattern)
click to toggle source
@see Mustermann::Pattern#==
# File lib/mustermann/composite.rb, line 33 def ==(pattern) patterns == patterns_from(pattern) end
===(string)
click to toggle source
# File lib/mustermann/composite.rb, line 48 def ===(string) patterns.map { |p| p === string }.inject(operator) end
eql?(pattern)
click to toggle source
# File lib/mustermann/composite.rb, line 38 def eql?(pattern) patterns.eql? patterns_from(pattern) end
expand(behavior = nil, values = {})
click to toggle source
(see Mustermann::Pattern#expand
)
# File lib/mustermann/composite.rb, line 69 def expand(behavior = nil, values = {}) raise NotImplementedError, 'expanding not supported' unless respond_to? :expand @expander ||= Mustermann::Expander.new(*patterns) @expander.expand(behavior, values) end
hash()
click to toggle source
# File lib/mustermann/composite.rb, line 43 def hash patterns.hash | operator.hash end
inspect()
click to toggle source
@!visibility private
# File lib/mustermann/composite.rb, line 87 def inspect "#<%p:%s>" % [self.class, simple_inspect] end
match(string)
click to toggle source
@see Mustermann::Pattern#match
# File lib/mustermann/composite.rb, line 58 def match(string) with_matching(string, :match) end
params(string)
click to toggle source
@see Mustermann::Pattern#params
# File lib/mustermann/composite.rb, line 53 def params(string) with_matching(string, :params) end
respond_to_special?(method)
click to toggle source
@!visibility private
# File lib/mustermann/composite.rb, line 63 def respond_to_special?(method) return false unless operator == :| patterns.all? { |p| p.respond_to?(method) } end
simple_inspect()
click to toggle source
@!visibility private
# File lib/mustermann/composite.rb, line 92 def simple_inspect pattern_strings = patterns.map { |p| p.simple_inspect } "(#{pattern_strings.join(" #{operator} ")})" end
to_s()
click to toggle source
@return [String] the string representation of the pattern
# File lib/mustermann/composite.rb, line 82 def to_s simple_inspect end
to_templates()
click to toggle source
(see Mustermann::Pattern#to_templates
)
# File lib/mustermann/composite.rb, line 76 def to_templates raise NotImplementedError, 'template generation not supported' unless respond_to? :to_templates patterns.flat_map(&:to_templates).uniq end
Private Instance Methods
patterns_from(pattern, **options)
click to toggle source
@!visibility private
# File lib/mustermann/composite.rb, line 105 def patterns_from(pattern, **options) return pattern.patterns if pattern.is_a? Composite and pattern.operator == self.operator [options.empty? && pattern.is_a?(Pattern) ? pattern : Mustermann.new(pattern, **options)] end
with_matching(string, method)
click to toggle source
@!visibility private
# File lib/mustermann/composite.rb, line 98 def with_matching(string, method) return unless self === string pattern = patterns.detect { |p| p === string } pattern.public_send(method, string) if pattern end