Class: Sexp::Any

Inherits:
Matcher show all
Defined in:
lib/sexp_matcher.rb

Overview

Matches when any of the sub-expressions match.

This is also available via Matcher#|.

examples:

s(:a) / s{ any(s(:a), s(:b)) } #=> [s(:a)]
s(:a) / s{     s(:a) | s(:b) } #=> [s(:a)] # same thing via |
s(:a) / s{ any(s(:b), s(:c)) } #=> []

Constant Summary

Constants inherited from Sexp

UNASSIGNED

Instance Attribute Summary collapse

Attributes inherited from Sexp

#comments, #file, #line, #line_max

Instance Method Summary collapse

Methods inherited from Matcher

#&, #-@, #/, #=~, #>>, #greedy?, match_subs=, match_subs?, parse, #|

Methods inherited from Sexp

#/, #=~, _, ___, all, any, #array_type?, atom, child, #compact, #deep_each, #depth, #each_of_type, #each_sexp, #eql?, #find_and_replace_all, #find_node, #find_nodes, from_array, #gsub, #hash, include, k, m, #map, #mass, #method_missing, #new, not?, q, #replace_sexp, #respond_to?, s, #search_each, #sexp_body, #sexp_body=, #sexp_type, #sexp_type=, #shift, #structure, #sub, t, #to_a, #value

Constructor Details

#initialize(*options) ⇒ Any

Create an Any matcher which will match any of the options.



607
608
609
# File 'lib/sexp_matcher.rb', line 607

def initialize *options
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Sexp

Instance Attribute Details

#optionsObject (readonly)

The collection of sub-matchers to match against.



602
603
604
# File 'lib/sexp_matcher.rb', line 602

def options
  @options
end

Instance Method Details

#==(o) ⇒ Object

:nodoc:



620
621
622
# File 'lib/sexp_matcher.rb', line 620

def == o # :nodoc:
  super && self.options == o.options
end

#inspectObject

:nodoc:



624
625
626
# File 'lib/sexp_matcher.rb', line 624

def inspect # :nodoc:
  options.map(&:inspect).join(" | ")
end

#pretty_print(q) ⇒ Object

:nodoc:



628
629
630
631
632
633
634
# File 'lib/sexp_matcher.rb', line 628

def pretty_print q # :nodoc:
  q.group 1, "any(", ")" do
    q.seplist options do |v|
      q.pp v
    end
  end
end

#satisfy?(o) ⇒ Boolean

Satisfied when any sub expressions match o

Returns:

  • (Boolean)


614
615
616
617
618
# File 'lib/sexp_matcher.rb', line 614

def satisfy? o
  options.any? { |exp|
    Sexp === exp && exp.satisfy?(o) || exp == o
  }
end