Class: BetterTranslate::Analyzer::OrphanDetector
- Inherits:
-
Object
- Object
- BetterTranslate::Analyzer::OrphanDetector
- Defined in:
- lib/better_translate/analyzer/orphan_detector.rb
Overview
Detects orphan i18n keys (keys defined but never used in code)
Instance Attribute Summary collapse
-
#all_keys ⇒ Hash
readonly
All translation keys with their values.
-
#orphans ⇒ Array<String>
readonly
Orphan keys (not used in code).
-
#used_keys ⇒ Set
readonly
Keys that are used in code.
Instance Method Summary collapse
-
#detect ⇒ Array<String>
Detect orphan keys.
-
#initialize(all_keys, used_keys) ⇒ OrphanDetector
constructor
Initialize detector.
-
#orphan_count ⇒ Integer
Get count of orphan keys.
-
#orphan_details ⇒ Hash
Get details of orphan keys with their values.
-
#usage_percentage ⇒ Float
Calculate usage percentage.
Constructor Details
#initialize(all_keys, used_keys) ⇒ OrphanDetector
Initialize detector
30 31 32 33 34 |
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 30 def initialize(all_keys, used_keys) @all_keys = all_keys @used_keys = used_keys @orphans = [] end |
Instance Attribute Details
#all_keys ⇒ Hash (readonly)
Returns All translation keys with their values.
17 18 19 |
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 17 def all_keys @all_keys end |
#orphans ⇒ Array<String> (readonly)
Returns Orphan keys (not used in code).
23 24 25 |
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 23 def orphans @orphans end |
#used_keys ⇒ Set (readonly)
Returns Keys that are used in code.
20 21 22 |
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 20 def used_keys @used_keys end |
Instance Method Details
#detect ⇒ Array<String>
Detect orphan keys
Compares all keys with used keys and identifies those that are never referenced.
46 47 48 |
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 46 def detect @orphans = all_keys.keys.reject { |key| used_keys.include?(key) } end |
#orphan_count ⇒ Integer
Get count of orphan keys
54 55 56 |
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 54 def orphan_count @orphans.size end |
#orphan_details ⇒ Hash
Get details of orphan keys with their values
66 67 68 69 70 71 72 73 |
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 66 def orphan_details # @type var result: Hash[String, untyped] result = {} @orphans.each do |key| result[key] = all_keys[key] end result end |
#usage_percentage ⇒ Float
Calculate usage percentage
83 84 85 86 87 88 |
# File 'lib/better_translate/analyzer/orphan_detector.rb', line 83 def usage_percentage return 0.0 if all_keys.empty? used_count = all_keys.size - @orphans.size (used_count.to_f / all_keys.size * 100).round(1).to_f end |