Module: Automodel::Helpers
- Defined in:
- lib/automodel/helpers.rb
Overview
Houses some helper methods used directly by #automodel.
Class Method Summary collapse
-
.map_tables(connection_handler, subschema: '') ⇒ Array<Hash>
Takes a connection handler (an object that implements ActiveRecord::ConnectionHandling), scrapes the target database, and returns a list of the tables' metadata.
-
.railsy_column_name(column) ⇒ String
Returns a Railsy name for the given column.
-
.railsy_name(name) ⇒ String
Returns the given name in Railsy form.
-
.register_class(class_object, as:, within: nil) ⇒ Class
Registers the given class as the given name and within the given namespace (if any).
Class Method Details
.map_tables(connection_handler, subschema: '') ⇒ Array<Hash>
Takes a connection handler (an object that implements ActiveRecord::ConnectionHandling), scrapes the target database, and returns a list of the tables' metadata.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/automodel/helpers.rb', line 34 def map_tables(connection_handler, subschema: '') ## Normalize the "subschema" name. ## subschema = "#{subschema}.".sub(%r{\.+$}, '.').sub(%r{^\.}, '') ## Prep the Automodel::SchemaInspector we'll be using. ## schema_inspector = Automodel::SchemaInspector.new(connection_handler) ## Get as much metadata as possible out of the Automodel::SchemaInspector. ## schema_inspector.tables.map do |table_name| table = {} table[:name] = "#{subschema}#{table_name}" table[:columns] = schema_inspector.columns(table[:name]) table[:primary_key] = schema_inspector.primary_key(table[:name]) table[:foreign_keys] = schema_inspector.foreign_keys(table[:name]) table[:base_name] = table[:name].split('.').last table[:model_name] = table[:base_name].underscore.classify table[:composite_primary_key] = table[:primary_key].is_a? Array table[:column_aliases] = table[:columns].map { |column| [column.name, column] }.to_h table end end |
.railsy_column_name(column) ⇒ String
Returns a Railsy name for the given column.
77 78 79 80 81 82 |
# File 'lib/automodel/helpers.rb', line 77 def railsy_column_name(column) name = railsy_name(column.name) name = name.sub(%r{^is_}, '') if column.type == :boolean name end |
.railsy_name(name) ⇒ String
Returns the given name in Railsy form.
94 95 96 |
# File 'lib/automodel/helpers.rb', line 94 def railsy_name(name) name.to_s.gsub(%r{[^a-z0-9]+}i, '_').underscore end |
.register_class(class_object, as:, within: nil) ⇒ Class
Registers the given class as the given name and within the given namespace (if any).
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/automodel/helpers.rb', line 124 def register_class(class_object, as:, within: nil) components = within.to_s.split('::').compact.map(&:to_sym) components.unshift(:Kernel) unless components.first.to_s.safe_constantize.present? namespace = components.shift.to_s.constantize components.each do |component| namespace = if component.in? namespace.constants namespace.const_get(component) else namespace.const_set(component, Module.new) end end namespace.const_set(as, class_object) end |