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
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 execptions 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
44 45 46 |
# File 'lib/mustermann/concat.rb', line 44 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 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.
79 80 81 82 83 |
# File 'lib/mustermann/concat.rb', line 79 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
49 50 51 52 |
# File 'lib/mustermann/concat.rb', line 49 def match(string) peeked = peek_match(string) peeked if peeked.to_s == string end |
#operator ⇒ Symbol
Returns always :+.
39 40 41 |
# File 'lib/mustermann/concat.rb', line 39 def operator :+ end |
#params(string) ⇒ Object
55 56 57 58 |
# File 'lib/mustermann/concat.rb', line 55 def params(string) params, size = peek_params(string) params if size == string.size end |
#peek_match(string) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/mustermann/concat.rb', line 66 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
74 75 76 |
# File 'lib/mustermann/concat.rb', line 74 def peek_params(string) pump(string, inject_with: :merge, with_size: true) { |p, s| p.peek_params(s) } end |
#peek_size(string) ⇒ Object
61 62 63 |
# File 'lib/mustermann/concat.rb', line 61 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.
86 87 88 89 |
# File 'lib/mustermann/concat.rb', line 86 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 |