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.



34
35
36
37
38
39
40
# File 'lib/accessibility/translator.rb', line 34

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.



28
29
30
# File 'lib/accessibility/translator.rb', line 28

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)


100
101
102
# File 'lib/accessibility/translator.rb', line 100

def classify klass
  @classifications[klass]
end

#cocoaify(key) ⇒ String

Given a symbol, return the equivalent accessibility constant.

Parameters:

  • key (#to_sym)

Returns:

  • (String)


84
85
86
# File 'lib/accessibility/translator.rb', line 84

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)


128
129
130
131
132
# File 'lib/accessibility/translator.rb', line 128

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:

  • keys (Array<String>)

Returns:



73
74
75
76
77
# File 'lib/accessibility/translator.rb', line 73

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)


116
117
118
# File 'lib/accessibility/translator.rb', line 116

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)


59
60
61
# File 'lib/accessibility/translator.rb', line 59

def unprefix key
  @unprefixes[key]
end