Class: Accessibility::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/accessibility/translator.rb

Overview

Maintain all the rules for transforming Cocoa constants into something a little more Rubyish and taking the Rubyish symbols and translating them back to Cocoa constants.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTranslator

Initialize the caches.


31
32
33
34
35
36
37
# File 'lib/accessibility/translator.rb', line 31

def initialize
  init_unprefixes
  init_rubyisms
  init_cocoaifications
  init_classifications
  init_singularizations
end

Class Method Details

.instanceAccessibility::Translator

Get the singleton instance of the Accessibility::Translator class. This is meant to mimic the important functionality of the Singleton mix-in.


25
26
27
# File 'lib/accessibility/translator.rb', line 25

def self.instance
  @instance ||= new
end

Instance Method Details

#classify(klass) ⇒ String

Get the class name equivalent for a given symbol or string. This is just a caching front end to the #classify method from the ActiveSupport inflector.

Examples:


classify 'text_field' # => "TextField"
classify 'buttons'    # => "Button"

Parameters:

  • klass (String)

Returns:

  • (String)

97
98
99
# File 'lib/accessibility/translator.rb', line 97

def classify klass
  @classifications[klass]
end

#cocoaify(key) ⇒ String

Given a symbol, return the equivalent accessibility constant.

Parameters:

  • key (#to_sym)

Returns:

  • (String)

81
82
83
# File 'lib/accessibility/translator.rb', line 81

def cocoaify key
  @cocoaifications[key.to_sym]
end

#guess_notification(name) ⇒ String

Try to turn an arbitrary symbol into a notification constant, and then get the value of the constant. If it cannot be turned into a notification constant then the original string parameter will be returned.

Parameters:

  • name (#to_s)

Returns:

  • (String)

125
126
127
128
129
# File 'lib/accessibility/translator.rb', line 125

def guess_notification name
  name  = name.to_s.gsub(/(?:^|_)(.)/) do $1.upcase! || $1 end
  const = "KAX#{name}Notification"
  Object.const_defined?(const) ? Object.const_get(const) : name
end

#rubyize(keys) ⇒ Array<Symbol>

Take an array of Cocoa accessibility constants and return an array of shortened Ruby symbols.

Examples:


rubyize ["AXRole", "AXTitleUIElement"] # => [:role, :title_ui_element]

Parameters:

Returns:


70
71
72
73
74
# File 'lib/accessibility/translator.rb', line 70

def rubyize keys
  keys = keys.map { |x| @rubyisms[x] }
  keys.flatten!
  keys
end

#singularize(klass) ⇒ String

Get the singularized version of the word passed in. This is just a caching front end to the #singularize method from the ActiveSupport inflector.

Examples:


singularize 'buttons'     # => 'button'
singularize 'check_boxes' # => 'check_box'

Parameters:

  • klass (String)

Returns:

  • (String)

113
114
115
# File 'lib/accessibility/translator.rb', line 113

def singularize klass
  @singularizations[klass]
end

#unprefix(key) ⇒ String

Note:

In the case of a predicate name, this will strip the 'Is' part of the name if it is present

Takes an accessibility constant and returns a new string with the namespace prefix removed.

Examples:


unprefix 'AXTitle'                    # => 'Title'
unprefix 'AXIsApplicationEnabled'     # => 'ApplicationEnabled'
unprefix 'MCAXEnabled'                # => 'Enabled'
unprefix KAXWindowCreatedNotification # => 'WindowCreated'
unprefix NSAccessibilityButtonRole    # => 'Button'

Parameters:

  • key (String)

Returns:

  • (String)

56
57
58
# File 'lib/accessibility/translator.rb', line 56

def unprefix key
  @unprefixes[key]
end