Class: DataType::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl/data_type.rb

Direct Known Subclasses

Currency, Date, Fixnum, Float, Object, Polymorphic, Range, String, Symbol, TrueClass

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

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



9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



5
6
7
# File 'lib/dsl/data_type.rb', line 5

def aliases
  @aliases
end

Class Method Details

.default_mockObject

Default mock should be overridden in derived classes



41
42
43
# File 'lib/dsl/data_type.rb', line 41

def self.default_mock
  short_text_mock
end

.long_text_mockObject



45
46
47
# 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?Boolean

Returns:

  • (Boolean)


81
# File 'lib/dsl/data_type.rb', line 81

def self.migrant_data_type?; true; end

.short_text_mockObject



49
50
51
# File 'lib/dsl/data_type.rb', line 49

def self.short_text_mock
  Faker::Lorem.sentence
end

Instance Method Details

#==(compared_column) ⇒ Object



27
28
29
30
# 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

#columnObject



23
24
25
# File 'lib/dsl/data_type.rb', line 23

def column
  column_defaults.merge(@options)
end

#column_default_changed?(old_default, new_default) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# 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_defaultsObject

Default is ‘ye good ol varchar(255)



19
20
21
# File 'lib/dsl/data_type.rb', line 19

def column_defaults 
  { :type => :string }
end

#dangerous_migration_from?(current_structure = nil) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# 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

#mockObject



32
33
34
# File 'lib/dsl/data_type.rb', line 32

def mock      
  @value || self.class.default_mock
end

#serialized?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/dsl/data_type.rb', line 36

def serialized?
  false
end

#structure_changes_from(current_structure = nil) ⇒ Object

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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