Module: Volt::Sql::Helper
- Defined in:
- app/sql/lib/helper.rb
Class Method Summary collapse
-
.column_type_and_options_for_sequel(klasses, options) ⇒ Object
Takes in the klass and options specified on the model or an add_column and returns the correct klass/options for add_column in sequel.
-
.klasses_and_options_from_db(db_field) ⇒ Object
This method takes in info from the db schema and returns the volt field klass and options that would have been used to create it.
-
.normalized_indexes_from_table(db, table_name) ⇒ Object
When asking for indexes on a table, the deferrable option will show as nil if it wasn’t set, we remove this to normalize it to the volt options.
Class Method Details
.column_type_and_options_for_sequel(klasses, options) ⇒ Object
Takes in the klass and options specified on the model or an add_column and returns the correct klass/options for add_column in sequel.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/sql/lib/helper.rb', line 68 def self.(klasses, ) = .dup # Remove from start fields klasses ||= [String, NilClass] allow_nil = klasses.include?(NilClass) klasses = klasses.reject {|klass| klass == NilClass } if klasses.size > 1 raise MultiTypeException, 'the sql adaptor only supports a single type (or NilClass) for each field.' end klass = klasses.first if .has_key?(:nil) [:allow_null] = .delete(:nil) else [:allow_null] = allow_nil end if klass == String # If no length restriction, make it text if [:size] if [:size] > 255 # Make the field text .delete(:size) [:text] = true end else [:text] = true end elsif klass == VoltTime klass = Time elsif klass == Volt::Boolean || klass == TrueClass || klass == FalseClass klass = TrueClass # what sequel uses for booleans end return klass, end |
.klasses_and_options_from_db(db_field) ⇒ Object
This method takes in info from the db schema and returns the volt field klass and options that would have been used to create it.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 61 62 63 |
# File 'app/sql/lib/helper.rb', line 13 def self.(db_field) klasses = [] = {} # merge values based on map (options key from db_key) { :text => :text, :size => :max_length, :nil => :allow_null, :default => :ruby_default }.each_pair do |opts_key, db_key| [opts_key] = db_field[db_key] if db_field.has_key?(db_key) end .delete(:default) if [:default] == nil db_type = db_field[:db_type].to_sym case db_field[:type] when :string klasses << String when :datetime klasses << VoltTime when :boolean klasses << Volt::Boolean when :float klasses << Float else case db_type when :text when :string klasses << String when :numeric klasses << Numeric when :integer klasses << Fixnum end end if klasses.size == 0 raise "Could not match database type #{db_type} in #{db_field.inspect}" end # Default is to allow nil unless [:nil] == false klasses << NilClass end .delete(:nil) return klasses, end |
.normalized_indexes_from_table(db, table_name) ⇒ Object
When asking for indexes on a table, the deferrable option will show as nil if it wasn’t set, we remove this to normalize it to the volt options.
112 113 114 115 116 117 118 |
# File 'app/sql/lib/helper.rb', line 112 def self.normalized_indexes_from_table(db, table_name) db.indexes(table_name).map do |name, | # Remove the deferrable that defaults to false (nil) = .reject {|key, value| key == :deferrable && !value } [name, ] end.to_h end |