Class: SlimKeyfy::Console::Translate
- Defined in:
- lib/slimkeyfy/console/translate.rb
Instance Attribute Summary collapse
-
#bak_path ⇒ Object
readonly
Returns the value of attribute bak_path.
-
#original_file_path ⇒ Object
readonly
Returns the value of attribute original_file_path.
Class Method Summary collapse
Instance Method Summary collapse
- #comment_tag ⇒ Object
- #create_yaml_processor(options) ⇒ Object
- #finalize! ⇒ Object
- #generate_key_base ⇒ Object
-
#initialize(options = {}) ⇒ Translate
constructor
A new instance of Translate.
- #process_new_line(idx, old_line, new_line, translations) ⇒ Object
- #stream_mode ⇒ Object
- #transformer ⇒ Object
- #translations_are_invalid?(translations) ⇒ Boolean
- #update_with(idx, line) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Translate
Returns a new instance of Translate.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/slimkeyfy/console/translate.rb', line 5 def initialize(={}) @extension = [:ext] @transformer = transformer @original_file_path = [:input] @no_backup = .fetch(:no_backup, false) @bak_path = SlimKeyfy::Slimutils::MFileUtils.backup(@original_file_path) @content = self.class.join_multiline( SlimKeyfy::Slimutils::FileReader.read(@bak_path).split("\n") ) @file_path = SlimKeyfy::Slimutils::MFileUtils.create_new_file(@original_file_path) @key_base = generate_key_base @yaml_processor = create_yaml_processor() @new_content = [] @changes = false end |
Instance Attribute Details
#bak_path ⇒ Object (readonly)
Returns the value of attribute bak_path.
3 4 5 |
# File 'lib/slimkeyfy/console/translate.rb', line 3 def bak_path @bak_path end |
#original_file_path ⇒ Object (readonly)
Returns the value of attribute original_file_path.
3 4 5 |
# File 'lib/slimkeyfy/console/translate.rb', line 3 def original_file_path @original_file_path end |
Class Method Details
.join_multiline(strings_array) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/slimkeyfy/console/translate.rb', line 102 def self.join_multiline( strings_array ) result = [] joining_str = '' indent_length = 0 long_str_start = /^[ ]+\|/ long_str_indent = /^[ ]+/ = /^[ ]+\|/ strings_array.each do |str| if joining_str.empty? if str[long_str_start] joining_str = str indent_length = str[long_str_start].length else result << str end #multiline string continues with spaces elsif ( str[long_str_indent] && str[long_str_indent].length.to_i >= indent_length ) joining_str << str.gsub( long_str_indent, ' ' ) #muliline string continues with spaces and vertical bar with same indentation elsif str[] && str[].length.to_i == indent_length joining_str << str.gsub( , ' ' ) #multiline string ends else result << joining_str joining_str = '' indent_length = 0 result << str end end result << joining_str unless joining_str.empty? result end |
Instance Method Details
#comment_tag ⇒ Object
98 99 100 |
# File 'lib/slimkeyfy/console/translate.rb', line 98 def comment_tag @transformer.slim? ? "//" : "#" end |
#create_yaml_processor(options) ⇒ Object
30 31 32 |
# File 'lib/slimkeyfy/console/translate.rb', line 30 def create_yaml_processor() SlimKeyfy::Slimutils::YamlProcessor.new([:locale], @key_base, .fetch(:yaml_output, nil)) end |
#finalize! ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/slimkeyfy/console/translate.rb', line 71 def finalize! if @changes if SlimKeyfy::Console::IOAction.yes_or_no?("Do you like to review your changes?") SlimKeyfy::Console::Printer.unix_diff(@bak_path, @original_file_path) end if SlimKeyfy::Console::IOAction.yes_or_no?("Do you like to save your changes?") @yaml_processor.store! puts "Saved! at #{@original_file_path}" SlimKeyfy::Slimutils::MFileUtils.rm(@bak_path) if @no_backup else SlimKeyfy::Slimutils::MFileUtils.restore(@bak_path, @original_file_path) puts "Restored!" end else SlimKeyfy::Slimutils::MFileUtils.restore(@bak_path, @original_file_path) puts "Nothing was changed!" end end |
#generate_key_base ⇒ Object
34 35 36 |
# File 'lib/slimkeyfy/console/translate.rb', line 34 def generate_key_base SlimKeyfy::Slimutils::BaseKeyGenerator.generate_key_base_from_path(@original_file_path, @extension) end |
#process_new_line(idx, old_line, new_line, translations) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/slimkeyfy/console/translate.rb', line 54 def process_new_line(idx, old_line, new_line, translations) SlimKeyfy::Console::Printer.difference(old_line, new_line, translations) case SlimKeyfy::Console::IOAction.choose("Changes wanted?") when "y" then update_with(idx, new_line) when "n" then update_with(idx, old_line) @yaml_processor.delete_translations(translations) when "x" then update_with(idx, SlimKeyfy::Console::Printer.tag(old_line, translations, comment_tag)) @yaml_processor.delete_translations(translations) when "a" then SlimKeyfy::Slimutils::MFileUtils.restore(@bak_path, @original_file_path) puts "Aborted!" exit end end |
#stream_mode ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/slimkeyfy/console/translate.rb', line 38 def stream_mode @content.each_with_index do |old_line, idx| word = SlimKeyfy::Transformer::Word.new(old_line, @key_base, @extension) new_line, translations = @transformer.new(word, @yaml_processor).transform if translations_are_invalid?(translations) @yaml_processor.delete_translations(translations) update_with(idx, old_line) else process_new_line(idx, old_line, new_line, translations) @changes = true end end SlimKeyfy::Slimutils::FileWriter.write(@file_path, @new_content.join("\n")) finalize! end |
#transformer ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/slimkeyfy/console/translate.rb', line 19 def transformer if @extension == "slim" SlimKeyfy::Transformer::SlimTransformer elsif @extension == "rb" SlimKeyfy::Transformer::ControllerTransformer else puts "Unknown extension type!" exit end end |
#translations_are_invalid?(translations) ⇒ Boolean
94 95 96 |
# File 'lib/slimkeyfy/console/translate.rb', line 94 def translations_are_invalid?(translations) translations.nil? or translations.empty? end |
#update_with(idx, line) ⇒ Object
90 91 92 |
# File 'lib/slimkeyfy/console/translate.rb', line 90 def update_with(idx, line) @new_content[idx] = line end |