Sha256: 4fc7e729c812e77d74321d0817ed7b93de1ba0f0f6224433b36ecfa39373fca7

Contents?: true

Size: 879 Bytes

Versions: 9

Compression:

Stored size: 879 Bytes

Contents

# encoding: utf-8

module Dusen
  class Syntax

    def initialize
      @scopers = {}
    end

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

    def learn_unknown_field(&unknown_scoper)
      @unknown_scoper = unknown_scoper
    end

    def search(root_scope, query)
      scope = root_scope
      query = parse(query) if query.is_a?(String)
      query = query.condensed
      query.each do |token|
        scoper = @scopers[token.field] || unknown_scoper
        scope = scoper.call(scope, token.value)
      end
      scope
    end

    def fields
      @scopers
    end

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

    private

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

    def unknown_scoper
      @unknown_scoper || DEFAULT_UNKNOWN_SCOPER
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dusen-0.4.8 lib/dusen/syntax.rb
dusen-0.4.6 lib/dusen/syntax.rb
dusen-0.4.5 lib/dusen/syntax.rb
dusen-0.4.4 lib/dusen/syntax.rb
dusen-0.4.3 lib/dusen/syntax.rb
dusen-0.4.2 lib/dusen/syntax.rb
dusen-0.4.1 lib/dusen/syntax.rb
dusen-0.4.0 lib/dusen/syntax.rb
dusen-0.3.0 lib/dusen/syntax.rb