Class: Iriq::PathShape

Inherits:
Object
  • Object
show all
Defined in:
lib/iriq/path_shape.rb

Overview

Renderer that produces a route-shape string by replacing variable segments with ‘hint` placeholders. As of v0.16 this is a thin wrapper around Iriq::Shape — kept for back-compat with callers that still want to get a string in one call.

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}"

Pass ‘canonical_dates: true` to render date-typed segments in canonical ISO form (2024/01/15 → 2024-01-15) instead of as a `date` placeholder. Pass `canonical_currencies: true` for the same treatment of currency codes (`usd` → `USD`).

For new code, prefer building an Iriq::Shape directly and calling ‘#render`. PathShape stays available for the common string-only path.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(classifier: SegmentClassifier::DEFAULT, hints: true, canonical_dates: false, canonical_currencies: false) ⇒ PathShape

Returns a new instance of PathShape.



23
24
25
26
27
28
29
# File 'lib/iriq/path_shape.rb', line 23

def initialize(classifier: SegmentClassifier::DEFAULT, hints: true,
               canonical_dates: false, canonical_currencies: false)
  @classifier           = classifier
  @hints                = hints
  @canonical_dates      = canonical_dates
  @canonical_currencies = canonical_currencies
end

Class Method Details

.for(segments, classifier: SegmentClassifier::DEFAULT, hints: true, canonical_dates: false, canonical_currencies: false) ⇒ Object



44
45
46
47
48
49
# File 'lib/iriq/path_shape.rb', line 44

def self.for(segments, classifier: SegmentClassifier::DEFAULT, hints: true,
             canonical_dates: false, canonical_currencies: false)
  new(classifier: classifier, hints: hints,
      canonical_dates: canonical_dates,
      canonical_currencies: canonical_currencies).for(segments)
end

Instance Method Details

#for(segments) ⇒ Object



31
32
33
# File 'lib/iriq/path_shape.rb', line 31

def for(segments)
  from_entries(SegmentHints.derive(segments || [], @classifier))
end

#from_entries(entries) ⇒ Object

Build a shape string from already-derived SegmentHints entries.



36
37
38
39
40
41
42
# File 'lib/iriq/path_shape.rb', line 36

def from_entries(entries)
  Shape.from_entries(entries).render(
    hints: @hints,
    canonical_dates: @canonical_dates,
    canonical_currencies: @canonical_currencies,
  )
end