Sha256: cf09a1578a34769121c63b0a7125cad00b5207bb190420137ace6322dfd1989e

Contents?: true

Size: 823 Bytes

Versions: 6

Compression:

Stored size: 823 Bytes

Contents

module Nasty
  module Specification
    def or(other_predicate = nil, &block)
      matcher = create_predicate(other_predicate, &block)
      create_predicate { |item| self.matches?(item) || matcher.matches?(item) }
    end

    def and(other_predicate = nil, &block)
      matcher = create_predicate(other_predicate, &block)
      create_predicate { |item| self.matches?(item) && matcher.matches?(item) }
    end

    def not
      create_predicate { |item| !self.matches?(item) }
    end

    private

    def create_predicate(predicate = nil, &block)
      block_given? ? Nasty::BlockSpecification.new(&block) : predicate
    end
  end

  class BlockSpecification
    include Nasty::Specification

    def initialize(&block)
      @block = block
    end

    def matches?(item)
      @block.call(item)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
nasty-0.0.1395464273 lib/nasty/block_specification.rb
nasty-0.0.1388168019 lib/nasty/block_specification.rb
nasty-0.0.1388167257 lib/nasty/block_specification.rb
nasty-0.0.1388166944 lib/nasty/block_specification.rb
nasty-0.0.1388166636 lib/nasty/block_specification.rb
nasty-0.0.1388165564 lib/nasty/block_specification.rb