Sha256: 602a0eae4de555c368e7eefc13dd30fb4aeb76eeff4988e26f8d490fbb2f6522

Contents?: true

Size: 904 Bytes

Versions: 5

Compression:

Stored size: 904 Bytes

Contents

require_relative 'nodifier'
require 'active_support/core_ext/hash'

module ConceptQL
  class Tree
    attr :nodifier, :behavior, :defined
    def initialize(opts = {})
      @nodifier = opts.fetch(:nodifier, Nodifier.new)
      @behavior = opts.fetch(:behavior, nil)
      @defined = {}
    end

    def root(*queries)
      @root ||= traverse(queries.flatten.map(&:statement).flatten.map(&:deep_symbolize_keys))
    end

    private
    def traverse(obj)
      case obj
      when Hash
        if obj.keys.length > 1
          obj = Hash[obj.map { |key, value| [ key, traverse(value) ]}]
          return obj
        end
        type = obj.keys.first
        values = traverse(obj[type])
        obj = nodifier.create(type, values, self)
        obj.extend(behavior) if behavior
        obj
      when Array
        obj.map { |value| traverse(value) }
      else
        obj
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
conceptql-0.1.0 lib/conceptql/tree.rb
conceptql-0.0.9 lib/conceptql/tree.rb
conceptql-0.0.8 lib/conceptql/tree.rb
conceptql-0.0.7 lib/conceptql/tree.rb
conceptql-0.0.6 lib/conceptql/tree.rb