class Filigree::DestructuringPattern
A pattern that matches an instance of a class and destructures it so that the values contained by the object may be matched upon.
Attributes
@return [Class]
Public Class Methods
Create a new destructuring pattern.
@param [Class] klass Class to match instances of. It must be destructurable. @param [Object] pattern Pattern elements to use in matching the object's values
Filigree::MultipleObjectPattern::new
# File lib/filigree/match.rb, line 595 def initialize(klass, pattern) @klass = klass super(pattern) end
Public Instance Methods
Specialized version of the bi-directional comparison operator.
@param [BasicPattern] other Right-hand side of the comparison
@return [Integer] Value corresponding to less than, equal to, or
greater than the right-hand side pattern.
# File lib/filigree/match.rb, line 573 def <=>(other) if other.is_a?(DestructuringPattern) if self.klass == other.klass base_compare(other) do self.pattern.zip(other.pattern).inject(0) do |total, pair| total + (pair.first <=> pair.last) end / self.pattern.length end elsif self.klass.subclass_of?(other.klass) then 1 else -1 end else super end end
Test to see if the object is an instance of the appropriate class, and if so destructure it and test it's values against the sub-pattern elements.
@param [Object] object Object
to test pattern against @param [Object] env Binding environment
@return [Boolean]
Filigree::MultipleObjectPattern#match?
# File lib/filigree/match.rb, line 608 def match?(object, env) object.is_a?(@klass) and super(object.destructure(@pattern.length), env) end
# File lib/filigree/match.rb, line 612 def weight 1 end