class RSpec::Virtus::Matcher

Public Class Methods

new(attribute_name) click to toggle source
# File lib/rspec_virtus/matcher.rb, line 4
def initialize(attribute_name)
  @attribute_name = attribute_name
  @options = {}
end

Public Instance Methods

description() click to toggle source
# File lib/rspec_virtus/matcher.rb, line 9
def description
  msg = "have attribute #{@attribute_name}"
  msg << ", #{@options[:type]}" if @options[:type]
  msg << ", default: #{@options[:default_value]}" if @options[:default_value]
  msg
end
failure_message() click to toggle source
# File lib/rspec_virtus/matcher.rb, line 37
def failure_message
  "should #{@attribute_name} to be defined"
end
failure_message_when_negated() click to toggle source
# File lib/rspec_virtus/matcher.rb, line 41
def failure_message_when_negated
  "should #{@attribute_name} not to be defined"
end
matches?(instance) click to toggle source
# File lib/rspec_virtus/matcher.rb, line 31
def matches?(instance)
  @instance = instance
  @subject = instance.class
  attribute_exists? && type_correct? && default_value_correct? && required?
end
of_type(type) click to toggle source
# File lib/rspec_virtus/matcher.rb, line 16
def of_type(type)
  @options[:type] = type
  self
end
with_default(default_value) click to toggle source
# File lib/rspec_virtus/matcher.rb, line 21
def with_default(default_value)
  @options[:default_value] = default_value
  self
end
with_required(value) click to toggle source
# File lib/rspec_virtus/matcher.rb, line 26
def with_required(value)
  @options[:required] = value
  self
end

Private Instance Methods

attribute() click to toggle source
# File lib/rspec_virtus/matcher.rb, line 47
def attribute
  @subject.attribute_set[@attribute_name]
end
attribute_default_value() click to toggle source
# File lib/rspec_virtus/matcher.rb, line 63
def attribute_default_value
  value = attribute.default_value.value

  case value
  when ::Proc
    value.call(@instance, attribute)
  when ::Symbol
    @instance.__send__(value)
  else
    value
  end
end
attribute_exists?() click to toggle source
# File lib/rspec_virtus/matcher.rb, line 59
def attribute_exists?
  !attribute.nil?
end
attribute_type() click to toggle source
# File lib/rspec_virtus/matcher.rb, line 55
def attribute_type
  attribute.primitive
end
default_value_correct?() click to toggle source
# File lib/rspec_virtus/matcher.rb, line 86
def default_value_correct?
  return true unless @options[:default_value]
  attribute_default_value == @options[:default_value]
end
member_type() click to toggle source
# File lib/rspec_virtus/matcher.rb, line 51
def member_type
  attribute.member_type.primitive
end
required?() click to toggle source
# File lib/rspec_virtus/matcher.rb, line 91
def required?
  return true if @options[:required].nil?
  attribute.required? == @options[:required]
end
type_correct?() click to toggle source
# File lib/rspec_virtus/matcher.rb, line 76
def type_correct?
  if @options[:type].is_a?(::Array)
    attribute_type == Array && member_type == @options[:type].first
  elsif @options[:type]
    attribute_type == @options[:type]
  else
    true
  end
end