Class: Mimi::DB::Dictate::SchemaDefinition
- Inherits:
-
Object
- Object
- Mimi::DB::Dictate::SchemaDefinition
- Defined in:
- lib/mimi/db/dictate/schema_definition.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#indexes ⇒ Object
readonly
Returns the value of attribute indexes.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Instance Method Summary collapse
-
#field(name, opts) ⇒ Object
Declares a field.
-
#index(columns, opts) ⇒ Object
Declares an index.
-
#initialize(table_name) ⇒ SchemaDefinition
constructor
A new instance of SchemaDefinition.
-
#primary_key ⇒ Mimi::DB::Dictate::SchemaDefinition::Column
Returns primary key column.
- #to_h ⇒ Object
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
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
7 8 9 |
# File 'lib/mimi/db/dictate/schema_definition.rb', line 7 def columns @columns end |
#indexes ⇒ Object (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_name ⇒ Object (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
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_key ⇒ Mimi::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_h ⇒ Object
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 |