Class: Sexp::Pattern

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

Overview

Matches any atom who’s string representation matches the patterns passed in.

examples:

s(:a) / s{ m('a') }                                      #=> [s(:a)]
s(:a) / s{ m(/\w/,/\d/) }                                #=> [s(:a)]
s(:tests, s(s(:test_a), s(:test_b))) / s{ m(/test_\w/) } #=> [s(:test_a),

TODO: maybe don’t require non-sexps? This does respond to =~ now.

Instance Attribute Summary collapse

Attributes inherited from Sexp

#comments, #file, #line

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, #find_and_replace_all, #find_node, #find_nodes, from_array, #gsub, include, #line_max, m, #map, #mass, #method_missing, #new, not?, #replace_sexp, #respond_to?, s, #search_each, #sexp_body, #sexp_body=, #sexp_type, #sexp_type=, #shift, #structure, #sub, t, #to_a

Constructor Details

#initialize(pattern) ⇒ Pattern

Create a Patten matcher which will match any atom that either matches the input pattern.



1183
1184
1185
# File 'lib/sexp.rb', line 1183

def initialize pattern
  @pattern = pattern
end

Dynamic Method Handling

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

Instance Attribute Details

#patternObject (readonly)

The regexp to match for the pattern.



1173
1174
1175
# File 'lib/sexp.rb', line 1173

def pattern
  @pattern
end

Instance Method Details

#==(o) ⇒ Object

:nodoc:



1175
1176
1177
# File 'lib/sexp.rb', line 1175

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

#inspectObject

:nodoc:



1194
1195
1196
# File 'lib/sexp.rb', line 1194

def inspect # :nodoc:
  "m(%p)" % pattern
end

#pretty_print(q) ⇒ Object

:nodoc:



1198
1199
1200
1201
1202
# File 'lib/sexp.rb', line 1198

def pretty_print q # :nodoc:
  q.group 1, "m(", ")" do
    q.pp pattern
  end
end

#satisfy?(o) ⇒ Boolean

Satisfied if o is an atom, and o matches pattern

Returns:

  • (Boolean)


1190
1191
1192
# File 'lib/sexp.rb', line 1190

def satisfy? o
  !o.kind_of?(Sexp) && o.to_s =~ pattern # TODO: question to_s
end