Sha256: 6bc43e5eead56241cbe3c695c85c2e29eb167be5ae4d59a398b922fbb87f41a3

Contents?: true

Size: 943 Bytes

Versions: 2

Compression:

Stored size: 943 Bytes

Contents

module Funkr

  # Should not be used directly
  module Matchers

    class SafeMatcher
      
      def initialize(match)
        @const, *@data = match
        @runner = nil
        @undefined = self.class.constructs.clone
      end
      
      def self.build_matchers(constructs)
        @constructs = constructs
        constructs.each do |c|
          name, *_ = c
          define_method(name) do |&b|
            @undefined.delete(name)
            if @const == name then
              @runner = b
            end
          end
        end
      end
      
      def self.constructs; @constructs; end
      
      def self.match_with(normal_form) # &block
        m = self.new(normal_form)
        yield m
        m.run_match
      end

      def run_match
        if @undefined.any? then
          raise "Incomplete match, missing : #{@undefined.join(" ")}"
        end
        @runner.call(*@data)
      end
      
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
funkr-0.0.40 lib/funkr/adt/matcher.rb
funkr-0.0.30 lib/funkr/adt/matcher.rb