Class: SexpPath::Matcher::Sibling

Inherits:
Base
  • Object
show all
Defined in:
lib/sexp_path/matcher/sibling.rb

Overview

See SexpPath::Matcher::Base for sibling relations: <,<<,>>,>

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#&, #-@, #>>, #|

Methods included from Traverse

#capture_as, #replace_sexp, #search, #search_each

Constructor Details

#initialize(subject, sibling, distance = nil) ⇒ Sibling

Creates a Matcher which will match any pair of Sexps that are siblings. Defaults to matching the immediate following sibling.



8
9
10
11
12
# File 'lib/sexp_path/matcher/sibling.rb', line 8

def initialize(subject, sibling, distance=nil)
  @subject = subject
  @sibling = sibling
  @distance = distance
end

Instance Attribute Details

#distanceObject (readonly)

Returns the value of attribute distance.



4
5
6
# File 'lib/sexp_path/matcher/sibling.rb', line 4

def distance
  @distance
end

#siblingObject (readonly)

Returns the value of attribute sibling.



4
5
6
# File 'lib/sexp_path/matcher/sibling.rb', line 4

def sibling
  @sibling
end

#subjectObject (readonly)

Returns the value of attribute subject.



4
5
6
# File 'lib/sexp_path/matcher/sibling.rb', line 4

def subject
  @subject
end

Instance Method Details

#inspectObject



36
37
38
# File 'lib/sexp_path/matcher/sibling.rb', line 36

def inspect
  "#{subject.inspect} >> #{sibling.inspect}"
end

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

Satisfied if o contains subject followed by sibling

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sexp_path/matcher/sibling.rb', line 15

def satisfy?(o, data={})
  # Future optimizations: 
  # * Shortcut matching sibling
  subject_matches = index_matches(subject, o)
  return nil if subject_matches.empty?
  
  sibling_matches = index_matches(sibling, o)
  return nil if sibling_matches.empty?

  subject_matches.each do |i1, data_1|
    sibling_matches.each do |i2, data_2|
      if (distance ? (i2-i1 == distance) : i2 > i1)
        data = data.merge(data_1).merge(data_2)
        return capture_match(o, data)
      end
    end
  end
  
  nil
end