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

name[R]

Column type, name and options.

opts[R]

Column type, name and options.

type[R]

Column type, name and options.

Public Class Methods

new( type, name, opts = {} ) click to toggle source

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

==(other) click to toggle source

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
allow_null() click to toggle source

Check whether the column allow NULL values.

# File lib/miguel/schema.rb, line 118
def allow_null
  allow = opts[ :null ]
  allow.nil? || allow
end
canonic_opts() click to toggle source

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
canonic_size( size ) click to toggle source

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
canonic_type() click to toggle source

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
default() click to toggle source

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
default_opts() click to toggle source

Get options with default value included.

# File lib/miguel/schema.rb, line 183
def default_opts
  { :default => default }.merge( opts )
end
dump( out ) click to toggle source

Dump column definition.

# File lib/miguel/schema.rb, line 196
def dump( out )
  out << "#{type} #{out_name}#{out_opts}"
end
primary_key_constraint?() click to toggle source

Test if the column is in fact just a primary key constraint.

# File lib/miguel/schema.rb, line 92
def primary_key_constraint?
  type == :primary_key && name.is_a?( Array )
end
type_default() click to toggle source

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
type_opts() click to toggle source

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