class DataType::Base

Attributes

aliases[RW]

Public Class Methods

default_mock() click to toggle source

Default mock should be overridden in derived classes

# File lib/dsl/data_type.rb, line 41
def self.default_mock
  short_text_mock
end
long_text_mock() click to toggle source
# File lib/dsl/data_type.rb, line 45
def self.long_text_mock    
  (1..3).to_a.collect { Faker::Lorem.paragraph }.join("\n")
end
migrant_data_type?() click to toggle source
# File lib/dsl/data_type.rb, line 81
def self.migrant_data_type?; true; end
new(options={}) click to toggle source

Pass the developer's ActiveRecord::Base structure and we'll decide the best structure

# File lib/dsl/data_type.rb, line 9
def initialize(options={})
  @options = options
  @value = options.delete(:value)
  @example = options.delete(:example)
  @field = options.delete(:field)
  @aliases = options.delete(:was) || ::Array.new
  options[:type] = options.delete(:as) if options[:as] # Nice little DSL alias for 'type'
end
short_text_mock() click to toggle source
# File lib/dsl/data_type.rb, line 49
def self.short_text_mock
  Faker::Lorem.sentence
end

Public Instance Methods

==(compared_column) click to toggle source
# File lib/dsl/data_type.rb, line 27
def ==(compared_column)
  # Ideally we should compare attributes, but unfortunately not all drivers report enough statistics for this
  column[:type] == compared_column[:type]
end
column() click to toggle source
# File lib/dsl/data_type.rb, line 23
def column
  column_defaults.merge(@options)
end
column_default_changed?(old_default, new_default) click to toggle source
# File lib/dsl/data_type.rb, line 77
def column_default_changed?(old_default, new_default)
  new_default.to_s != old_default.to_s
end
column_defaults() click to toggle source

Default is 'ye good ol varchar(255)

# File lib/dsl/data_type.rb, line 19
def column_defaults 
  { :type => :string }
end
dangerous_migration_from?(current_structure = nil) click to toggle source
# File lib/dsl/data_type.rb, line 73
def dangerous_migration_from?(current_structure = nil)
  current_structure && (column[:type] != :text && [:string, :text].include?(current_structure[:type]) && column[:type] != current_structure[:type])
end
mock() click to toggle source
# File lib/dsl/data_type.rb, line 32
def mock      
  @value || self.class.default_mock
end
serialized?() click to toggle source
# File lib/dsl/data_type.rb, line 36
def serialized?
  false
end
structure_changes_from(current_structure = nil) click to toggle source

Decides if and how a column will be changed Provide the details of a previously column, or simply nil to create a new column

# File lib/dsl/data_type.rb, line 55
def structure_changes_from(current_structure = nil)
  new_structure = column
 
  if current_structure
    # General RDBMS data loss scenarios
    if new_structure[:limit] && current_structure[:limit].to_i != new_structure[:limit].to_i ||
       new_structure[:type] != current_structure[:type] ||
       !new_structure[:default].nil? && column_default_changed?(current_structure[:default], new_structure[:default])

       column
    else
      nil # No changes
    end
  else
    column
  end
end