Sha256: 6c9043168fad79c1996f1f873aac4cefb3d47e5eb341038e50ac7b2549dcc1c7

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 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 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

2 entries across 2 versions & 1 rubygems

Version Path
minidusen-0.8.0 lib/minidusen/query.rb
minidusen-0.7.0 lib/minidusen/query.rb