# File lib/miguel/schema.rb, line 92 def primary_key_constraint? type == :primary_key && name.is_a?( Array ) end
class Miguel::Schema::Column
Class representing single database column.
Constants
- CANONIC_TYPES
Canonic names of some builtin ruby types.
- DEFAULT_OPTS
Default options implied for certain types.
- IGNORED_OPTS
Options which are ignored for columns. These usually relate to the associated foreign key constraints, not the column itself.
- NON_TYPE_OPTS
Options which are not relevant to type specification.
Attributes
Column
type, name and options.
Column
type, name and options.
Column
type, name and options.
Public Class Methods
Create new column with given type and name.
# File lib/miguel/schema.rb, line 85 def initialize( type, name, opts = {} ) @type = type @name = name @opts = opts end
Public Instance Methods
Compare one column with another one.
# File lib/miguel/schema.rb, line 188 def == other other.is_a?( Column ) && name == other.name && canonic_type == other.canonic_type && canonic_opts == other.canonic_opts end
Check whether the column allow NULL values.
# File lib/miguel/schema.rb, line 118 def allow_null allow = opts[ :null ] allow.nil? || allow end
Get the column options in a canonic way.
# File lib/miguel/schema.rb, line 173 def canonic_opts return {} if primary_key_constraint? o = { :type => canonic_type, :default => default, :null => true } o.merge!( DEFAULT_OPTS[ canonic_type ] || {} ) o.merge!( opts ) o[ :size ] = canonic_size( o[ :size ] ) o.delete_if{ |key, value| IGNORED_OPTS.include? key } end
Convert given size into its canonic form.
# File lib/miguel/schema.rb, line 150 def canonic_size( size ) if canonic_type == :decimal && size.is_a?( Integer ) [ size, 0 ] else size end end
Get the canonic type name, for type comparison.
# File lib/miguel/schema.rb, line 144 def canonic_type t = type.to_s.downcase.to_sym CANONIC_TYPES[ t ] || t end
Get the column default.
# File lib/miguel/schema.rb, line 97 def default d = opts[ :default ] d = type_default if d.nil? && ! allow_null d end
Get options with default value included.
# File lib/miguel/schema.rb, line 183 def default_opts { :default => default }.merge( opts ) end
Dump column definition.
# File lib/miguel/schema.rb, line 196 def dump( out ) out << "#{type} #{out_name}#{out_opts}" end
Test if the column is in fact just a primary key constraint.
Get default default for column type.
# File lib/miguel/schema.rb, line 104 def type_default case canonic_type when :string "" when :boolean false when :enum, :set [ *opts[ :elements ], "" ].first else 0 end end
Get opts relevant to the column type only (excludes :null and :default).
# File lib/miguel/schema.rb, line 127 def type_opts opts.reject{ |key, value| NON_TYPE_OPTS.include? key } end