Class: PoParser::Po

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/poparser/po.rb

Overview

Po class keeps all entries of a Po file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Po

Returns a new instance of Po.



10
11
12
13
# File 'lib/poparser/po.rb', line 10

def initialize(args = {})
  @entries = []
  @path    = args[:path]
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



7
8
9
# File 'lib/poparser/po.rb', line 7

def header
  @header
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/poparser/po.rb', line 8

def path
  @path
end

Instance Method Details

#add(entry) ⇒ Po Also known as: <<

add new entries to po file

Examples:

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

Parameters:

  • entry (Hash, Array)

    a hash of entry contents or an array of hashes

Returns:

Raises:

  • (ArgumentError)


29
30
31
32
33
34
# 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

#delete(entry) ⇒ Entry

Delete entry from po file

Examples:


delete(entry)

Parameters:

  • entry (Entry)

    to be deleted

Returns:

Raises:

  • (ArgumentError)


45
46
47
48
49
# 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

#eachObject



154
155
156
157
158
# File 'lib/poparser/po.rb', line 154

def each
  @entries.each do |entry|
    yield entry
  end
end

#entries(include_obsolete = false) ⇒ Array Also known as: all

Returns an array of all entries in po file

Parameters:

  • include_obsolete (Boolean) (defaults to: false)

    Whether include obsolete entries or not

Returns:

  • (Array)


55
56
57
58
59
# File 'lib/poparser/po.rb', line 55

def entries(include_obsolete = false)
  return @entries if include_obsolete

  find_all { |entry| !entry.obsolete? }
end

#fuzzyArray

Finds all entries that are flaged as fuzzy

Returns:

  • (Array)

    an array of fuzzy entries



65
66
67
# File 'lib/poparser/po.rb', line 65

def fuzzy
  find_all(&:fuzzy?)
end

#inspectObject



160
161
162
163
164
# 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

#obsoleteArray Also known as: cached

Finds all obsolete entries

Returns:

  • (Array)

    an array of obsolete entries



86
87
88
# File 'lib/poparser/po.rb', line 86

def obsolete
  find_all(&:obsolete?)
end

#save_fileObject

Saves the file to the provided path

Raises:

  • (ArgumentError)


148
149
150
151
152
# 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) ⇒ Array

Search for entries with provided string

Parameters:

  • label (Symbol)

    One of the known LABELS

  • string (String)

    String to search for

Returns:

  • (Array)

    Array of matched entries

Raises:

  • (ArgumentError)


104
105
106
107
108
109
110
111
# 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

#sizeString Also known as: length

Count of all entries without counting obsolete entries

Returns:

  • (String)


94
95
96
# File 'lib/poparser/po.rb', line 94

def size
  entries.length
end

#statsHash

Shows statistics and status of the provided file in percentage.

Returns:

  • (Hash)

    a hash of translated, untranslated and fuzzy percentages



116
117
118
119
120
121
122
123
124
125
126
# 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_hArray

Converts Po file to an hashes of entries

Returns:

  • (Array)

    array of hashes of entries



131
132
133
134
135
# 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_sString

Shows a String representation of the Po file

Returns:

  • (String)


140
141
142
143
144
145
# 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

#translatedArray

Finds all entries that are translated

Returns:

  • (Array)

    an array of translated entries



79
80
81
# File 'lib/poparser/po.rb', line 79

def translated
  find_all(&:translated?)
end

#untranslatedArray

Finds all entries that are untranslated

Returns:

  • (Array)

    an array of untranslated entries



72
73
74
# File 'lib/poparser/po.rb', line 72

def untranslated
  find_all(&:untranslated?)
end