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.



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

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.



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

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:

Returns:



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

def classify klass
  @classifications[klass]
end

#cocoaify(key) ⇒ String

Given a symbol, return the equivalent accessibility constant.

Parameters:

  • (#to_sym)

Returns:



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

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:

  • (#to_s)

Returns:



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

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]

Returns:

  • (Array<Symbol>)


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

def rubyize keys
  keys.map { |x| @rubyisms[x] }
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:

Returns:



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

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:

Returns:



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

def unprefix key
  @unprefixes[key]
end