Sha256: 970921235082e26915ff15f0033c82338784ff262144b486d415b3649120f7b7

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

module CQL

  class TagFilter
    attr_reader :tags

    def initialize tags
      @tags = tags
    end

    def has_tags?(object, target_tags)
      target_tags.all? { |target_tag|
        tags = object.tags
        tags = tags.collect { |tag| tag.name } unless Gem.loaded_specs['cuke_modeler'].version.version[/^0/]
        tags.include?(target_tag)
      }
    end

    def execute(objects, negate)
      method = negate ? :reject : :select

      objects.send(method) { |object| has_tags?(object, tags) }
    end

  end

  class ContentMatchFilter
    attr_reader :pattern

    def initialize(pattern)
      raise(ArgumentError, "Can only match a String or Regexp. Got #{pattern.class}.") unless pattern.is_a?(String) || pattern.is_a?(Regexp)

      @pattern = pattern
    end

    def content_match?(content)
      if pattern.is_a?(String)
        content.any? { |thing| thing == pattern }
      else
        content.any? { |thing| thing =~ pattern }
      end
    end

  end

  class TypeCountFilter
    attr_reader :types, :comparison

    def initialize types, comparison
      @types = types
      @comparison = comparison
    end

    def execute(input, negate)
      method = negate ? :reject : :select

      input.send(method) do |object|
        type_count(object).send(comparison.operator, comparison.amount)
      end
    end

  end

  class NameFilter < ContentMatchFilter

    def execute(input, negate)
      method = negate ? :reject : :select

      input.send(method) do |object|
        content_match?([object.name])
      end
    end

  end

  class TagCountFilter < TypeCountFilter

    def type_count(test)
      test.tags.size
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cql-1.4.0 lib/cql/filters.rb
cql-1.3.0 lib/cql/filters.rb