Class: ScraperWiki::API::Matchers::HaveUniqueValues

Inherits:
FieldMatcher show all
Defined in:
lib/scraperwiki-api/matchers.rb

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from ScraperWiki::API::Matchers::CustomMatcher

Instance Method Details

#failure_predicateObject



679
680
681
# File 'lib/scraperwiki-api/matchers.rb', line 679

def failure_predicate
  "aren't unique"
end

#mismatchesObject



644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'lib/scraperwiki-api/matchers.rb', line 644

def mismatches
  counts = Hash.new 0
  if @subfield
    items.each do |item|
      unless blank? item[@field]
        v = Yajl::Parser.parse item[@field]
        if Hash === v
          unless blank? v[@subfield]
            counts[v[@subfield]] += 1
          end
        elsif Array === v
          v.each do |w|
            if Hash === w
              unless blank? w[@subfield]
                counts[w[@subfield]] += 1
              end
            else
              raise NotImplementerError, 'Can only handle subfields that are hashes or arrays of hashes'
            end
          end
        else
          raise NotImplementerError, 'Can only handle subfields that are hashes or arrays of hashes'
        end
      end
    end
  else
    items.each do |item|
      unless blank? item[@field]
        counts[item[@field]] += 1
      end
    end
  end
  counts.select{|_,count| count > 1}.keys
end

#negative_failure_predicateObject



683
684
685
# File 'lib/scraperwiki-api/matchers.rb', line 683

def negative_failure_predicate
  'are unique'
end