Sha256: d63d526f69b33cf9ea8b998168be17f9d89b006c0324b768f4e52c22d444a797

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 KB

Contents

module Minidusen
  class Query

    include Enumerable

    attr_reader :tokens

    def initialize(initial_tokens = [])
      @tokens = initial_tokens
    end

    def <<(token)
      tokens << token
    end

    def [](index)
      tokens[index]
    end

    def size
      tokens.size
    end

    def to_s
      collect(&:to_s).join(" + ")
    end

    def each(&block)
      tokens.each(&block)
    end

    def condensed
      include_texts = include.select(&:text?).collect(&:value)
      exclude_texts = exclude.select(&:text?).collect(&:value)
      field_tokens = tokens.reject(&:text?)

      condensed_tokens = field_tokens
      if include_texts.present?
        options = { :field => 'text', :value => include_texts, :exclude => false }
        condensed_tokens << Token.new(options)
      end
      if exclude_texts.present?
        options = { :field => 'text', :value => exclude_texts, :exclude => true }
        condensed_tokens << Token.new(options)
      end
      self.class.new(condensed_tokens)
    end

    def include
      self.class.new tokens.reject(&:exclude?)
    end

    def exclude
      self.class.new tokens.select(&:exclude?)
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
minidusen-0.11.2 lib/minidusen/query.rb
minidusen-0.11.1 lib/minidusen/query.rb
minidusen-0.11.0 lib/minidusen/query.rb
minidusen-0.10.1 lib/minidusen/query.rb
minidusen-0.10 lib/minidusen/query.rb
minidusen-0.9 lib/minidusen/query.rb
minidusen-0.8.3 lib/minidusen/query.rb
minidusen-0.8.2 lib/minidusen/query.rb
minidusen-0.8.1 lib/minidusen/query.rb