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

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

Defined Under Namespace

Classes: Column, Index

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name) ⇒ SchemaDefinition

Returns a new instance of SchemaDefinition.



9
10
11
12
13
# File 'lib/mimi/db/dictate/schema_definition.rb', line 9

def initialize(table_name)
  @table_name = table_name
  @columns = {}
  @indexes = []
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



7
8
9
# File 'lib/mimi/db/dictate/schema_definition.rb', line 7

def columns
  @columns
end

#indexesObject (readonly)

Returns the value of attribute indexes.



7
8
9
# File 'lib/mimi/db/dictate/schema_definition.rb', line 7

def indexes
  @indexes
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



7
8
9
# File 'lib/mimi/db/dictate/schema_definition.rb', line 7

def table_name
  @table_name
end

Instance Method Details

#field(name, opts) ⇒ Object

Declares a field

Example:

field :s1, as: :string
field :s2, type: :string, size: 64
field :v1, as: :decimal, size: 10      # precision: 10
field :v2, as: :decimal, size: [10, 3] # precision: 10, scale: 3


24
25
26
27
28
29
30
31
32
# File 'lib/mimi/db/dictate/schema_definition.rb', line 24

def field(name, opts)
  name = name.to_sym
  raise "Cannot redefine field :#{name}" if @columns[name]
  opts_type = opts[:type] || opts[:as]
  if primary_key && (opts[:primary_key] || opts_type == :primary_key)
    raise "Cannot redefine primary key (:#{primary_key.name}) with :#{name}"
  end
  @columns[name] = Column.new(name, opts)
end

#index(columns, opts) ⇒ Object

Declares an index

Example:

index :name
index [:first_name, :last_name]
index :ref_code, unique: true

Parameters:

  • columns (String, Symbol, Array<String,Symbol>)

    columns to index on

  • opts (Hash)

    index parameters (:unique, :name etc)



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mimi/db/dictate/schema_definition.rb', line 44

def index(columns, opts)
  case columns
  when String, Symbol
    columns = [columns.to_sym]
  when Array
    unless columns.all? { |c| c.is_a?(String) || c.is_a?(Symbol) }
      raise "Invalid column reference in index definition [#{columns}]"
    end
    columns = columns.map(&:to_sym)
  else
    raise 'Invalid columns argument to .index'
  end
  if columns == [primary_key.name]
    # TODO: warn the primary key index is ignored
  end
  @indexes << Index.new(columns, opts)
end

#primary_keyMimi::DB::Dictate::SchemaDefinition::Column

Returns primary key column



66
67
68
69
70
# File 'lib/mimi/db/dictate/schema_definition.rb', line 66

def primary_key
  pk = columns.values.find { |c| c.params[:primary_key] }
  # raise "Primary key is not defined on '#{table_name}'" unless pk
  pk
end

#to_hObject



72
73
74
75
76
77
78
# File 'lib/mimi/db/dictate/schema_definition.rb', line 72

def to_h
  {
    table_name: table_name,
    columns: columns.values.map(&:to_h),
    indexes: indexes.map(&:to_h)
  }
end