class Mongoid::Matchers::Document::HaveFieldMatcher

Attributes

default[R]
errors[R]
fields[R]
klass[R]
type[R]

Public Class Methods

new(*fields) click to toggle source
# File lib/matchers/document/have_field.rb, line 15
def initialize *fields
  @fields = fields.collect(&:to_s)
  @errors = []
end

Public Instance Methods

description() click to toggle source
# File lib/matchers/document/have_field.rb, line 64
def description
  desc = "have #{fields.size > 1 ? 'fields' : 'field'} named"
  desc << " #{to_sentence(fields)}"
  desc << " of type #{type.inspect}" if type
  desc << " with default value of #{default.inspect}" if !default.nil?
  desc
end
failure_message() click to toggle source
# File lib/matchers/document/have_field.rb, line 55
def failure_message
  "#{klass} to #{description}, got #{errors.to_sentence}"
end
matches?(subject) click to toggle source
# File lib/matchers/document/have_field.rb, line 30
def matches? subject
  @klass = class_of subject

  fields.each do |field|
    if klass.fields.include? field
      error = ''
      result_field = klass.fields[field]

      if check_type_with result_field.type
        error << " of type #{result_field.type.inspect}"
      end

      if check_default_with result_field.default_val
        error << " with default value of #{result_field.default_val.inspect}"
      end

      errors << "field #{field.inspect << error}" if !error.blank?
    else
      errors << "no field named #{field.inspect}"
    end
  end

  errors.empty?
end
negative_failure_message() click to toggle source
# File lib/matchers/document/have_field.rb, line 59
def negative_failure_message
  msg = "#{klass.inspect} to not #{description}, "
  msg << "got #{klass.inspect} to #{description}"
end
of_type(type) click to toggle source
# File lib/matchers/document/have_field.rb, line 20
def of_type type
  @type = type
  self
end
with_default_value(default) click to toggle source
# File lib/matchers/document/have_field.rb, line 25
def with_default_value default
  @default = default
  self
end

Private Instance Methods

check_default_with(_default) click to toggle source
# File lib/matchers/document/have_field.rb, line 78
def check_default_with _default
  !default.nil? && !_default.nil? && _default != default
end
check_type_with(_type) click to toggle source
# File lib/matchers/document/have_field.rb, line 74
def check_type_with _type
  type && _type != type
end