Class: Stringer::StringsFile
- Inherits:
-
Object
- Object
- Stringer::StringsFile
- Defined in:
- lib/stringer/strings_file.rb
Instance Attribute Summary collapse
-
#lines ⇒ Object
Returns the value of attribute lines.
Instance Method Summary collapse
- #apply(other_string_file) ⇒ Object
- #check_for_file(path) ⇒ Object
- #comment_lines ⇒ Object
- #comments ⇒ Object
- #fetch_lines_at(path) ⇒ Object
-
#initialize(path) ⇒ StringsFile
constructor
A new instance of StringsFile.
- #key_from_line(line) ⇒ Object
- #translation_hash ⇒ Object
- #translation_lines ⇒ Object
- #value_from_line(line) ⇒ Object
- #write! ⇒ Object
Constructor Details
#initialize(path) ⇒ StringsFile
Returns a new instance of StringsFile.
5 6 7 8 9 |
# File 'lib/stringer/strings_file.rb', line 5 def initialize(path) check_for_file(path) @path = path @lines = fetch_lines_at(path) end |
Instance Attribute Details
#lines ⇒ Object
Returns the value of attribute lines.
3 4 5 |
# File 'lib/stringer/strings_file.rb', line 3 def lines @lines end |
Instance Method Details
#apply(other_string_file) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/stringer/strings_file.rb', line 52 def apply(other_string_file) removed_keys = translation_hash.keys - other_string_file.translation_hash.keys added_keys = other_string_file.translation_hash.keys - translation_hash.keys @translation_hash = other_string_file.translation_hash.merge(translation_hash) removed_keys.each {|k| @translation_hash.delete(k)} [added_keys, removed_keys] end |
#check_for_file(path) ⇒ Object
11 12 13 |
# File 'lib/stringer/strings_file.rb', line 11 def check_for_file(path) raise "No Localisations found at #{path}" unless File.exist?(path) end |
#comment_lines ⇒ Object
21 22 23 |
# File 'lib/stringer/strings_file.rb', line 21 def comment_lines @lines.select{|l| l =~ /^\/\*/ } end |
#comments ⇒ Object
29 30 31 32 33 |
# File 'lib/stringer/strings_file.rb', line 29 def comments @comments ||= comment_lines.collect do |line| line.gsub("/*", "").gsub("*/", "").strip end end |
#fetch_lines_at(path) ⇒ Object
15 16 17 18 19 |
# File 'lib/stringer/strings_file.rb', line 15 def fetch_lines_at(path) IO.readlines(path, mode:"rb:UTF-16LE").collect do |l| l.encode("UTF-8").gsub("\uFEFF", "") end end |
#key_from_line(line) ⇒ Object
44 45 46 |
# File 'lib/stringer/strings_file.rb', line 44 def key_from_line(line) line.split("=").first.gsub("\"", "").strip end |
#translation_hash ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/stringer/strings_file.rb', line 35 def translation_hash return @translation_hash if @translation_hash @translation_hash = translation_lines.inject({}) do |r,line| r[key_from_line(line)] = value_from_line(line) r end end |
#translation_lines ⇒ Object
25 26 27 |
# File 'lib/stringer/strings_file.rb', line 25 def translation_lines @lines.select{|l| l =~ /^"/} end |
#value_from_line(line) ⇒ Object
48 49 50 |
# File 'lib/stringer/strings_file.rb', line 48 def value_from_line(line) line.split("=").last.strip.gsub(";", "").gsub("\"", "") end |
#write! ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/stringer/strings_file.rb', line 60 def write! File.open(@path, "wb:UTF-16LE") do |file| file.write("\uFEFF") file.write("/* Generated */\n") translation_hash.each do |key, value| file.puts "\"#{key}\" = \"#{value}\";" end end end |