Class: ScraperWiki::API::Matchers::FieldKeyMatcher

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, #mismatches, #negative_failure_description

Methods inherited from DatastoreMatcher

#does_not_match?, #failure_description, #failure_message, #failure_size, #failures, #items, #matches, #matches?, #mismatches, #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

#difference(v) ⇒ Object

Raises:

  • (NotImplementerError)


725
726
727
# File 'lib/scraperwiki-api/matchers.rb', line 725

def difference(v)
  raise NotImplementerError, 'Subclasses must implement this method'
end

#failure_predicateObject



729
730
731
# File 'lib/scraperwiki-api/matchers.rb', line 729

def failure_predicate
  "#{predicate}: #{difference.join ', '}"
end

#match?(v) ⇒ Boolean

Returns:

  • (Boolean)


708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
# File 'lib/scraperwiki-api/matchers.rb', line 708

def match?(v)
  w = Yajl::Parser.parse v
  if Hash === w
    difference(w).empty?
  elsif Array === w
    w.all? do |x|
      if Hash === x
        difference(x).empty?
      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

#negative_failure_predicateObject



733
734
735
# File 'lib/scraperwiki-api/matchers.rb', line 733

def negative_failure_predicate
  "#{negative_predicate}: #{difference.join ', '}"
end