class Accessibility::Qualifier

Used in searches to answer whether or not a given element meets the expected criteria.

Constants

TRANSLATOR

@private @return [Accessibility::Translator]

Public Class Methods

new(klass, criteria) click to toggle source

Initialize a qualifier with the kind of object that you want to qualify and a dictionary of filter criteria. You can optionally pass a block if your qualification criteria is too complicated for key/value pairs; the blocks return value will be used to determine if an element qualifies.

@example

Accessibility::Qualifier.new(:standard_window, title: 'Test')
Accessibility::Qualifier.new(:buttons, {})
Accessibility::Qualifier.new(:Table, { row: { title: /Price/ } })
Accessibility::Qualifier.new(:element) do |element|
  element.children.size > 5 && NSContainsRect(element.bounds, rect)
end

@param klass [#to_s] @param criteria [Hash] @yield Optional block that can qualify an element

# File lib/accessibility/qualifier.rb, line 29
def initialize klass, criteria
  @klass    = TRANSLATOR.classify(klass)
  @criteria = criteria
  @block    = Proc.new if block_given?
  compile!
end

Public Instance Methods

describe() click to toggle source

Return a compact description of the qualifier. If the qualifier includes a block then a checkmarked box will be included.

@return [String]

# File lib/accessibility/qualifier.rb, line 50
def describe
  "#{@klass}#{@criteria.ax_pp}#{@block ? '[✔]' : ''}"
end
qualifies?(element) click to toggle source

Whether or not a candidate object matches the criteria given at initialization.

@param element [AX::Element]

# File lib/accessibility/qualifier.rb, line 41
def qualifies? element
  the_right_type?(element) && meets_criteria?(element)
end

Private Instance Methods

block_check(element) click to toggle source
# File lib/accessibility/qualifier.rb, line 135
def block_check element
  @block.call element
end
compile!() click to toggle source

Take a hash of search filters and generate an optimized search array. This is done to avoid checking types for each call to {#qualifies?}.

@return [void]

# File lib/accessibility/qualifier.rb, line 67
def compile!
  @filters = @criteria.map do |key, value|
    if value.kind_of? Hash
      [:subsearch, key, value]
    elsif key.kind_of? Array
      filter = value.kind_of?(Regexp) ?
        :parameterized_match : :parameterized_equality
      [filter, *key, value]
    else
      filter = value.kind_of?(Regexp) ?
        :match : :equality
      [filter, key, value]
    end
  end
  @filters << [:block_check] if @block
end
equality(attr, value, element) click to toggle source
# File lib/accessibility/qualifier.rb, line 120
def equality attr, value, element
  return unless element.attributes.include? attr
  element.attribute(attr) == value
end
match(attr, regexp, element) click to toggle source
# File lib/accessibility/qualifier.rb, line 115
def match attr, regexp, element
  return unless element.attributes.include? attr
  element.attribute(attr).to_s.match regexp
end
meets_criteria?(element) click to toggle source

Determines if the element meets all the criteria of the filters, spawning sub-searches if necessary.

@param element [AX::Element]

# File lib/accessibility/qualifier.rb, line 105
def meets_criteria? element
  @filters.all? do |filter|
    self.send(*filter, element)
  end
end
parameterized_equality(attr, param, value, element) click to toggle source
# File lib/accessibility/qualifier.rb, line 130
def parameterized_equality attr, param, value, element
  return unless element.parameterized_attributes.include? attr
  element.parameterized_attribute(attr, param) == value
end
parameterized_match(attr, param, regexp, element) click to toggle source
# File lib/accessibility/qualifier.rb, line 125
def parameterized_match attr, param, regexp, element
  return unless element.parameterized_attributes.include? attr
  element.parameterized_attribute(attr, param).to_s.match regexp
end
subsearch(klass, criteria, element) click to toggle source
# File lib/accessibility/qualifier.rb, line 111
def subsearch klass, criteria, element
  !element.search(klass, criteria).blank?
end
the_right_type?(element) click to toggle source

Checks if a candidate object is of the correct class, respecting that that the class being searched for may not be defined yet.

@param element [AX::Element]

# File lib/accessibility/qualifier.rb, line 89
def the_right_type? element
  unless @const
    if AX.const_defined? @klass
      @const = AX.const_get @klass
    else
      return false
    end
  end
  element.kind_of? @const
end