Class: Shoulda::Matchers::ActiveRecord::HaveDbColumnMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/active_record/have_db_column_matcher.rb

Defined Under Namespace

Classes: DecoratedColumn

Constant Summary collapse

OPTIONS =
%i(precision limit default null scale primary array).freeze

Instance Method Summary collapse

Constructor Details

#initialize(column) ⇒ HaveDbColumnMatcher

Returns a new instance of HaveDbColumnMatcher.



113
114
115
116
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 113

def initialize(column)
  @column = column
  @options = {}
end

Instance Method Details

#descriptionObject



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 160

def description
  desc = "have db column named #{@column}"
  desc << " of type #{@options[:column_type]}" if @options.key?(:column_type)
  desc << " of sql_type #{@options[:sql_column_type]}" if @options.key?(:sql_column_type)
  desc << " of precision #{@options[:precision]}" if @options.key?(:precision)
  desc << " of limit #{@options[:limit]}" if @options.key?(:limit)
  desc << " of default #{@options[:default]}" if @options.key?(:default)
  desc << " of null #{@options[:null]}" if @options.key?(:null)
  desc << " of primary #{@options[:primary]}" if @options.key?(:primary)
  desc << " of scale #{@options[:scale]}" if @options.key?(:scale)
  desc
end

#failure_messageObject



152
153
154
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 152

def failure_message
  "Expected #{expectation} (#{@missing})"
end

#failure_message_when_negatedObject



156
157
158
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 156

def failure_message_when_negated
  "Did not expect #{expectation}"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 138

def matches?(subject)
  @subject = subject
  column_exists? &&
    correct_column_type? &&
    correct_sql_column_type? &&
    correct_precision? &&
    correct_limit? &&
    correct_default? &&
    correct_null? &&
    correct_scale? &&
    correct_primary? &&
    correct_array?
end

#of_sql_type(sql_column_type) ⇒ Object



123
124
125
126
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 123

def of_sql_type(sql_column_type)
  @options[:sql_column_type] = sql_column_type
  self
end

#of_type(column_type) ⇒ Object



118
119
120
121
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 118

def of_type(column_type)
  @options[:column_type] = column_type
  self
end

#with_options(opts = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 128

def with_options(opts = {})
  validate_options(opts)
  OPTIONS.each do |attribute|
    if opts.key?(attribute.to_sym)
      @options[attribute.to_sym] = opts[attribute.to_sym]
    end
  end
  self
end