Sha256: ca4fef886c6571349e8ada24192d92e9d14c9abd2f9ce0f9411278261523d617

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module CQL

  class TagFilter
    attr_reader :tags

    def initialize tags
      @tags = tags
    end

    def has_tags?(object, tags)
      tags.all? { |tag| object.tags.include?(tag) }
    end

    def execute objects
      objects.find_all { |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
      input.find_all do |object|
        type_count(object).send(comparison.operator, comparison.amount)
      end
    end

  end

  class NameFilter < ContentMatchFilter

    def execute(input)
      input.find_all 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.1.0 lib/cql/filters.rb
cql-1.0.1 lib/cql/filters.rb