Class: HoboFields::FieldSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/hobo_fields/field_spec.rb

Defined Under Namespace

Classes: UnknownSqlTypeError

Constant Summary collapse

TYPE_SYNONYMS =
[[:timestamp, :datetime]]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, type, options = {}) ⇒ FieldSpec

Returns a new instance of FieldSpec.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'lib/hobo_fields/field_spec.rb', line 7

def initialize(model, name, type, options={})
  raise ArgumentError, "you cannot provide a field spec for the primary key" if name == model.primary_key
  self.model = model
  self.name = name.to_sym
  self.type = type.is_a?(String) ? type.to_sym : type
  position = options.delete(:position)
  self.options = options
  self.position = position || model.field_specs.length
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



17
18
19
# File 'lib/hobo_fields/field_spec.rb', line 17

def model
  @model
end

#nameObject

Returns the value of attribute name.



17
18
19
# File 'lib/hobo_fields/field_spec.rb', line 17

def name
  @name
end

#optionsObject

Returns the value of attribute options.



17
18
19
# File 'lib/hobo_fields/field_spec.rb', line 17

def options
  @options
end

#positionObject

Returns the value of attribute position.



17
18
19
# File 'lib/hobo_fields/field_spec.rb', line 17

def position
  @position
end

#typeObject

Returns the value of attribute type.



17
18
19
# File 'lib/hobo_fields/field_spec.rb', line 17

def type
  @type
end

Instance Method Details

#commentObject



64
65
66
# File 'lib/hobo_fields/field_spec.rb', line 64

def comment
  options[:comment]
end

#defaultObject



60
61
62
# File 'lib/hobo_fields/field_spec.rb', line 60

def default
  options[:default]
end

#different_to?(col_spec) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hobo_fields/field_spec.rb', line 79

def different_to?(col_spec)
  !same_type?(col_spec) ||
    # we should be able to use col_spec.comment, but col_spec has
    # a nil table_name for some strange reason.
    begin
      if model.table_exists?
        col_comment = ActiveRecord::Base.try.column_comment(col_spec.name, model.table_name) 
        col_comment != nil && col_comment != comment
      else
        false
      end
    end ||
    begin
      check_attributes = [:null, :default]
      check_attributes += [:precision, :scale] if sql_type == :decimal && !col_spec.is_a?(SQLITE_COLUMN_CLASS)  # remove when rails fixes https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2872
      check_attributes -= [:default] if sql_type == :text && col_spec.is_a?(MYSQL_COLUMN_CLASS)
      check_attributes << :limit if sql_type.in?([:string, :text, :binary, :integer])
      check_attributes.any? do |k|
        if k==:default && sql_type==:datetime
          col_spec.default.try.to_datetime != default.try.to_datetime
        else
          col_spec.send(k) != self.send(k)
        end
      end
    end
end

#limitObject



44
45
46
# File 'lib/hobo_fields/field_spec.rb', line 44

def limit
  options[:limit] || native_types[sql_type][:limit]
end

#nullObject



56
57
58
# File 'lib/hobo_fields/field_spec.rb', line 56

def null
  :null.in?(options) ? options[:null] : true
end

#precisionObject



48
49
50
# File 'lib/hobo_fields/field_spec.rb', line 48

def precision
  options[:precision]
end

#same_type?(col_spec) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
# File 'lib/hobo_fields/field_spec.rb', line 68

def same_type?(col_spec)
  t = sql_type
  TYPE_SYNONYMS.each do |synonyms|
    if t.in? synonyms
      return col_spec.type.in?(synonyms)
    end
  end
  t == col_spec.type
end

#scaleObject



52
53
54
# File 'lib/hobo_fields/field_spec.rb', line 52

def scale
  options[:scale]
end

#sql_typeObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/hobo_fields/field_spec.rb', line 33

def sql_type
  options[:sql_type] or begin
                          if native_type?(type)
                            type
                          else
                            field_class = HoboFields.to_class(type)
                            field_class && field_class::COLUMN_TYPE or raise UnknownSqlTypeError, "#{type.inspect} for #{model}.#{name}"
                          end
                        end
end