Class: Filigree::MultipleObjectPattern

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

Overview

An abstract class that matches multiple objects to multiple patterns.

Direct Known Subclasses

DestructuringPattern, OuterPattern

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AbstractClass

abstract_method, extended, install_icvars, new

Methods inherited from BasicPattern

#<=>, #as

Constructor Details

#initialize(pattern) ⇒ MultipleObjectPattern

Create a new pattern with multiple elements.

Parameters:

  • pattern (Array<Object>)

    Array of pattern elements



465
466
467
# File 'lib/filigree/match.rb', line 465

def initialize(pattern)
	@pattern = pattern
end

Instance Attribute Details

#patternArray<BasicPattern> (readonly)

Returns:



460
461
462
# File 'lib/filigree/match.rb', line 460

def pattern
  @pattern
end

Instance Method Details

#base_compare(other) ⇒ Integer

A wrapper method to sort MultipleObjectPattern objects by their arity.

Parameters:

  • other (BasicPattern)

    Right-hand side of the comparison

Returns:

  • (Integer)

    Value corresponding to less than, equal to, or greater than the right-hand side pattern.



476
477
478
479
480
481
482
# File 'lib/filigree/match.rb', line 476

def base_compare(other)
	if self.pattern.length == other.pattern.length
		yield
	else
		self.pattern.length - other.pattern.length
	end
end

#match?(objects, env) ⇒ Boolean

Test multiple objects against multiple pattern elements.

Parameters:

  • objects (Object)

    Object to test pattern against

Returns:

  • (Boolean)


489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/filigree/match.rb', line 489

def match?(objects, env)
	if objects.length == @pattern.length
		@pattern.zip(objects).each do |pattern_elem, object|
			return false unless pattern_elem.match?(object, env)
		end

		true

	else
		(@pattern.length == 1 and @pattern.first == WildcardPattern.instance)
	end
end