Class: Iriq::PathShape
- Inherits:
-
Object
- Object
- Iriq::PathShape
- Defined in:
- lib/iriq/path_shape.rb
Overview
Converts a sequence of path segments into a route-shape string by replacing variable segments with ‘hint` placeholders, falling back to `type` when no hint is available.
PathShape.for(["users", "123", "orders", "456"])
# => "/users/{user_id}/orders/{order_id}"
Pass ‘hints: false` to use raw types instead:
PathShape.for(["users", "123"], hints: false)
# => "/users/{integer_id}"
Class Method Summary collapse
Instance Method Summary collapse
- #for(segments) ⇒ Object
-
#from_entries(entries) ⇒ Object
Build a shape string from already-derived SegmentHints entries.
-
#initialize(classifier: SegmentClassifier::DEFAULT, hints: true) ⇒ PathShape
constructor
A new instance of PathShape.
- #shape_token(entry) ⇒ Object
Constructor Details
#initialize(classifier: SegmentClassifier::DEFAULT, hints: true) ⇒ PathShape
Returns a new instance of PathShape.
14 15 16 17 |
# File 'lib/iriq/path_shape.rb', line 14 def initialize(classifier: SegmentClassifier::DEFAULT, hints: true) @classifier = classifier @hints = hints end |
Class Method Details
.for(segments, classifier: SegmentClassifier::DEFAULT, hints: true) ⇒ Object
41 42 43 |
# File 'lib/iriq/path_shape.rb', line 41 def self.for(segments, classifier: SegmentClassifier::DEFAULT, hints: true) new(classifier: classifier, hints: hints).for(segments) end |
Instance Method Details
#for(segments) ⇒ Object
19 20 21 22 23 |
# File 'lib/iriq/path_shape.rb', line 19 def for(segments) return "/" if segments.nil? || segments.empty? from_entries(SegmentHints.derive(segments, @classifier)) end |
#from_entries(entries) ⇒ Object
Build a shape string from already-derived SegmentHints entries. Used by Corpus to avoid re-deriving entries per observation when it needs multiple shape variants (raw and hinted).
28 29 30 31 32 |
# File 'lib/iriq/path_shape.rb', line 28 def from_entries(entries) return "/" if entries.nil? || entries.empty? "/" + entries.map { |e| shape_token(e) }.join("/") end |
#shape_token(entry) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/iriq/path_shape.rb', line 34 def shape_token(entry) return entry[:value] unless entry[:variable] placeholder = @hints ? (entry[:hint] || entry[:type]) : entry[:type] "{#{placeholder}}" end |