Sha256: f4c00d353e68a4f5d34364d9df67b2b3ef1aa9fc1047a9a276aee6a875862ce1

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

module DiviningRod
  class Mappings

    class << self

      attr_accessor :root_definition

      def define(opts = {})
        @root_definition = Definition.new(opts) { true }
        yield Mappings.new(@root_definition)
        @root_definition
      end

      def evaluate(obj)
        @root_definition.evaluate(obj)
      end

    end

    attr_reader :default_opts, :parent

    def initialize(parent, default_opts = {})
      @parent = parent
      unless default_opts == {}
        p "Passed in default_opts"
        p default_opts
      end
      @default_opts = Mash.new(default_opts)
    end

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

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

    def with_options(opts)
      yield self.class.new(parent, opts)
    end

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

    private

    def merged_opts(opts)
      opts = default_opts.merge(opts)
      opts = parent.opts.merge(opts)
      opts
    end

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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
divining_rod-0.6.3 lib/divining_rod/mappings.rb