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



34
35
36
37
38
39
40
41
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 34

def description
  type = @type.class == Array ? "Array#{@type}" : @type
  str = "have attribute #{@name}"
  str += " of type #{type}#{coercer_description}" unless @type.nil?
  str += " with default \"#{@expected_default}\"" if @expected_default
  str += " and is strict" if @expected_strict
  str
end

#failure_messageObject



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

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

#matches?(klass) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 26

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

  valid_type && valid_coercer && valid_default && matches_strict
end

#strictObject



21
22
23
24
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 21

def strict
  @expected_strict = true
  self
end

#with_default(expected_default) ⇒ Object



16
17
18
19
# File 'lib/virtus/matchers/have_attribute_matcher.rb', line 16

def with_default(expected_default)
  @expected_default = expected_default
  self
end