Sha256: b72b38597ad90d79ecdfddf60e78447ab8fc071f420a0b3fd4ae009b20a23e5d

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require_relative 'nodifier'
require_relative 'converter'
require_relative 'scope'
require 'facets/hash/deep_rekey'
require 'facets/array/recurse'

module ConceptQL
  # Tree is used to walk through a ConceptQL statement, instantiate
  # all operators, and then provide access to the root operator
  class Tree
    attr :nodifier, :behavior, :defined, :opts, :temp_tables, :scope
    def initialize(opts = {})
      @nodifier = opts.fetch(:nodifier, Nodifier.new)
      @behavior = opts.fetch(:behavior, nil)
      @defined = {}
      @temp_tables = {}
      @opts = {}
      @scope = opts.fetch(:scope, Scope.new)
    end

    def root(query)
      @root ||= start_traverse(query.statement)
    end

    private

    def start_traverse(stmt)
      case stmt
      when Hash
        traverse(converter.convert(stmt))
      when Array
        traverse(stmt)
      end
    end

    def traverse(stmt)
      stmt.recurse(Array, Hash) do |arr_or_hash|
        if arr_or_hash.is_a?(Array)
          type = arr_or_hash.shift
          obj = nodifier.create(scope, type, *arr_or_hash)
          obj.extend(behavior) if behavior
          obj
        else
          arr_or_hash
        end
      end
    end

    def converter
      @converter ||= Converter.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
conceptql-0.2.0 lib/conceptql/tree.rb