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

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

Instance Method Summary collapse

Methods inherited from FieldMatcher

#at, #blank?, #failure_description, #in, #matcher, #matches, #negative_failure_description

Methods inherited from DatastoreMatcher

#does_not_match?, #failure_description, #failure_message, #failure_size, #failures, #items, #matches, #matches?, #negative_failure_description, #negative_failure_message

Methods inherited from CustomMatcher

#does_not_match?, #failure_message, #initialize, #matches?, #negative_failure_message

Constructor Details

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

Instance Method Details

#failure_predicateObject



636
637
638
# File 'lib/scraperwiki-api/matchers.rb', line 636

def failure_predicate
  "aren't unique"
end

#mismatchesObject



601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/scraperwiki-api/matchers.rb', line 601

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



640
641
642
# File 'lib/scraperwiki-api/matchers.rb', line 640

def negative_failure_predicate
  'are unique'
end