Class: Filigree::BasicPattern

Inherits:
Object
  • Object
show all
Extended by:
AbstractClass
Defined in:
lib/filigree/match.rb

Overview

This class provides the basis for all match patterns.

Instance Method Summary collapse

Methods included from AbstractClass

abstract_method, extended, install_icvars, new

Instance Method Details

#as(binding_pattern) ⇒ Object

Wraps this pattern in a Filigree::BindingPattern, causing the object that this pattern matches to be bound to this name in the with block.

Parameters:

  • binding_pattern (BindingPattern)

    Binding pattern containing the name



273
274
275
# File 'lib/filigree/match.rb', line 273

def as(binding_pattern)
	binding_pattern.tap { |bp| bp.pattern_elem = self }
end

#match_pattern_element(pattern_elem, object, env) ⇒ Boolean

Test to see if a single object matches a single pattern, using the given environment to store bindings.

Parameters:

  • pattern_elem (Object)

    Object representing the pattern

  • object (Object)

    Object to be matched

  • env (Object)

    Environment in which to store bindings

Returns:

  • (Boolean)

    If the pattern element matched



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/filigree/match.rb', line 285

def match_pattern_element(pattern_elem, object, env)
	case pattern_elem
	when Class
		object.is_a?(pattern_elem)
	
	when Regexp
		(object.is_a?(String) and (md = pattern_elem.match(object))).tap do |match|
			env.send("match_data=", md) if match
		end
	
	when BasicPattern
		pattern_elem.match?(object, env)
		 
	else
		object == pattern_elem
	end
end