Class: Virtus::Matchers::HaveAttributeMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(name, type) ⇒ HaveAttributeMatcher

Returns a new instance of HaveAttributeMatcher.



6
7
8
9
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 6

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

Instance Method Details

#descriptionObject



23
24
25
26
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 23

def description
  type = @type.class == Array ? "Array#{@type}" : @type
  "have attribute #{@name} of type #{type}"
end

#failure_messageObject



28
29
30
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 28

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

#matches?(klass) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 11

def matches?(klass)
  @klass = klass
  attribute = @klass.attribute_set[@name]
  return false unless attribute
  if @type.class == Array
    attribute.primitive == Array &&
      attribute.options[:member_type].primitive == @type.first
  else
    attribute.primitive == @type
  end
end