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 17
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 26
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 11
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 32
def ==(pattern)
  patterns == patterns_from(pattern)
end
===(string) click to toggle source

@see Mustermann::Pattern#===

# File lib/mustermann/composite.rb, line 47
def ===(string)
  patterns.map { |p| p === string }.inject(operator)
end
eql?(pattern) click to toggle source

@see Mustermann::Pattern#eql?

# File lib/mustermann/composite.rb, line 37
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 68
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

@see Mustermann::Pattern#hash

# File lib/mustermann/composite.rb, line 42
def hash
  patterns.hash | operator.hash
end
inspect() click to toggle source

@!visibility private

# File lib/mustermann/composite.rb, line 86
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 57
def match(string)
  with_matching(string, :match)
end
params(string) click to toggle source

@see Mustermann::Pattern#params

# File lib/mustermann/composite.rb, line 52
def params(string)
  with_matching(string, :params)
end
respond_to_special?(method) click to toggle source

@!visibility private

# File lib/mustermann/composite.rb, line 62
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 91
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 81
def to_s
  simple_inspect
end
to_templates() click to toggle source

(see Mustermann::Pattern#to_templates)

# File lib/mustermann/composite.rb, line 75
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 = nil) click to toggle source

@!visibility private

# File lib/mustermann/composite.rb, line 104
def patterns_from(pattern, options = nil)
  return pattern.patterns if pattern.is_a? Composite and pattern.operator == self.operator
  [options ? Mustermann.new(pattern, **options) : pattern]
end
with_matching(string, method) click to toggle source

@!visibility private

# File lib/mustermann/composite.rb, line 97
def with_matching(string, method)
  return unless self === string
  pattern = patterns.detect { |p| p === string }
  pattern.public_send(method, string) if pattern
end