Sha256: c22c5768ba69c77764f54617503caa4b848fccb7e5af1d8afaf46cdc21807c6a

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module Keepasser
  class Comparator
    attr_reader :errors

    def initialize left, right
      parsers = [Parser.new(left), Parser.new(right)]

      @errors = {}

      parsers[0].keys.each do |group|
        missing = []
        left = parsers[0][group].keys - parsers[1][group].keys
        left.map { |entry| parsers[0][group].delete entry }

        right = parsers[1][group].keys - parsers[0][group].keys
        missing += right.map { |e| { e => parsers[1][group][e].fields } }
        right.map { |entry| parsers[1][group].delete entry }

        if missing.any?
          begin
            @errors['Missing entries'][group] = missing.clone
          rescue NoMethodError
            @errors['Missing entries'] = {}
            @errors['Missing entries'][group] = missing.clone
          end
        end
      end

      parsers[0].each_pair do |group, entries|
        entries.each_pair do |title, data|
          if data.fields != parsers[1][group][title].fields
            @errors['Different data'] = {}
            @errors['Different data'][group] = {}
            @errors['Different data'][group][title] = {}
            data.fields.each_pair do |key, value|
              other_value = parsers[1][group][title].fields[key]
              if value != parsers[1][group][title].fields[key]
                @errors['Different data'][group][title][key] = [value, other_value]
              end
            end
          end
        end
      end
    end

    def to_s
      @errors.to_yaml
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
keepasser-0.1.0 lib/keepasser/comparator.rb