class Locca::AndroidCollectionWriter

Public Class Methods

new(file_manager) click to toggle source
# File lib/locca/android_collection_writer.rb, line 31
def initialize(file_manager)
    @file_manager = file_manager
end

Public Instance Methods

prepare_content(content) click to toggle source
# File lib/locca/android_collection_writer.rb, line 80
def prepare_content(content)
    return content.gsub(/\n/, "\\n")
end
write_to_path(collection, filepath) click to toggle source
# File lib/locca/android_collection_writer.rb, line 35
def write_to_path(collection, filepath)
    if not filepath
        raise ArgumentException, 'filepath can\'t be nil'
    end

    FileUtils.mkdir_p(@file_manager.dirname(filepath))


    document = Nokogiri::XML("")
    document.encoding = "UTF-8"

    resources = Nokogiri::XML::Node.new('resources', document)

    collection.sorted_each do |item|
                        if item.comment
                                resources.add_child(Nokogiri::XML::Comment.new(document, " #{item.comment} "))
                        end

        if item.plural?
            node = Nokogiri::XML::Node.new('plurals', document)
            node["name"] = item.key

            item.value.each do |key, value|
                nodeItem = Nokogiri::XML::Node.new('item', document)
                nodeItem["quantity"] = key
                nodeItem.content = prepare_content(value)
                node.add_child(nodeItem)
            end

            resources.add_child(node)
        else
            node = Nokogiri::XML::Node.new('string', document)
            node["name"] = item.key
            node.content = prepare_content(item.value)
            resources.add_child(node)
        end
        end

    document.root = resources

    @file_manager.open(filepath, "w") do |io|
        io << document.to_xml
    end
end