class Babelish::XcodeMacros

Attributes

content[RW]
keys[RW]
table[RW]

Public Class Methods

new(table = "Localizable", keys = {}) click to toggle source
# File lib/babelish/xcode_macros.rb, line 4
def initialize(table = "Localizable", keys = {})
  @content = ""
  @table = table
  @keys = keys
end
write_macros(file_path, table, keys, comments = {}) click to toggle source
# 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

Public Instance Methods

process(table, keys, comments = {}) click to toggle source
# 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) click to toggle source
# 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