Class: Mustermann::Composite

Inherits:
Pattern
  • Object
show all
Defined in:
lib/mustermann/composite.rb

Overview

Class for pattern objects composed of multiple patterns using binary logic.

Constant Summary

Constants inherited from Pattern

Pattern::PATTERN_METHODS

Constants included from Mustermann

DEFAULT_TYPE, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pattern

#&, #=~, #^, #named_captures, #names, #peek, #peek_match, #peek_params, #peek_size, supported_options, #to_proc, #|

Methods included from Mustermann

[], register

Constructor Details

#initialize(patterns, options = {}) ⇒ Composite

Returns a new instance of Composite.



27
28
29
30
31
# File 'lib/mustermann/composite.rb', line 27

def initialize(patterns, options = {})
  operator = options.delete(:operator) || :|
  @operator = operator.to_sym
  @patterns = patterns.flat_map { |p| patterns_from(p, options) }
end

Instance Attribute Details

#operatorObject (readonly)

Returns the value of attribute operator.



7
8
9
# File 'lib/mustermann/composite.rb', line 7

def operator
  @operator
end

#patternsObject (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) ⇒ Mustermann::Pattern

Returns a new composite pattern.

Returns:



17
18
19
20
21
22
23
24
25
# File 'lib/mustermann/composite.rb', line 17

def self.new(*patterns)
  options = patterns.last.kind_of?(Hash) ? patterns.pop : {}
  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

.supported?(option, options = {}) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



11
12
13
14
# File 'lib/mustermann/composite.rb', line 11

def self.supported?(option, options = {})
  return true if super
  options[:type] and Mustermann[options[:type]].supported?(option, options)
end

Instance Method Details

#==(pattern) ⇒ Object

See Also:

  • Pattern#==


34
35
36
# File 'lib/mustermann/composite.rb', line 34

def ==(pattern)
  patterns == patterns_from(pattern)
end

#===(string) ⇒ Object

See Also:



39
40
41
# File 'lib/mustermann/composite.rb', line 39

def ===(string)
  patterns.map { |p| p === string }.inject(operator)
end

#expand(behavior = nil, values = {}) ⇒ String

Note:

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.

Examples:

Expanding a pattern

pattern = Mustermann.new('/:name(.:ext)?')
pattern.expand(name: 'hello')             # => "/hello"
pattern.expand(name: 'hello', ext: 'png') # => "/hello.png"

Checking if a pattern supports expanding

if pattern.respond_to? :expand
  pattern.expand(name: "foo")
else
  warn "does not support expanding"
end

Parameters:

  • behavior (Symbol) (defaults to: nil)

    What to do with additional key/value pairs not present in the values hash. Possible options: :raise, :ignore, :append.

  • values (Hash{Symbol: #to_s, Array<#to_s>}) (defaults to: {})

    Values to use for expansion.

Returns:

  • (String)

    expanded string

Raises:

  • (NotImplementedError)

    raised if expand is not supported.

  • (Mustermann::ExpandError)

    raised if a value is missing or unknown

See Also:



60
61
62
63
64
# File 'lib/mustermann/composite.rb', line 60

def expand(behavior = nil, values = {})
  raise NotImplementedError, 'expanding not supported' unless respond_to? :expand
  @expander ||= Mustermann::Expander.new(*patterns)
  @expander.expand(behavior, values)
end

#match(string) ⇒ Object

See Also:



49
50
51
# File 'lib/mustermann/composite.rb', line 49

def match(string)
  with_matching(string, :match)
end

#params(string) ⇒ Object

See Also:



44
45
46
# File 'lib/mustermann/composite.rb', line 44

def params(string)
  with_matching(string, :params)
end

#to_sString

Returns the string representation of the pattern.

Returns:

  • (String)

    the string representation of the pattern



73
74
75
# File 'lib/mustermann/composite.rb', line 73

def to_s
  simple_inspect
end

#to_templatesString

Note:

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.

Examples:

Expanding a pattern

pattern = Mustermann.new('/:name(.:ext)?')
pattern.expand(name: 'hello')             # => "/hello"
pattern.expand(name: 'hello', ext: 'png') # => "/hello.png"

Checking if a pattern supports expanding

if pattern.respond_to? :expand
  pattern.expand(name: "foo")
else
  warn "does not support expanding"
end

Parameters:

  • behavior (Symbol)

    What to do with additional key/value pairs not present in the values hash. Possible options: :raise, :ignore, :append.

  • values (Hash{Symbol: #to_s, Array<#to_s>})

    Values to use for expansion.

Returns:

  • (String)

    expanded string

Raises:

  • (NotImplementedError)

    raised if expand is not supported.

  • (Mustermann::ExpandError)

    raised if a value is missing or unknown

See Also:



67
68
69
70
# File 'lib/mustermann/composite.rb', line 67

def to_templates
  raise NotImplementedError, 'template generation not supported' unless respond_to? :to_templates
  patterns.flat_map(&:to_templates).uniq
end