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

Inherits:
DatastoreMatcher 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

#at(subfield) ⇒ Object



502
503
504
505
# File 'lib/scraperwiki-api/matchers.rb', line 502

def at(subfield)
  @subfield = subfield
  self
end

#blank?(v) ⇒ Boolean

Returns:

  • (Boolean)


557
558
559
# File 'lib/scraperwiki-api/matchers.rb', line 557

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

#failure_descriptionObject



561
562
563
564
565
566
567
# File 'lib/scraperwiki-api/matchers.rb', line 561

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

#failure_predicateObject

Raises:

  • (NotImplementerError)


577
578
579
# File 'lib/scraperwiki-api/matchers.rb', line 577

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

#in(field) ⇒ Object



497
498
499
500
# File 'lib/scraperwiki-api/matchers.rb', line 497

def in(field)
  @field = field
  self
end

#matcher(meth) ⇒ Object

Note:

+@subfield+ can be a hash or an array of hashes



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/scraperwiki-api/matchers.rb', line 508

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



549
550
551
# File 'lib/scraperwiki-api/matchers.rb', line 549

def matches
  matcher :select
end

#mismatchesObject



553
554
555
# File 'lib/scraperwiki-api/matchers.rb', line 553

def mismatches
  matcher :reject
end

#negative_failure_descriptionObject



569
570
571
572
573
574
575
# File 'lib/scraperwiki-api/matchers.rb', line 569

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)


581
582
583
# File 'lib/scraperwiki-api/matchers.rb', line 581

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