123456789_123456789_123456789_123456789_123456789_

Class: Rinda::Template

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Tuple
Instance Chain:
self, Tuple
Inherits: Rinda::Tuple
Defined in: lib/rinda/rinda.rb

Overview

Templates are used to match tuples in ::Rinda.

Class Method Summary

Tuple - Inherited

.new

Creates a new Tuple from ary_or_hash which must be an Array or Hash.

Instance Method Summary

Tuple - Inherited

#[]

Accessor method for elements of the tuple.

#each

Iterate through the tuple, yielding the index or key, and the value, thus ensuring arrays are iterated similarly to hashes.

#fetch

Fetches item k from the tuple.

#size

The number of elements in the tuple.

#value

Return the tuple itself.

#hash?,
#init_with_ary

Munges ary into a valid Tuple.

#init_with_hash

Ensures hash is a valid Tuple.

Constructor Details

This class inherits a constructor from Rinda::Tuple

Instance Method Details

#===(tuple)

Alias for #match.

[ GitHub ]

  
# File 'lib/rinda/rinda.rb', line 171

def ===(tuple)
  match(tuple)
end

#match(tuple)

Matches this template against tuple. The tuple must be the same size as the template. An element with a nil value in a template acts as a wildcard, matching any value in the corresponding position in the tuple. Elements of the template match the tuple if the are #== or #===.

Template.new([:foo, 5]).match   Tuple.new([:foo, 5]) # => true
Template.new([:foo, nil]).match Tuple.new([:foo, 5]) # => true
Template.new([String]).match    Tuple.new(['hello']) # => true

Template.new([:foo]).match      Tuple.new([:foo, 5]) # => false
Template.new([:foo, 6]).match   Tuple.new([:foo, 5]) # => false
Template.new([:foo, nil]).match Tuple.new([:foo])    # => false
Template.new([:foo, 6]).match   Tuple.new([:foo])    # => false
[ GitHub ]

  
# File 'lib/rinda/rinda.rb', line 150

def match(tuple)
  return false unless tuple.respond_to?(:size)
  return false unless tuple.respond_to?(:fetch)
  return false unless self.size == tuple.size
  each do |k, v|
    begin
      it = tuple.fetch(k)
    rescue
      return false
    end
    next if v.nil?
    next if v == it
    next if v === it
    return false
  end
  return true
end