class PutText::POFile

Attributes

entries[RW]

Public Class Methods

new(entries) click to toggle source

Create a new POFile @param [Array<POEntry>] entries an array of POEntry objects, that should

be placed in this file.
# File lib/puttext/po_file.rb, line 13
    def initialize(entries)
      @entries = entries
      @header_entry = POEntry.new(
        flags: ['fuzzy'],
        msgid: '',
        msgstr: <<-STRING.unindent
          POT-Creation-Date: #{Time.now.strftime('%Y-%m-%d %H:%M%z')}
          MIME-Version: 1.0
          Content-Type: text/plain; charset=UTF-8
        STRING
      )
    end

Public Instance Methods

==(other) click to toggle source
# File lib/puttext/po_file.rb, line 55
def ==(other)
  @entries.sort == other.entries.sort
end
merge(other_file) click to toggle source

Merge the contents of another POFile to this POFile. @param [POFile] other_file the file to merge the contents of to this file.

# File lib/puttext/po_file.rb, line 47
def merge(other_file)
  unless other_file.is_a?(POFile)
    raise ArgumentError, 'argument must be a PutText::POFile'
  end

  @entries += other_file.entries
end
to_s() click to toggle source
# File lib/puttext/po_file.rb, line 26
def to_s
  str_io = StringIO.new
  write_to(str_io)
  str_io.string
end
write_to(io) click to toggle source

Write the contents of this file to the specified IO object. @param [IO] io the IO object to write the contents of the file to.

# File lib/puttext/po_file.rb, line 34
def write_to(io)
  deduplicate

  io.write(@header_entry.to_s)

  @entries.each do |entry|
    io.write("\n")
    io.write(entry.to_s)
  end
end

Private Instance Methods

deduplicate() click to toggle source
# File lib/puttext/po_file.rb, line 61
def deduplicate
  uniq_entries = {}

  @entries.each do |entry|
    key = entry.unique_key

    if uniq_entries[key]
      uniq_entries[key].merge(entry)
    else
      uniq_entries[key] = entry
    end
  end

  @entries = uniq_entries.values
end