Class: I18n::Tasks::Scanners::Results::KeyOccurrences

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#keyString (readonly)

Returns the key.

Returns:

  • (String)

    the key.



11
12
13
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 11

def key
  @key
end

#occurrencesArray<Occurrence> (readonly)

Returns the key’s occurrences.

Returns:

  • (Array<Occurrence>)

    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.

Parameters:

Returns:



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

Returns:

  • (Boolean)


25
26
27
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 25

def eql?(other)
  self == other
end

#hashObject



29
30
31
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 29

def hash
  [@key, @occurrences].hash
end

#inspectObject



33
34
35
# File 'lib/i18n/tasks/scanners/results/key_occurrences.rb', line 33

def inspect
  "KeyOccurrences(#{key.inspect}, [#{occurrences.map(&:inspect).join(', ')}])"
end