Class: RubyPgExtras::DetectFkColumn
- Inherits:
-
Object
- Object
- RubyPgExtras::DetectFkColumn
- Defined in:
- lib/ruby_pg_extras/detect_fk_column.rb
Constant Summary collapse
- PLURAL_RULES =
[ [/s$/i, "s"], [/^(ax|test)is$/i, '\1es'], [/(octop|vir)us$/i, '\1i'], [/(alias|status)$/i, '\1es'], [/(bu)s$/i, '\1ses'], [/(buffal|tomat)o$/i, '\1oes'], [/([ti])um$/i, '\1a'], [/sis$/i, "ses"], [/(?:([^f])fe|([lr])f)$/i, '\1\2ves'], [/([^aeiouy]|qu)y$/i, '\1ies'], [/(x|ch|ss|sh)$/i, '\1es'], [/(matr|vert|ind)(?:ix|ex)$/i, '\1ices'], [/^(m|l)ouse$/i, '\1ice'], [/^(ox)$/i, '\1en'], [/(quiz)$/i, '\1zes'], ]
- IRREGULAR =
{ "person" => "people", "man" => "men", "child" => "children", "sex" => "sexes", "move" => "moves", "zombie" => "zombies", }
- UNCOUNTABLE =
%w(equipment information rice money species series fish sheep jeans police)
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.call(column_name, tables) ⇒ Object
32 33 34 |
# File 'lib/ruby_pg_extras/detect_fk_column.rb', line 32 def self.call(column_name, tables) new.call(column_name, tables) end |
Instance Method Details
#call(column_name, tables) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/ruby_pg_extras/detect_fk_column.rb', line 36 def call(column_name, tables) return false unless column_name =~ /_id$/ table_name = column_name.split("_").first table_name = pluralize(table_name) tables.include?(table_name) end |
#pluralize(word) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ruby_pg_extras/detect_fk_column.rb', line 43 def pluralize(word) return word if UNCOUNTABLE.include?(word.downcase) return IRREGULAR[word] if IRREGULAR.key?(word) return IRREGULAR.invert[word] if IRREGULAR.value?(word) PLURAL_RULES.reverse.each do |(rule, replacement)| return word.gsub(rule, replacement) if word.match?(rule) end word + "s" end |