class PoParser::Po

Po class keeps all entries of a Po file

Attributes

header[R]
path[RW]

Public Class Methods

new(args = {}) click to toggle source
# File lib/poparser/po.rb, line 10
def initialize(args = {})
  @entries = []
  @path    = args[:path]
end

Public Instance Methods

<<(entry)
Alias for: add
add(entry) click to toggle source

add new entries to po file

@example

entry = {
          translator_comment: 'comment',
          reference: 'reference comment',
          flag: 'fuzzy',
          msgid: 'translatable string',
          msgstr: 'translation'
        }
add(entry)

@param entry [Hash, Array] a hash of entry contents or an array of hashes @return [Po]

# File lib/poparser/po.rb, line 29
def add(entry)
  return import_hash(entry) if entry.is_a?(Hash)
  return import_array(entry) if entry.is_a?(Array)

  raise ArgumentError, 'Must be a hash or an array of hashes'
end
Also aliased as: <<
all(include_obsolete = false)
Alias for: entries
cached()
Alias for: obsolete
delete(entry) click to toggle source

Delete entry from po file

@example

delete(entry)

@param entry [Entry] to be deleted @return [Entry]

# File lib/poparser/po.rb, line 45
def delete(entry)
  raise(ArgumentError, 'Must be an Entry') unless entry.is_a?(PoParser::Entry)

  @entries.delete(entry)
end
each() { |entry| ... } click to toggle source
# File lib/poparser/po.rb, line 154
def each
  @entries.each do |entry|
    yield entry
  end
end
entries(include_obsolete = false) click to toggle source

Returns an array of all entries in po file

@param include_obsolete [Boolean] Whether include obsolete entries or not @return [Array]

# File lib/poparser/po.rb, line 55
def entries(include_obsolete = false)
  return @entries if include_obsolete

  find_all { |entry| !entry.obsolete? }
end
Also aliased as: all
fuzzy() click to toggle source

Finds all entries that are flaged as fuzzy

@return [Array] an array of fuzzy entries

# File lib/poparser/po.rb, line 65
def fuzzy
  find_all(&:fuzzy?)
end
inspect() click to toggle source
# File lib/poparser/po.rb, line 160
def inspect
  "<#{self.class.name}, Translated: #{translated.length}"\
    "(#{stats[:translated]}%) Untranslated: #{untranslated.length}"\
    "(#{stats[:untranslated]}%) Fuzzy: #{fuzzy.length}(#{stats[:fuzzy]}%)>"
end
length()
Alias for: size
obsolete() click to toggle source

Finds all obsolete entries

@return [Array] an array of obsolete entries

# File lib/poparser/po.rb, line 86
def obsolete
  find_all(&:obsolete?)
end
Also aliased as: cached
save_file() click to toggle source

Saves the file to the provided path

# File lib/poparser/po.rb, line 148
def save_file
  raise ArgumentError, 'Need a Path to save the file' if @path.nil?

  File.open(@path, 'w') { |file| file.write(to_s) }
end
search_in(label, string) click to toggle source

Search for entries with provided string

@param label [Symbol] One of the known LABELS @param string [String] String to search for @return [Array] Array of matched entries

# File lib/poparser/po.rb, line 104
def search_in(label, string)
  raise(ArgumentError, "Unknown key: #{label}") unless LABELS.include?(label.to_sym)

  find_all do |entry|
    text = entry.send(label).str
    text.match(/#{string}/i)
  end
end
size() click to toggle source

Count of all entries without counting obsolete entries

@return [String]

# File lib/poparser/po.rb, line 94
def size
  entries.length
end
Also aliased as: length
stats() click to toggle source

Shows statistics and status of the provided file in percentage.

@return [Hash] a hash of translated, untranslated and fuzzy percentages

# File lib/poparser/po.rb, line 116
def stats
  untranslated_size = untranslated.size
  translated_size   = translated.size
  fuzzy_size        = fuzzy.size

  {
    translated: percentage(translated_size),
    untranslated: percentage(untranslated_size),
    fuzzy: percentage(fuzzy_size),
  }
end
to_h() click to toggle source

Converts Po file to an hashes of entries

@return [Array] array of hashes of entries

# File lib/poparser/po.rb, line 131
def to_h
  array = @entries.map(&:to_h)
  array.unshift(@header.to_h) if @header
  array
end
to_s() click to toggle source

Shows a String representation of the Po file

@return [String]

# File lib/poparser/po.rb, line 140
def to_s
  array = @entries.map(&:to_s)
  # add a blank line after header
  array.unshift(@header.to_s, '') if @header
  array.join("\n")
end
translated() click to toggle source

Finds all entries that are translated

@return [Array] an array of translated entries

# File lib/poparser/po.rb, line 79
def translated
  find_all(&:translated?)
end
untranslated() click to toggle source

Finds all entries that are untranslated

@return [Array] an array of untranslated entries

# File lib/poparser/po.rb, line 72
def untranslated
  find_all(&:untranslated?)
end

Private Instance Methods

add_entry(entry) click to toggle source

rubocop:disable Style/SafeNavigation

# File lib/poparser/po.rb, line 189
def add_entry(entry)
  return add_header_entry(entry) if entry[:msgid] && entry[:msgid].empty?

  @entries << Entry.new(entry)
end
add_header_entry(entry) click to toggle source

rubocop:enable Style/SafeNavigation

# File lib/poparser/po.rb, line 196
def add_header_entry(entry)
  raise('Duplicate entry, header was already instantiated') if @header

  @header = Header.new(Entry.new(entry))
end
import_array(entry) click to toggle source
# File lib/poparser/po.rb, line 182
def import_array(entry)
  entry.each { |en| add_entry(en) }

  self
end
import_hash(entry) click to toggle source
# File lib/poparser/po.rb, line 176
def import_hash(entry)
  add_entry(entry)

  self
end
percentage(count) click to toggle source

calculates percentages based on total number of entries

@param [Integer] number of entries @return [Float] percentage of the provided entries

# File lib/poparser/po.rb, line 172
def percentage(count)
  ((count.to_f / size) * 100).round(1)
end