Class: Sexp

Inherits:
Object
  • Object
show all
Includes:
SexpPath::Traverse
Defined in:
lib/sexp_path.rb

Overview

SexpPath extends Sexp with Traverse. This adds support for searching S-Expressions

Direct Known Subclasses

SexpPath::Matcher::Base

Instance Method Summary collapse

Methods included from SexpPath::Traverse

#capture_as, #replace_sexp, #search, #search_each

Instance Method Details

#satisfy?(o, data = {}) ⇒ Boolean

Extends Sexp to allow any Sexp to be used as a SexpPath matcher

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sexp_path.rb', line 53

def satisfy?(o, data={})
  return false unless o.is_a? Sexp
  return false unless length == o.length || (last.is_a?(Sexp) && last.greedy?)
  
  each_with_index do |child,i|
    if child.is_a?(Sexp)
      candidate = child.greedy? ? o[i..-1] : o[i]
      return false unless child.satisfy?( candidate, data )
    else
      return false unless child == o[i]
    end
  end

  capture_match(o, data)
end