Class: ScraperWiki::API::Matchers::FieldMatcher

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

Instance Method Summary collapse

Methods inherited from DatastoreMatcher

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

#at(subfield) ⇒ Object



459
460
461
462
# File 'lib/scraperwiki-api/matchers.rb', line 459

def at(subfield)
  @subfield = subfield
  self
end

#blank?(v) ⇒ Boolean

Returns:

  • (Boolean)


514
515
516
# File 'lib/scraperwiki-api/matchers.rb', line 514

def blank?(v)
  v.respond_to?(:empty?) ? v.empty? : !v
end

#failure_descriptionObject



518
519
520
521
522
523
524
# File 'lib/scraperwiki-api/matchers.rb', line 518

def failure_description
  if @subfield
    "#{@field}:#{@subfield} values #{failure_predicate}"
  else
    "#{@field} values #{failure_predicate}"
  end
end

#failure_predicateObject

Raises:

  • (NotImplementerError)


534
535
536
# File 'lib/scraperwiki-api/matchers.rb', line 534

def failure_predicate
  raise NotImplementerError, 'Subclasses must implement this method'
end

#in(field) ⇒ Object



454
455
456
457
# File 'lib/scraperwiki-api/matchers.rb', line 454

def in(field)
  @field = field
  self
end

#matcher(meth) ⇒ Object

Note:

@subfield can be a hash or an array of hashes



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/scraperwiki-api/matchers.rb', line 465

def matcher(meth)
  if @subfield
    items.send(meth) do |item|
      if blank? item[@field]
        meth == :reject
      else
        v = Yajl::Parser.parse item[@field]
        if Hash === v
          if blank? v[@subfield]
            meth == :reject
          else
            match? v[@subfield]
          end
        elsif Array === v
          v.all? do |w|
            if Hash === w
              if blank? w[@subfield]
                meth == :reject
              else
                match? w[@subfield]
              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.send(meth) do |item|
      if blank? item[@field]
        meth == :reject
      else
        match? item[@field]
      end
    end
  end
end

#matchesObject



506
507
508
# File 'lib/scraperwiki-api/matchers.rb', line 506

def matches
  matcher :select
end

#mismatchesObject



510
511
512
# File 'lib/scraperwiki-api/matchers.rb', line 510

def mismatches
  matcher :reject
end

#negative_failure_descriptionObject



526
527
528
529
530
531
532
# File 'lib/scraperwiki-api/matchers.rb', line 526

def negative_failure_description
  if @subfield
    "#{@field}:#{@subfield} values #{negative_failure_predicate}"
  else
    "#{@field} values #{negative_failure_predicate}"
  end
end

#negative_failure_predicateObject

Raises:

  • (NotImplementerError)


538
539
540
# File 'lib/scraperwiki-api/matchers.rb', line 538

def negative_failure_predicate
  raise NotImplementerError, 'Subclasses must implement this method'
end