lib/i18n/coverage/reporter.rb in i18n-coverage-0.1.1 vs lib/i18n/coverage/reporter.rb in i18n-coverage-0.2.0

- old
+ new

@@ -2,38 +2,40 @@ require 'i18n/coverage/key_logger' module I18n module Coverage class Reporter - def self.report(locale: 'en', locale_dir_path: 'config/locales') - Reporter.new(locale: locale, locale_dir_path: locale_dir_path).report + def self.report + Reporter.new.report end - def initialize(locale: 'en', locale_dir_path: 'config/locales') - @existing_keys = KeyLister.list_keys(locale: locale, locale_dir_path: locale_dir_path) - @used_keys = KeyLogger.stored_keys - @percentage_used = (@used_keys.count.to_f / @existing_keys.count.to_f) * 100 - @unused_keys = @existing_keys - @used_keys + def initialize + @existing_keys = KeyLister.list_keys + @stored_keys = KeyLogger.stored_keys end def report - puts "" - puts "I18n Coverage: #{@percentage_used.round(2)}% of the keys used" - puts "#{@existing_keys.count} keys found in yml file, #{@used_keys.count} keys used during the tests" - - if @unused_keys - puts "Unused keys:" - @unused_keys.map {|k| puts " #{k}"} - end + I18n::Coverage.config.printer.print(hash_report) end def hash_report + used_keys = @existing_keys - unused_keys + percentage_used = (used_keys.count.to_f / @existing_keys.count) * 100 + { key_count: @existing_keys.count, - used_key_count: @used_keys.count, - percentage_used: @percentage_used, - unused_keys: @unused_keys + used_key_count: used_keys.count, + percentage_used: percentage_used, + unused_keys: unused_keys.to_a } end + + private + + def unused_keys + @unused_keys ||= @existing_keys.reject do |key| + @stored_keys.any? { |stored_key| key.start_with?(stored_key.to_s) } + end + end end end -end \ No newline at end of file +end