Sha256: 31a8ea418e996ed38516b5860805142afa67479a4603423748cd73035c4637e3

Contents?: true

Size: 1.85 KB

Versions: 38

Compression:

Stored size: 1.85 KB

Contents

module MuckEngine # :nodoc:
  module Models # :nodoc:
    module Matchers
      
      # 'newer_than' named scope which retrieves items newer than given date
      # requires that the class have a factory
      # Tests:
      #   scope :newer_than, lambda { |time| {:conditions => ["created_at < ?", time || 1.day.ago] } }
      # Examples:
      #   it { should scope_recent }
      def scope_newer_than
        TimeMatcher.new(:newer_than)
      end

      # 'scope_older_than' named scope which retrieves items older than the given date
      # requires that the class have a factory
      # Tests:
      #   scope :newer_than, lambda { |time| {:conditions => ["created_at < ?", time || 1.day.ago] } }
      # Examples:
      #   it { should scope_recent }
      def scope_older_than
        TimeMatcher.new(:older_than)
      end
            
      class TimeMatcher < MuckMatcherBase # :nodoc:

        def initialize(scope, field = :created_at)
          @scope = scope
          @field = field
        end
        
        def matches?(subject)
          @subject = subject
          @subject.class.delete_all
          old_item = Factory(factory_name, @field => 1.month.ago)
          new_item = Factory(factory_name, @field => 1.day.ago)
          items = @subject.class.send(@scope, 1.week.ago)          
          if @scope == :newer_than
            items.include?(new_item) && !items.include?(old_item)
          elsif @scope == :older_than
            !items.include?(new_item) && items.include?(old_item)
          else
            raise "matcher not implemented for #{@scope}"
          end
        end
        
        def failure_message
          "Expected #{factory_name} to scope by #{@scope} on #{@field} and be successful. But the call failed"
        end
        
        def description
          "items scoped #{@scope}"
        end
          
      end

    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
muck-engine-3.5.0 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.4.0 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.18 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.17 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.16 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.15 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.14 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.13 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.12 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.11 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.10 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.9 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.8 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.7 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.6 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.5 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.4 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.3 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.2 lib/muck-engine/test/models/matchers/scope_time_matchers.rb
muck-engine-3.3.1 lib/muck-engine/test/models/matchers/scope_time_matchers.rb