Class: Filigree::BindingPattern

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

Overview

A pattern that binds a sub-pattern’s matching object to a name in the binding environment.

Instance Attribute Summary collapse

Attributes inherited from SingleObjectPattern

#pattern_elem

Instance Method Summary collapse

Methods included from AbstractClass

#abstract_method, extended, #install_icvars, #new

Methods inherited from BasicPattern

#<=>

Constructor Details

#initialize(name, pattern_elem = WildcardPattern.instance) ⇒ BindingPattern

Create a new binding pattern.

Parameters:

  • name (Symbol)

    Name to bind to

  • pattern_elem (Object) (defaults to: WildcardPattern.instance)

    Sub-pattern



431
432
433
434
# File 'lib/filigree/match.rb', line 431

def initialize(name, pattern_elem = WildcardPattern.instance)
	@name = name
	super(pattern_elem)
end

Instance Attribute Details

#pattern_elem=(value) ⇒ Object (writeonly)

Sets the attribute pattern_elem

Parameters:

  • value

    the value to set the attribute pattern_elem to.



425
426
427
# File 'lib/filigree/match.rb', line 425

def pattern_elem=(value)
  @pattern_elem = value
end

Instance Method Details

#as(_, _) ⇒ Object

Overridden method to prevent binding BindingPattern objects.



437
438
439
# File 'lib/filigree/match.rb', line 437

def as(_, _)
	raise 'Binding a BindingPattern is not allowed.'
end

#match?(object, env) ⇒ Boolean

Test the object for equality to the pattern element. Binds the object to the binding pattern’s name if it does match.

Parameters:

  • object (Object)

    Object to test pattern against

  • env (Object)

    Binding environment

Returns:

  • (Boolean)


448
449
450
# File 'lib/filigree/match.rb', line 448

def match?(object, env)
	@pattern_elem.match?(object, env).tap { |match| env.send("#{@name}=", object) if match }
end

#weightObject



452
453
454
# File 'lib/filigree/match.rb', line 452

def weight
	@pattern_elem.weight
end