Class: Mongoid::Matchers::HaveFieldMatcher

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(*attrs) ⇒ HaveFieldMatcher

Returns a new instance of HaveFieldMatcher.



4
5
6
# File 'lib/matchers/document.rb', line 4

def initialize(*attrs)
  @attributes = attrs.collect(&:to_s)
end

Instance Method Details

#descriptionObject



48
49
50
51
52
53
# File 'lib/matchers/document.rb', line 48

def description
  desc = "have #{@attributes.size > 1 ? 'fields' : 'field'} named #{@attributes.collect(&:inspect).to_sentence}"
  desc << " of type #{@type.inspect}" if @type
  desc << " with default value of #{@default.inspect}" if !@default.nil?
  desc
end

#failure_message_for_shouldObject



40
41
42
# File 'lib/matchers/document.rb', line 40

def failure_message_for_should
  "Expected #{@klass.inspect} to #{description}, got #{@errors.to_sentence}"
end

#failure_message_for_should_notObject



44
45
46
# File 'lib/matchers/document.rb', line 44

def failure_message_for_should_not
  "Expected #{@klass.inspect} to not #{description}, got #{@klass.inspect} to #{description}"
end

#matches?(klass) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/matchers/document.rb', line 18

def matches?(klass)
  @klass = klass.is_a?(Class) ? klass : klass.class
  @errors = []
  @attributes.each do |attr|
    if @klass.fields.include?(attr)
      error = ""
      if @type and @klass.fields[attr].type != @type
        error << " of type #{@klass.fields[attr].type}"
      end
      
      if !@default.nil? and !@klass.fields[attr].default.nil? and @klass.fields[attr].default != @default
        error << " with default value of #{@klass.fields[attr].default}"
      end

      @errors.push("field #{attr.inspect}" << error) unless error.blank?
    else
      @errors.push "no field named #{attr.inspect}"
    end
  end
  @errors.empty?
end

#of_type(type) ⇒ Object



8
9
10
11
# File 'lib/matchers/document.rb', line 8

def of_type(type)
  @type = type
  self
end

#with_default_value_of(default) ⇒ Object



13
14
15
16
# File 'lib/matchers/document.rb', line 13

def with_default_value_of(default)
  @default = default
  self
end