Sha256: 4339297c9ce4186eec4edfa47b5f6f82f8cfcdd277a8cc57b2d7e3b517c51fc2

Contents?: true

Size: 1.11 KB

Versions: 57

Compression:

Stored size: 1.11 KB

Contents

module HammerCLI
  module Options

    class Matcher

      def initialize(filter)
        @filter = filter
      end

      def matches?(option)
        @filter.each do |attribute, filter|
          return false if !attribute_matches?(option, attribute, filter)
        end
        return true
      end

      protected

      def attribute_matches?(option, attribute, filter)
        if filter.is_a? Array
          return filter.any? {|filter_part| attribute_matches?(option, attribute, filter_part)}
        elsif filter.is_a? Regexp
          return attribute_matches_regexp?(option, attribute, filter)
        else
          return attribute_matches_value?(option, attribute, filter)
        end
      end

      def attribute_matches_value?(option, attribute, filter)
        get_attribute_value(option, attribute) == filter
      end

      def attribute_matches_regexp?(option, attribute, filter)
        get_attribute_value(option, attribute) =~ filter
      end

      def get_attribute_value(option, attribute_name)
        option.send(attribute_name)
      rescue NoMethodError
        nil
      end

    end


  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
hammer_cli-3.14.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.13.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.12.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.10.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.9.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.8.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.7.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.5.1 lib/hammer_cli/options/matcher.rb
hammer_cli-3.6.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.5.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.4.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.3.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.2.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.1.1 lib/hammer_cli/options/matcher.rb
hammer_cli-3.0.2 lib/hammer_cli/options/matcher.rb
hammer_cli-3.1.0 lib/hammer_cli/options/matcher.rb
hammer_cli-3.0.1 lib/hammer_cli/options/matcher.rb
hammer_cli-3.0.0 lib/hammer_cli/options/matcher.rb
hammer_cli-2.5.1 lib/hammer_cli/options/matcher.rb
hammer_cli-2.5.0 lib/hammer_cli/options/matcher.rb