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`.

Class Method Summary collapse

Class Method Details

.derive(segments, classifier) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/iriq/segment_hints.rb', line 8

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



21
22
23
24
25
26
27
28
29
30
# File 'lib/iriq/segment_hints.rb', line 21

def hint_for(segments, i, type, variable, classifier)
  return nil unless variable && i > 0

  prev = segments[i - 1]
  return nil unless classifier.classify(prev) == :literal

  base   = Inflector.singularize(prev)
  suffix = type == :uuid ? "_uuid" : "_id"
  "#{base}#{suffix}"
end