Parent

DataMapper::Adapters::FerretAdapter

Public Class Methods

new(name, options) click to toggle source
# File lib/ferret_adapter/adapter.rb, line 4
def initialize(name, options)
  super
  @index = unless File.extname(@options[:path]) == '.sock'
    LocalIndex.new(@options)
  else
    RemoteIndex.new(@options)
  end
end

Public Instance Methods

create(resources) click to toggle source
# File lib/ferret_adapter/adapter.rb, line 13
def create(resources)
  resources.each do |resource|
    attributes = DataMapper::Ext::Hash.to_mash(resource.attributes(:field))

    # Since we don't inspect the models before generating the indices,
    # we'll map the resource's key to the :id column.
    attributes[:id]    ||= resource.key.first
    attributes[:_type]   = resource.model.name

    @index.add attributes
  end
end
delete(collection) click to toggle source
# File lib/ferret_adapter/adapter.rb, line 41
def delete(collection)
  @index.delete dm_query_to_ferret_query(collection.query)
  1
end
read(query) click to toggle source

This returns an array of Ferret docs (glorified hashes) which can be used to instantiate objects by doc and doc

# File lib/ferret_adapter/adapter.rb, line 28
def read(query)
  fields = query.fields
  key    = query.model.key(name).first

  ferret_query = dm_query_to_ferret_query(query)

  @index.search(ferret_query, :limit => query.limit).map do |lazy_doc|
    Hash[ fields.map { |p| [ p, p.typecast(lazy_doc[p.field]) ] } ].update(
      key.field => key.typecast(lazy_doc[:id])
    )
  end
end
search(ferret_query, limit = :all) click to toggle source

This returns a hash of the resource constant and the ids returned for it from the search.

{ Story => ["1", "2"], Image => ["2"] }
# File lib/ferret_adapter/adapter.rb, line 49
def search(ferret_query, limit = :all)
  results = {}
  @index.search(ferret_query, :limit => limit).each do |doc|
    resources = results[Object.const_get(doc[:_type])] ||= []
    resources << doc[:id]
  end
  results
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.