Sha256: cb9e3502ae544c9da5aefa84a711d597aff79b2cbb9f233bbc5f7a59a6f061d4

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module Minidusen
  class Syntax

    def initialize
      @scopers = {}
    end

    def learn_field(field, &scoper)
      field = field.to_s
      @scopers[field] = scoper
    end

    def search(instance, root_scope, query)
      query = parse(query) if query.is_a?(String)
      query = query.condensed
      matches = apply_query(instance, root_scope, query.include)
      if query.exclude.any?
        matches = append_excludes(instance, matches, query.exclude)
      end
      matches
    end

    def fields
      @scopers
    end

    def parse(query)
      Parser.parse(query)
    end

    private

    NONE = lambda do |scope, *args|
      scope.where('1=2')
    end

    def apply_query(instance, root_scope, query)
      scope = root_scope
      query.each do |token|
        scoper = @scopers[token.field] || NONE
        scope = instance.instance_exec(scope, token.value, &scoper)
      end
      scope
    end

    def append_excludes(instance, matches, exclude_query)
      excluded_records = apply_query(instance, matches.origin_class, exclude_query)
      qualified_id_field = Util.qualify_column_name(excluded_records, excluded_records.primary_key)
      exclude_sql = "#{qualified_id_field} NOT IN (#{excluded_records.select(qualified_id_field).to_sql})"
      matches.where(exclude_sql)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
minidusen-0.8.0 lib/minidusen/syntax.rb