Module: Rigor::ModuleGraph::Inflector

Defined in:
lib/rigor/module_graph/inflector.rb

Overview

Minimal Rails-style inflection helpers. Used to infer a class name from a Rails association argument (+has_many :invoices+ → Invoice).

Deliberately tiny — we don’t ship ActiveSupport::Inflector and its irregular-noun table; the plugin records its guess at confidence: “syntax” so a downstream reviewer can spot mis-singularised plurals in the graph. Apps that need exact association class names should rely on class_name: overrides in the source, which the Analyzer reads verbatim.

Constant Summary collapse

IRREGULAR_PLURALS =
{
  "people" => "person",
  "men" => "man",
  "women" => "woman",
  "children" => "child",
  "feet" => "foot",
  "teeth" => "tooth",
  "geese" => "goose",
  "mice" => "mouse",
  "lice" => "louse"
}.freeze

Class Method Summary collapse

Class Method Details

.camelize(word) ⇒ Object

“foo_bar_baz” → “FooBarBaz”. Plain Rails camelize without acronym handling.



45
46
47
# File 'lib/rigor/module_graph/inflector.rb', line 45

def camelize(word)
  word.to_s.split("_").map { |seg| seg.empty? ? seg : seg[0].upcase + seg[1..] }.join
end

.class_name_for(symbol_or_string) ⇒ Object

“invoices” → “Invoice” — the common Rails association argument to class-name path.



51
52
53
# File 'lib/rigor/module_graph/inflector.rb', line 51

def class_name_for(symbol_or_string)
  camelize(singularize(symbol_or_string.to_s))
end

.preserve_case(replacement, original) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/rigor/module_graph/inflector.rb', line 55

def preserve_case(replacement, original)
  # The irregular table is lower-case; preserve a leading
  # capital from the source so +People+ → +Person+.
  return replacement.capitalize if original[0] =~ /[A-Z]/

  replacement.dup
end

.singularize(word) ⇒ String

Returns best-effort singular form.

Parameters:

  • word (String)

Returns:

  • (String)

    best-effort singular form



32
33
34
35
36
37
38
39
40
41
# File 'lib/rigor/module_graph/inflector.rb', line 32

def singularize(word)
  downcased = word.downcase
  return word.dup if word.empty?
  return preserve_case(IRREGULAR_PLURALS[downcased], word) if IRREGULAR_PLURALS.key?(downcased)
  return word[0..-4] + "y" if word =~ /ies\z/i && word.size > 3
  return word[0..-3] if word =~ /ses\z/i # buses → bus, classes → clas... we accept loss
  return word[0..-2] if word.end_with?("s") && !word.end_with?("ss")

  word.dup
end