class Fame::InterfaceBuilder

Constants

ACCEPTED_FILE_TYPES

All accepted Interface Builder file types

LOCALIZATION_COMMENT_KEYPATH
LOCALIZATION_ENABLED_KEYPATH

Keypaths to custom runtime attributes (provided by iOS Extenstion, see Fame.swift)

Public Class Methods

determine_ib_files!(path) click to toggle source

Searches the given path for Interface Builder files (.storyboard & .xib) and returns their paths. @param path The path that should be searched for Interface Builder files. @return [Array<String>] An array of paths to Interface Builder files.

# File lib/fame/interface_builder.rb, line 56
def self.determine_ib_files!(path)
  raise "The provided file or folder does not exist" unless File.exist? path

  if File.directory?(path)
    files = Dir.glob(path + "/**/*{#{ACCEPTED_FILE_TYPES.join(',')}}")
    raise "The provided folder did not contain any interface files (#{ACCEPTED_FILE_TYPES.join(', ')})" unless files.count > 0
    return files
  else
    raise "The provided file is not an interface file (#{ACCEPTED_FILE_TYPES.join(', ')})" unless ACCEPTED_FILE_TYPES.include? File.extname(path)
    return [path]
  end
end
new(ib_path) click to toggle source

Initializer @param ib_path The path to an Interface Builder file that should be localized.

# File lib/fame/interface_builder.rb, line 19
def initialize(ib_path)
  @ib_path = ib_path
end

Public Instance Methods

nodes() click to toggle source

Searches the current Interface Builder file for XML nodes that should be localized. Localization is only enabled if explicitly set via the fame Interface Builder integration (see Fame.swift file). @return [Array<LocalizedNode>] An array of LocalizedNode objects that represent the localizable elements of the given Interface Builder file

# File lib/fame/interface_builder.rb, line 28
def nodes
  ib_file = File.open(@ib_path)
  doc = Nokogiri::XML(ib_file)
  ib_file.close

  # Grab raw nokogiri nodes that have a localization keypath
  raw_nodes = doc.xpath("//userDefinedRuntimeAttribute[@keyPath='#{LOCALIZATION_ENABLED_KEYPATH}']")

  # Map raw nodes info to instances of LocalizedNode
  raw_nodes.map do |node|
    parent = node.parent.parent                                                                                                     # i.e. UILabel, UISwitch, etc.
    vc = parent.xpath("ancestor::viewController")                                   # the view controller of the element (only available in .storyboard files)
    element_name = parent.name                                                                                                              # i.e. label, switch
    original_id = parent['id']                                                                                                              # ugly Xcode ID, e.g. F4z-Kg-ni6
    vc_name = vc.attr('customClass').value rescue nil                       # name of custom view controller class

    i18n_enabled = node.parent.xpath("userDefinedRuntimeAttribute[@keyPath='#{LOCALIZATION_ENABLED_KEYPATH}']").attr('value').value == "YES" rescue false
    i18n_comment = node.parent.xpath("userDefinedRuntimeAttribute[@keyPath='#{LOCALIZATION_COMMENT_KEYPATH}']").attr('value').value rescue nil

    LocalizedNode.new(node, original_id, vc_name, element_name, i18n_enabled, i18n_comment)
  end
end