Sha256: bbafb796e99b641c6bf9f0a7aca32eb26d051c06d809eca1b30d4c80dd1773f2

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

module DiviningRod
  class Mapper
    include Murge
    
    def initialize(parent, default_opts = {})
      @parent = parent
      @default_opts = default_opts
    end

    def pattern(type, pattern, opts = {})
      opts = murge(default_options, opts)
      definition = Matchers.send(type.to_sym, pattern, opts)
      append_to_parent(definition)
      if block_given?
        yield self.class.new(definition)
      end
      definition
    end
    
    def with_options(opts)
      yield self.class.new(@parent, opts)
    end

    def default(opts = {})
      definition = Definition.new(opts) { true }
      append_to_parent(definition)
      definition
    end

    def method_missing(meth, *args, &blk)
      # Lets us use map.ua instead of map.pattern :ua
      if Matchers.respond_to?(meth.to_sym)
        self.pattern(meth, args[0], args[1,], &blk)
      else
        super
      end
    end

    private
    
      def default_options
        @default_opts || {}
      end

      def append_to_parent(definition)
        if @parent
          @parent.children << definition
        end
        definition
      end

  end


end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
divining_rod-0.5.0 lib/divining_rod/mapper.rb
divining_rod-0.4.0 lib/divining_rod/mapper.rb