Class: Mustermann::Concat
- Defined in:
- lib/mustermann/concat.rb
Overview
Class for pattern objects that are a concatenation of other patterns.
Constant Summary
Constants included from Mustermann
CompileError, DEFAULT_TYPE, Error, ExpandError, ParseError
Instance Attribute Summary
Attributes inherited from Composite
Attributes inherited from Pattern
Instance Method Summary collapse
- #===(string) ⇒ Object
-
#expand(behavior = nil, values = {}) ⇒ String
Expanding is supported by almost all patterns (notable exceptions are Shell, Regular and Simple).
- #match(string) ⇒ Object
-
#operator ⇒ Symbol
Always :+.
- #params(string) ⇒ Object
- #peek_match(string) ⇒ Object
- #peek_params(string) ⇒ Object
- #peek_size(string) ⇒ Object
-
#to_templates ⇒ Array<String>
Generates a list of URI template strings representing the pattern.
Methods inherited from Composite
#==, #eql?, #hash, new, supported?, #to_s
Methods inherited from Pattern
#+, #==, #=~, #eql?, #hash, #named_captures, #names, new, #peek, supported?, supported_options, #to_proc, #to_s, #|
Methods included from Mustermann
Instance Method Details
#===(string) ⇒ Object
55 56 57 |
# File 'lib/mustermann/concat.rb', line 55 def ===(string) peek_size(string) == string.size end |
#expand(behavior = nil, values = {}) ⇒ String
This method is only implemented by certain subclasses.
Expanding is supported by almost all patterns (notable exceptions are Shell, Regular and Simple).
Union Mustermann::Composite patterns (with the | operator) support expanding if all patterns they are composed of also support it.
90 91 92 93 94 |
# File 'lib/mustermann/concat.rb', line 90 def (behavior = nil, values = {}) raise NotImplementedError, 'expanding not supported' unless respond_to? :expand @expander ||= Mustermann::Expander.new(self) { combined_ast } @expander.(behavior, values) end |
#match(string) ⇒ Object
60 61 62 63 |
# File 'lib/mustermann/concat.rb', line 60 def match(string) peeked = peek_match(string) peeked if peeked.to_s == string end |
#operator ⇒ Symbol
Returns always :+.
50 51 52 |
# File 'lib/mustermann/concat.rb', line 50 def operator :+ end |
#params(string) ⇒ Object
66 67 68 69 |
# File 'lib/mustermann/concat.rb', line 66 def params(string) params, size = peek_params(string) params if size == string.size end |
#peek_match(string) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/mustermann/concat.rb', line 77 def peek_match(string) pump(string, initial: SimpleMatch.new) do |pattern, substring| return unless match = pattern.peek_match(substring) [match, match.to_s.size] end end |
#peek_params(string) ⇒ Object
85 86 87 |
# File 'lib/mustermann/concat.rb', line 85 def peek_params(string) pump(string, inject_with: :merge, with_size: true) { |p, s| p.peek_params(s) } end |
#peek_size(string) ⇒ Object
72 73 74 |
# File 'lib/mustermann/concat.rb', line 72 def peek_size(string) pump(string) { |p,s| p.peek_size(s) } end |
#to_templates ⇒ Array<String>
This method is only implemented by certain subclasses.
Generates a list of URI template strings representing the pattern.
Note that this transformation is lossy and the strings matching these templates might not match the pattern (and vice versa).
This comes in quite handy since URI templates are not made for pattern matching. That way you can easily use a more precise template syntax and have it automatically generate hypermedia links for you.
Template generation is supported by almost all patterns (notable exceptions are Shell, Regular and Simple). Union Mustermann::Composite patterns (with the | operator) support template generation if all patterns they are composed of also support it.
97 98 99 100 |
# File 'lib/mustermann/concat.rb', line 97 def to_templates raise NotImplementedError, 'template generation not supported' unless respond_to? :to_templates @to_templates ||= patterns.inject(['']) { |list, pattern| list.product(pattern.to_templates).map(&:join) }.uniq end |