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.



89
90
91
92
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 89

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

Instance Method Details

#descriptionObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 130

def description
  desc = "have db column named #{@column}"
  if @options.key?(:column_type)
    desc << " of type #{@options[:column_type]}"
  end
  if @options.key?(:precision)
    desc << " of precision #{@options[:precision]}"
  end
  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



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

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

#failure_message_when_negatedObject



126
127
128
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 126

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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 109

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

#of_type(column_type) ⇒ Object



94
95
96
97
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 94

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

#with_options(opts = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 99

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