Class: Filigree::OuterPattern

Inherits:
MultipleObjectPattern show all
Defined in:
lib/filigree/match.rb

Overview

The class that contains all of the pattern elements passed to a with clause.

Instance Attribute Summary collapse

Attributes inherited from MultipleObjectPattern

#pattern

Instance Method Summary collapse

Methods inherited from MultipleObjectPattern

#base_compare

Methods included from AbstractClass

#abstract_method, extended, #install_icvars, #new

Methods inherited from BasicPattern

#as

Constructor Details

#initialize(pattern, guard, block) ⇒ OuterPattern

Create a new outer pattern with the given pattern elements, guard, and block.

Parameters:

  • pattern (Array<Object>)

    Pattern elements

  • guard (Proc)

    Guard clause that is tested if the pattern matches

  • block (Proc)

    Block to be evaluated if the pattern matches



537
538
539
540
541
# File 'lib/filigree/match.rb', line 537

def initialize(pattern, guard, block)
	super(pattern)
	@guard = guard
	@block = block
end

Instance Attribute Details

#block=(value) ⇒ Object (writeonly)

Sets the attribute block

Parameters:

  • value

    the value to set the attribute block to.



507
508
509
# File 'lib/filigree/match.rb', line 507

def block=(value)
  @block = value
end

#guardObject (readonly)

Returns the value of attribute guard.



508
509
510
# File 'lib/filigree/match.rb', line 508

def guard
  @guard
end

Instance Method Details

#<=>(other) ⇒ -1, ...

Specialized version of the bi-directional comparison operator.

Parameters:

  • other (BasicPattern)

    Right-hand side of the comparison

Returns:

  • (-1, 0, 1)

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



516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/filigree/match.rb', line 516

def <=>(other)
	base_compare(other) do
		comp_res =
		self.pattern.zip(other.pattern).inject(0) do |total, pair|
			total + (pair.first <=> pair.last)
		end <=> 0

		if comp_res == 0
			self.guard ? (other.guard ? 0 : 1) : (other.guard ? -1 : comp_res)
		else
			comp_res
		end
	end
end

#call(env, objects = []) ⇒ Object

Call the pattern’s block, passing the given objects to the block.

Parameters:

  • env (Object)

    Environment in which to evaluate the block

  • objects (Array<Object>) (defaults to: [])

    Arguments to the block



547
548
549
# File 'lib/filigree/match.rb', line 547

def call(env, objects = [])
	if @block then env.instance_exec(*objects, &@block) else nil end
end

#match?(objects, env) ⇒ Boolean

Test the objects for equality to the pattern elements.

Parameters:

  • objects (Object)

    Objects to test pattern elements against

  • env (Object)

    Binding environment

Returns:

  • (Boolean)


557
558
559
# File 'lib/filigree/match.rb', line 557

def match?(objects, env)
	super && (@guard.nil? or env.instance_exec(&@guard))
end