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



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

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

Instance Method Details

#descriptionObject



76
77
78
79
80
81
82
# File 'lib/matchers/document.rb', line 76

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

#failure_message_for_shouldObject Also known as: failure_message



65
66
67
# File 'lib/matchers/document.rb', line 65

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

#failure_message_for_should_notObject Also known as: failure_message_when_negated



69
70
71
# File 'lib/matchers/document.rb', line 69

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

#localizedObject



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

def localized
  @localized = true
  self
end

#matches?(klass) ⇒ Boolean



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/matchers/document.rb', line 28

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?
        if @klass.fields[attr].default_val.nil?
          error << " with default not set"
        elsif @klass.fields[attr].default_val != @default
          error << " with default value of #{@klass.fields[attr].default_val}"
        end
      end

      if @field_alias and @klass.fields[attr].options[:as] != @field_alias
        error << " with alias #{@klass.fields[attr].options[:as]}"
      end

      @errors.push("field #{attr.inspect}" << error) unless error.blank?

      if @localized
        unless @klass.fields[attr].localized?
          @errors.push "is not localized #{attr.inspect}"
        end
      end

    else
      @errors.push "no field named #{attr.inspect}"
    end
  end
  @errors.empty?
end

#of_type(type) ⇒ Object



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

def of_type(type)
  @type = type
  self
end

#with_alias(field_alias) ⇒ Object



18
19
20
21
# File 'lib/matchers/document.rb', line 18

def with_alias(field_alias)
  @field_alias = field_alias
  self
end

#with_default_value_of(default) ⇒ Object



23
24
25
26
# File 'lib/matchers/document.rb', line 23

def with_default_value_of(default)
  @default = default
  self
end