Sha256: 33e01f2756de30d578adad61aef131d1b6a12d664c7c22200ef416580aefcfbb

Contents?: true

Size: 1.17 KB

Versions: 9

Compression:

Stored size: 1.17 KB

Contents

module Scrivito

class ObjSearchBuilder < Struct.new(:query)

  def build
    reset_enumerator

    set_predicates
    set_offset
    set_order
    set_batch_size
    set_include_deleted

    enumerator
  end

  private

  def reset_enumerator
    @enumerator = nil
  end

  def enumerator
    @enumerator ||= ObjSearchEnumerator.new(Workspace.current)
  end

  def set_predicates
    query[:predicates].each do |p|
      values = deserialize_values(p[:value])
      if p[:negate]
        enumerator.and_not(p[:field], p[:operator], values)
      else
        enumerator.and(p[:field], p[:operator], values, p[:boost])
      end
    end
  end

  def set_offset
    enumerator.offset(query[:offset].to_i) if query[:offset]
  end

  def set_order
    enumerator.order(Hash[*query[:order]]) if query[:order]
  end

  def set_batch_size
    enumerator.batch_size(query[:batch_size]) if query[:batch_size]
  end

  def set_include_deleted
    enumerator.include_deleted if query[:include_deleted]
  end

  def deserialize_values(values)
    values.map do |(type, value)|
      if type == 'date'
        DateAttribute.deserialize_from_client(value)
      else
        value
      end
    end
  end
end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
scrivito_sdk-1.2.0 lib/scrivito/obj_search_builder.rb
scrivito_sdk-1.2.0.rc3 lib/scrivito/obj_search_builder.rb
scrivito_sdk-1.2.0.rc2 lib/scrivito/obj_search_builder.rb
scrivito_sdk-1.2.0.rc1 lib/scrivito/obj_search_builder.rb
scrivito_sdk-1.1.1 lib/scrivito/obj_search_builder.rb
scrivito_sdk-1.1.0 lib/scrivito/obj_search_builder.rb
scrivito_sdk-1.1.0.rc3 lib/scrivito/obj_search_builder.rb
scrivito_sdk-1.1.0.rc2 lib/scrivito/obj_search_builder.rb
scrivito_sdk-1.1.0.rc1 lib/scrivito/obj_search_builder.rb