Module: Iriq::SegmentHints
- Defined in:
- lib/iriq/segment_hints.rb
Overview
Walks a segment list and annotates each entry with the type, whether it’s variable, and a RESTful “hint” (e.g. ‘user_id`) when a variable segment follows a literal one — `/users/123` ⇒ hint `user_id`.
Constant Summary collapse
- HINT_ELIGIBLE_TYPES =
Only ID-shaped types get the noun-singularize hint. Semantic types (version, locale, currency, date, etc.) are more informative as ‘type` than as `noun_id` — `/api/v1/…` should render `version`, not `api_id`.
%i[integer uuid hash opaque_id slug].to_set.freeze
Class Method Summary collapse
Class Method Details
.derive(segments, classifier) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/iriq/segment_hints.rb', line 16 def derive(segments, classifier) segments.each_with_index.map do |seg, i| type = classifier.classify(seg) variable = classifier.variable?(type) { value: seg, type: type, variable: variable, hint: hint_for(segments, i, type, variable, classifier), } end end |
.hint_for(segments, i, type, variable, classifier) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/iriq/segment_hints.rb', line 29 def hint_for(segments, i, type, variable, classifier) return nil unless variable && i > 0 return nil unless HINT_ELIGIBLE_TYPES.include?(type) prev = segments[i - 1] return nil unless classifier.classify(prev) == :literal base = Inflector.singularize(prev) suffix = type == :uuid ? "_uuid" : "_id" "#{base}#{suffix}" end |