Class: PgGraph::Inflector
- Inherits:
-
Object
- Object
- PgGraph::Inflector
- Defined in:
- lib/pg_graph/inflector.rb
Overview
< Dry::Inflector
Constant Summary collapse
- SUPPORTED_RUBY_CLASSES =
[String, Integer, Float, BigDecimal, Boolean, Hash, Time, NilClass, Array]
Instance Method Summary collapse
-
#camelize(s) ⇒ Object
Camelize string.
-
#column2field_name(column_name) ⇒ Object
Convert a column name to a field name by removing a ‘_id’ at the end.
-
#initialize ⇒ Inflector
constructor
A new instance of Inflector.
-
#klass_name(qualified_klass_name) ⇒ Object
Remove module prefix from class names.
-
#plural?(word) ⇒ Boolean
#plural? is defined using #singularize because #pluralize would cause endless recursion.
- #pluralize(word) ⇒ Object
-
#postgres_type2ruby_class(name) ⇒ Object
Types are both native postgres type and types from the information_schema.
- #record_type2table(name) ⇒ Object
- #record_type2table_type(name) ⇒ Object
- #singular?(word) ⇒ Boolean
-
#singularize(word) ⇒ Object
Note that DryInflector::singularize handles the special PgGraph pluralization rules by default.
- #table2record_type(name) ⇒ Object
- #table2table_type(name) ⇒ Object
- #table_type2record_type(name) ⇒ Object
- #table_type2table(name) ⇒ Object
- #type2array(name) ⇒ Object
Constructor Details
#initialize ⇒ Inflector
Returns a new instance of Inflector.
5 6 7 |
# File 'lib/pg_graph/inflector.rb', line 5 def initialize() @inflector = Dry::Inflector.new end |
Instance Method Details
#camelize(s) ⇒ Object
Camelize string
74 75 76 77 |
# File 'lib/pg_graph/inflector.rb', line 74 def camelize(s) s = s.sub(/^[a-z\d]*/) { |match| match.capitalize } s.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" } end |
#column2field_name(column_name) ⇒ Object
Convert a column name to a field name by removing a ‘_id’ at the end
68 69 70 71 |
# File 'lib/pg_graph/inflector.rb', line 68 def column2field_name(column_name) # column_name.sub(/_id$|_kind$/, "") column_name.sub(/_id$/, "") end |
#klass_name(qualified_klass_name) ⇒ Object
Remove module prefix from class names. Used in debugging
80 81 82 |
# File 'lib/pg_graph/inflector.rb', line 80 def klass_name(qualified_klass_name) demodulize(qualified_klass_name) end |
#plural?(word) ⇒ Boolean
#plural? is defined using #singularize because #pluralize would cause endless recursion
27 28 29 |
# File 'lib/pg_graph/inflector.rb', line 27 def plural?(word) singularize(word) != word end |
#pluralize(word) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/pg_graph/inflector.rb', line 9 def pluralize(word) return word if plural? word result = @inflector.pluralize(word) if result == word word =~ /s$/ ? "#{word}es" : "#{word}s" else result end end |
#postgres_type2ruby_class(name) ⇒ Object
Types are both native postgres type and types from the information_schema
Unsupported types: box, circle, interval, line, lseg, money, path, pg_lsn, pg_snapshot, point, polygon
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/pg_graph/inflector.rb', line 88 def postgres_type2ruby_class(name) case name # Boolean when "bool", "boolean"; Boolean # Character types when "character", "char", "character varying", "varchar", "text"; String # Binary data when "bit", "bit varying", "varbit", "bytea"; String # Integer types when "bigint", "int8", "integer", "int", "int4", "smallint", "int2"; Integer # Float types when "double precision", "float8", "real", "float4", "float" ; Float # Decimal types when "numeric", "decimal"; BigDecimal # Temporal when "time", "time with time zone", "timetz", "time without time zone", "timestamp", "timestamp with time zone", "timestamptz", "timestamp without time zone"; Time when "date"; Date # Array when /^_/; Array # Serials when "bigserial", "serial8", "serial", "serial4", "smallserial", "serial2"; Integer # UUIDS when "uuid"; String # JSON when "json", "jsonb"; Hash # XML when "xml"; String # Special types when "cidr"; String when "inet"; String when "macaddr", "macaddr8"; String when "tsquery", "tsvector"; String else raise "PgGraph doesn't support the '#{name.inspect}' postgres type" end end |
#record_type2table(name) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/pg_graph/inflector.rb', line 51 def record_type2table(name) if name =~ /s$/ name + "es" else pluralize(name) end end |
#record_type2table_type(name) ⇒ Object
59 60 61 |
# File 'lib/pg_graph/inflector.rb', line 59 def record_type2table_type(name) type2array(name) end |
#singular?(word) ⇒ Boolean
31 32 33 |
# File 'lib/pg_graph/inflector.rb', line 31 def singular?(word) singularize(word) == word end |
#singularize(word) ⇒ Object
Note that DryInflector::singularize handles the special PgGraph pluralization rules by default
21 22 23 |
# File 'lib/pg_graph/inflector.rb', line 21 def singularize(word) @inflector.singularize(word) end |
#table2record_type(name) ⇒ Object
39 40 41 |
# File 'lib/pg_graph/inflector.rb', line 39 def table2record_type(name) singularize(name) end |
#table2table_type(name) ⇒ Object
35 36 37 |
# File 'lib/pg_graph/inflector.rb', line 35 def table2table_type(name) record_type2table_type(table2record_type(name)) end |
#table_type2record_type(name) ⇒ Object
47 48 49 |
# File 'lib/pg_graph/inflector.rb', line 47 def table_type2record_type(name) name[1..-2] end |
#table_type2table(name) ⇒ Object
43 44 45 |
# File 'lib/pg_graph/inflector.rb', line 43 def table_type2table(name) record_type2table(table_type2record_type(name)) end |
#type2array(name) ⇒ Object
63 64 65 |
# File 'lib/pg_graph/inflector.rb', line 63 def type2array(name) "[#{name}]" end |