module Xcodeproj::Project::Object::CaseConverter
Converts between camel case names used in the xcodeproj plist files and the ruby symbols used to represent them.
Public Class Methods
convert_to_plist(name, type = nil)
click to toggle source
@return [String] The plist equivalent of the given Ruby name.
@param [Symbol, String] name
The name to convert
@param [Symbol, Nil] type
The type of conversion. Pass `nil` for normal camel case and `:lower` for camel case starting with a lower case letter.
@example
CaseConverter.convert_to_plist(:project_ref) #=> ProjectRef
# File lib/xcodeproj/project/case_converter.rb, line 20 def self.convert_to_plist(name, type = nil) case name when :remote_global_id_string 'remoteGlobalIDString' else if type == :lower cache = plist_cache[:lower] ||= {} cache[name] ||= camelize(name, :uppercase_first_letter => false) else cache = plist_cache[:normal] ||= {} cache[name] ||= camelize(name) end end end
convert_to_ruby(name)
click to toggle source
@return [Symbol] The Ruby equivalent of the given plist name.
@param [String] name
The name to convert
@example
CaseConverter.convert_to_ruby('ProjectRef') #=> :project_ref
# File lib/xcodeproj/project/case_converter.rb, line 74 def self.convert_to_ruby(name) underscore(name.to_s).to_sym end
plist_cache()
click to toggle source
@return [Hash] A cache for the conversion to the Plist
format.
@note A cache is used because this operation is performed for each
attribute of the project when it is saved and caching it has an important performance benefit.
# File lib/xcodeproj/project/case_converter.rb, line 84 def self.plist_cache @plist_cache ||= {} end
Private Class Methods
camelize(term, uppercase_first_letter: true)
click to toggle source
The following two methods are taken from activesupport, github.com/rails/rails/blob/v5.0.2/activesupport/lib/active_support/inflector/methods.rb all credit for them goes to the original authors.
The code is used under the MIT license.
# File lib/xcodeproj/project/case_converter.rb, line 41 def self.camelize(term, uppercase_first_letter: true) string = term.to_s string = if uppercase_first_letter string.sub(/^[a-z\d]*/, &:capitalize) else string.sub(/^(?:(?=a)b(?=\b|[A-Z_])|\w)/, &:downcase) end string.gsub!(%r{(?:_|(/))([a-z\d]*)}i) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" } string.gsub!('/'.freeze, '::'.freeze) string end
underscore(camel_cased_word)
click to toggle source
# File lib/xcodeproj/project/case_converter.rb, line 54 def self.underscore(camel_cased_word) return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/ word = camel_cased_word.to_s.gsub('::'.freeze, '/'.freeze) word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)((?=a)b)(?=\b|[^a-z])/) { "#{Regexp.last_match(1) && '_'.freeze}#{Regexp.last_match(2).downcase}" } word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze) word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze) word.tr!('-'.freeze, '_'.freeze) word.downcase! word end