Class: Storext::Matchers::HaveAttributeMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/storext/matchers/have_attribute_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil) ⇒ HaveAttributeMatcher

Returns a new instance of HaveAttributeMatcher.



4
5
6
7
# File 'lib/storext/matchers/have_attribute_matcher.rb', line 4

def initialize(name, type = nil)
  @name = name
  @expected_type = type
end

Instance Method Details

#descriptionObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/storext/matchers/have_attribute_matcher.rb', line 30

def description
  messages = ["have attribute #{@name}"]
  messages << ["of type #{@expected_type}"]
  if @expected_column
    messages << [%Q(in column "#{@expected_column}")]
  end
  if @expected_default
    messages << [%Q(with default "#{@expected_default}")]
  end

  messages.join(" ")
end

#failure_messageObject



43
44
45
# File 'lib/storext/matchers/have_attribute_matcher.rb', line 43

def failure_message
  "expected #{@klass} to #{description}"
end

#in_column(column) ⇒ Object



9
10
11
12
# File 'lib/storext/matchers/have_attribute_matcher.rb', line 9

def in_column(column)
  @expected_column = column
  self
end

#matches?(class_or_record) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
# File 'lib/storext/matchers/have_attribute_matcher.rb', line 19

def matches?(class_or_record)
  @klass = class_or_record.is_a?(ActiveRecord::Base) ?
    class_or_record.class : class_or_record
  definition = @klass.storext_definitions[@name]

  definition.present? &&
    matches_type?(definition[:type]) &&
    matches_column?(definition[:column]) &&
    matches_default?(definition[:opts][:default])
end

#with_default(expected_default) ⇒ Object



14
15
16
17
# File 'lib/storext/matchers/have_attribute_matcher.rb', line 14

def with_default(expected_default)
  @expected_default = expected_default
  self
end