Class: Babelish::XcodeMacros

Inherits:
Object
  • Object
show all
Defined in:
lib/babelish/xcode_macros.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table = "Localizable", keys = {}) ⇒ XcodeMacros

Returns a new instance of XcodeMacros.



4
5
6
7
8
# File 'lib/babelish/xcode_macros.rb', line 4

def initialize(table = "Localizable", keys = {})
  @content = ""
  @table = table
  @keys = keys
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/babelish/xcode_macros.rb', line 3

def content
  @content
end

#keysObject

Returns the value of attribute keys.



3
4
5
# File 'lib/babelish/xcode_macros.rb', line 3

def keys
  @keys
end

#tableObject

Returns the value of attribute table.



3
4
5
# File 'lib/babelish/xcode_macros.rb', line 3

def table
  @table
end

Class Method Details

.write_macros(file_path, table, keys, comments = {}) ⇒ Object



10
11
12
13
14
# File 'lib/babelish/xcode_macros.rb', line 10

def self.write_macros(file_path, table, keys, comments = {})
  instance = XcodeMacros.new
  instance.process(table, keys, comments)
  instance.write_content(file_path)
end

Instance Method Details

#process(table, keys, comments = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/babelish/xcode_macros.rb', line 16

def process(table, keys, comments = {})
  keys.each do |key|
    clean_key = key.gsub(' ', '')
    clean_key.gsub!(/[[:punct:]]/, '_')       
    clean_key.gsub!('__', '_')
    clean_key = clean_key[1..clean_key.size-1] if clean_key[0] == '_'
    clean_key = clean_key[0..clean_key.size-2] if clean_key.size > 1 and clean_key[clean_key.size-1] == '_'
    macro_name = "LS_#{clean_key.upcase}" 
    macro_name += "_#{table.upcase}" if table != "Localizable" 
    comment = comments[key]
    @content << String.new(<<-EOS)
#define #{macro_name} NSLocalizedStringFromTable(@"#{key}",@"#{table}",@"#{comment.to_s}")
    EOS
  end
  @content
end

#write_content(file_path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/babelish/xcode_macros.rb', line 33

def write_content(file_path)
  header = String.new(<<-EOS)
//
//  file_path
//  
//  This file was generated by Babelish
//  
//  https://github.com/netbe/babelish
//
    EOS
  header.gsub! "file_path", File.basename(file_path)
  file = File.new(file_path, "w")
  file.write header + @content
  file.close
end