Class: Forematter::Commands::Merge

Inherits:
Forematter::CommandRunner show all
Defined in:
lib/forematter/commands/merge.rb

Instance Method Summary collapse

Methods inherited from Forematter::CommandRunner

#call

Instance Method Details

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/forematter/commands/merge.rb', line 14

def run
  dups      = values.dup
  canonical = dups.shift

  files_with(field).each do |file|
    old = file[field].to_ruby
    log_skip(file, "#{field} is not an array") && next unless old.is_a?(Array)

    # Continue unless unless field had one of the values to remove
    next if (old & dups).empty?
    dups.each { |v| file[field].delete(v) }

    # Save the original canonical for later
    file[field] << canonical unless file[field].include?(canonical)
    file.write
  end
end