Class: I18n::Tasks::Scanners::Results::KeyOccurrences
- Inherits:
-
Object
- Object
- I18n::Tasks::Scanners::Results::KeyOccurrences
- Defined in:
- lib/i18n/tasks/scanners/results/key_occurrences.rb
Overview
Note:
This is a value type. Equality and hash code are determined from the attributes.
A scanned key and all its occurrences.
Instance Attribute Summary collapse
-
#key ⇒ String
readonly
The key.
-
#occurrences ⇒ Array<Occurrence>
readonly
The key’s occurrences.
Class Method Summary collapse
-
.merge_keys(keys_occurrences) ⇒ Array<KeyOccurrences>
Merge KeyOccurrences in an Enumerable<KeyOccurrences> so that in the resulting Array<KeyOccurrences>: * Each key occurs only once.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(key:, occurrences:) ⇒ KeyOccurrences
constructor
A new instance of KeyOccurrences.
- #inspect ⇒ Object
Constructor Details
#initialize(key:, occurrences:) ⇒ KeyOccurrences
Returns a new instance of KeyOccurrences.
16 17 18 19 |
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 16 def initialize(key:, occurrences:) @key = key @occurrences = occurrences end |
Instance Attribute Details
#key ⇒ String (readonly)
Returns the key.
11 12 13 |
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 11 def key @key end |
#occurrences ⇒ Array<Occurrence> (readonly)
Returns the key’s occurrences.
14 15 16 |
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 14 def occurrences @occurrences end |
Class Method Details
.merge_keys(keys_occurrences) ⇒ Array<KeyOccurrences>
Merge I18n::Tasks::Scanners::Results::KeyOccurrences in an Enumerable<KeyOccurrences> so that in the resulting Array<KeyOccurrences>:
-
Each key occurs only once.
-
Occurrences from multiple instances of the key are merged.
-
The order of keys is preserved, occurrences are ordered by Occurrence#path.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 43 def self.merge_keys(keys_occurrences) keys_occurrences.each_with_object({}) do |key_occurrences, results_by_key| (results_by_key[key_occurrences.key] ||= []) << key_occurrences.occurrences end.map do |key, all_occurrences| occurrences = all_occurrences.flatten(1) occurrences.sort_by!(&:path) occurrences.uniq! new(key: key, occurrences: occurrences) end end |
Instance Method Details
#==(other) ⇒ Object
21 22 23 |
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 21 def ==(other) other.key == @key && other.occurrences == @occurrences end |
#eql?(other) ⇒ Boolean
25 26 27 |
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 25 def eql?(other) self == other end |
#hash ⇒ Object
29 30 31 |
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 29 def hash [@key, @occurrences].hash end |
#inspect ⇒ Object
33 34 35 |
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 33 def inspect "KeyOccurrences(#{key.inspect}, [#{occurrences.map(&:inspect).join(', ')}])" end |