Class: Accessibility::Translator
- Inherits:
-
Object
- Object
- Accessibility::Translator
- 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 ⇒ Accessibility::Translator
Get the singleton instance of the Translator class.
Instance Method Summary collapse
-
#classify(klass) ⇒ String
Get the class name equivalent for a given symbol or string.
-
#cocoaify(key) ⇒ String
Given a symbol, return the equivalent accessibility constant.
-
#guess_notification(name) ⇒ String
Try to turn an arbitrary symbol into a notification constant, and then get the value of the constant.
-
#initialize ⇒ Translator
constructor
Initialize the caches.
-
#rubyize(keys) ⇒ Array<Symbol>
Take an array of Cocoa accessibility constants and return an array of shortened Ruby symbols.
-
#singularize(klass) ⇒ String
Get the singularized version of the word passed in.
-
#unprefix(key) ⇒ String
Takes an accessibility constant and returns a new string with the namespace prefix removed.
Constructor Details
#initialize ⇒ Translator
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
.instance ⇒ Accessibility::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.
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.
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.
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.
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.
116 117 118 |
# File 'lib/accessibility/translator.rb', line 116 def singularize klass @singularizations[klass] end |
#unprefix(key) ⇒ String
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.
59 60 61 |
# File 'lib/accessibility/translator.rb', line 59 def unprefix key @unprefixes[key] end |