Class: Fame::InterfaceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/fame/interface_builder.rb

Constant Summary collapse

LOCALIZATION_ENABLED_KEYPATH =

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

"i18n_enabled".freeze
LOCALIZATION_COMMENT_KEYPATH =
"i18n_comment".freeze
ACCEPTED_FILE_TYPES =

All accepted Interface Builder file types

[".storyboard", ".xib"].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ib_path) ⇒ InterfaceBuilder

Initializer

Parameters:

  • ib_path

    The path to an Interface Builder file that should be localized.



19
20
21
# File 'lib/fame/interface_builder.rb', line 19

def initialize(ib_path)
  @ib_path = ib_path
end

Class Method Details

.determine_ib_files!(path) ⇒ Array<String>

Searches the given path for Interface Builder files (.storyboard & .xib) and returns their paths.

Parameters:

  • path

    The path that should be searched for Interface Builder files.

Returns:

  • (Array<String>)

    An array of paths to Interface Builder files.



56
57
58
59
60
61
62
63
64
65
66
67
# 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

Instance Method Details

#nodesArray<LocalizedNode>

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).

Returns:

  • (Array<LocalizedNode>)

    An array of LocalizedNode objects that represent the localizable elements of the given Interface Builder file



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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