Class: Mimi::DB::Dictate::SchemaDefinition::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/mimi/db/dictate/schema_definition.rb

Overview

Represents a column in schema definition

Constant Summary collapse

DEFAULT_TYPE =
:string

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts) ⇒ Column

Creates a Column object

Parameters:

  • name (String, Symbol)
  • opts (Hash)


92
93
94
95
96
97
98
99
# File 'lib/mimi/db/dictate/schema_definition.rb', line 92

def initialize(name, opts)
  @name = name.to_sym
  @params = opts.dup
  @params[:type] ||= @params[:as] || DEFAULT_TYPE
  @params = Mimi::DB::Dictate::TypeDefaults.infer_params(@params)
  @type = @params[:type]
  @sequel_type = @params[:sequel_type]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



85
86
87
# File 'lib/mimi/db/dictate/schema_definition.rb', line 85

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



85
86
87
# File 'lib/mimi/db/dictate/schema_definition.rb', line 85

def params
  @params
end

#sequel_typeObject (readonly)

Returns the value of attribute sequel_type.



85
86
87
# File 'lib/mimi/db/dictate/schema_definition.rb', line 85

def sequel_type
  @sequel_type
end

#typeObject (readonly)

Returns the value of attribute type.



85
86
87
# File 'lib/mimi/db/dictate/schema_definition.rb', line 85

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mimi/db/dictate/schema_definition.rb', line 122

def ==(other)
  unless other.name == name
    raise ArgumentError, 'Cannot compare columns with different names'
  end
  equal = true
  equal &&= params[:db_type] == other.params[:db_type]
  equal &&= params[:primary_key] == other.params[:primary_key]
  equal &&= params[:not_null] == other.params[:not_null]
  equal &&= params[:db_default].to_s == other.params[:db_default].to_s
  equal
end

#to_hObject



101
102
103
104
105
106
# File 'lib/mimi/db/dictate/schema_definition.rb', line 101

def to_h
  {
    name: name,
    params: params.dup
  }
end

#to_sObject



114
115
116
117
118
119
120
# File 'lib/mimi/db/dictate/schema_definition.rb', line 114

def to_s
  public_params = params.only(
    :type, :primary_key, :auto_increment, :not_null, :default, :size
  ).select { |_, v| v }.to_h
  public_params = public_params.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')
  "#{name}(#{public_params})"
end

#to_sequel_paramsObject



108
109
110
111
112
# File 'lib/mimi/db/dictate/schema_definition.rb', line 108

def to_sequel_params
  p = params.dup.except(:type, :as)
  p[:null] = !p[:not_null] if p.key?(:not_null)
  p
end