Class: PoParser::Po
Overview
Po class keeps all entries of a Po file
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
-
#add(entry) ⇒ Po
(also: #<<)
add new entries to po file.
-
#delete(entry) ⇒ Entry
Delete entry from po file.
- #each ⇒ Object
-
#entries(include_obsolete = false) ⇒ Array
(also: #all)
Returns an array of all entries in po file.
-
#fuzzy ⇒ Array
Finds all entries that are flaged as fuzzy.
-
#initialize(args = {}) ⇒ Po
constructor
A new instance of Po.
- #inspect ⇒ Object
-
#obsolete ⇒ Array
(also: #cached)
Finds all obsolete entries.
-
#save_file ⇒ Object
Saves the file to the provided path.
-
#search_in(label, string) ⇒ Array
Search for entries with provided string.
-
#size ⇒ String
(also: #length)
Count of all entries without counting obsolete entries.
-
#stats ⇒ Hash
Shows statistics and status of the provided file in percentage.
-
#to_h ⇒ Array
Converts Po file to an hashes of entries.
-
#to_s ⇒ String
Shows a String representation of the Po file.
-
#translated ⇒ Array
Finds all entries that are translated.
-
#untranslated ⇒ Array
Finds all entries that are untranslated.
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
#header ⇒ Object (readonly)
Returns the value of attribute header.
7 8 9 |
# File 'lib/poparser/po.rb', line 7 def header @header end |
#path ⇒ Object
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
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
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 |
#each ⇒ Object
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
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 |
#fuzzy ⇒ Array
Finds all entries that are flaged as fuzzy
65 66 67 |
# File 'lib/poparser/po.rb', line 65 def fuzzy find_all(&:fuzzy?) end |
#inspect ⇒ Object
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 |
#obsolete ⇒ Array Also known as: cached
Finds all obsolete entries
86 87 88 |
# File 'lib/poparser/po.rb', line 86 def obsolete find_all(&:obsolete?) end |
#save_file ⇒ Object
Saves the file to the provided path
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
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 |
#size ⇒ String Also known as: length
Count of all entries without counting obsolete entries
94 95 96 |
# File 'lib/poparser/po.rb', line 94 def size entries.length end |
#stats ⇒ Hash
Shows statistics and status of the provided file in percentage.
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_h ⇒ Array
Converts Po file to an 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_s ⇒ String
Shows a String representation of the Po file
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 |
#translated ⇒ Array
Finds all entries that are translated
79 80 81 |
# File 'lib/poparser/po.rb', line 79 def translated find_all(&:translated?) end |
#untranslated ⇒ Array
Finds all entries that are untranslated
72 73 74 |
# File 'lib/poparser/po.rb', line 72 def untranslated find_all(&:untranslated?) end |