Sha256: 71a5f70425d77c199fddb4576e352230663b0e85c514ef309b217c8c791e8d99

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

module Remarkable::Mongoid
  module Matchers
    def reference_one(attr)
      AssociationMatcher.new(attr, ::Mongoid::Associations::ReferencesOne)
    end
    
    def reference_many(attr)
      AssociationMatcher.new(attr, ::Mongoid::Associations::ReferencesMany)
    end

    def reference_many_as_array(attr)
      AssociationMatcher.new(attr, ::Mongoid::Associations::ReferencesManyAsArray)
    end
    
    def be_referenced_in(attr)
      AssociationMatcher.new(attr, ::Mongoid::Associations::ReferencedIn)
    end

    def embed_one(attr)
      AssociationMatcher.new(attr, ::Mongoid::Associations::EmbedsOne)
    end
    
    def embed_many(attr)
      AssociationMatcher.new(attr, ::Mongoid::Associations::EmbedsMany)
    end
    
    def be_embedded_in(attr)
      AssociationMatcher.new(attr, ::Mongoid::Associations::EmbeddedIn)
    end
    
    class AssociationMatcher
      attr_accessor :attr, :association_type

      def initialize(attr, association_type)
        self.attr             = attr.to_s
        self.association_type = association_type
      end

      def matches?(subject)
        @subject     = subject
        associations = @subject.associations.select { |k,v| v.association == association_type }
        associations.detect { |k| k.first == attr } != nil
      end

      def description
        "has #{humanized_association} association :#{attr}"
      end
      
      def failure_message_for_should
        "\n#{humanized_association} association failure\nExpected: '#{attr}'"
      end
      
      private
      
      def humanized_association
        association_type.to_s.split('::').last
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
remarkable_mongoid-0.5.0 lib/remarkable/mongoid/associations.rb
remarkable_mongoid-0.4.0 lib/remarkable/mongoid/associations.rb
remarkable_mongoid-0.3.0 lib/remarkable/mongoid/associations.rb