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 = nil) ⇒ 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 = nil)
  @name = name
  @type = type
end

Instance Method Details

#coerced_with(custom_coercer) ⇒ Object



11
12
13
14
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 11

def coerced_with(custom_coercer)
  @custom_coercer = custom_coercer
  self
end

#descriptionObject



29
30
31
32
33
34
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 29

def description
  type = @type.class == Array ? "Array#{@type}" : @type
  str = "have attribute #{@name}"
  str += " of type #{type}#{coercer_description}" unless @type.nil?
  str
end

#failure_messageObject



36
37
38
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 36

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

#matches?(klass) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 16

def matches?(klass)
  @klass = klass
  @attribute = @klass.attribute_set[@name]
  return false unless @attribute

  if @type.class == Array
    return @attribute.primitive == Array &&
      @attribute.options[:member_type].primitive == @type.first
  end

  valid_type && valid_coercer
end