Module: DataMapper::Adapters::Sphinx::Resource::ClassMethods

Defined in:
lib/dm-sphinx-adapter/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(model) ⇒ Object

:nodoc:



32
33
34
35
# File 'lib/dm-sphinx-adapter/resource.rb', line 32

def self.extended(model) #:nodoc:
  model.instance_variable_set(:@sphinx_indexes, {})
  model.instance_variable_set(:@sphinx_attributes, {})
end

Instance Method Details

#attribute(name, type, options = {}) ⇒ Object

Defines a sphinx attribute on the resource.

See

DataMapper::Adapters::Sphinx::Attribute

Parameters

name<Symbol>

The name of a sphinx attribute to order/restrict by for this resource.

type<Class>

The type to define this attribute as.

options<Hash>

An optional hash of attribute options.



79
80
81
82
83
84
85
# File 'lib/dm-sphinx-adapter/resource.rb', line 79

def attribute(name, type, options = {})
  # Attributes are just properties without a getter/setter in the model.
  # This keeps DataMapper::Query happy when building queries.
  attribute = Sphinx::Attribute.new(self, name, type, options)
  properties(repository_name)[attribute.name] = attribute
  attribute
end

#index(name, options = {}) ⇒ Object

Defines a sphinx index on the resource.

Indexes are naturally ordered, with delta indexes at the end of the list so that duplicate document IDs in delta indexes override your main indexes.

See

  • DataMapper::Adapters::Sphinx::Index

Parameters

name<Symbol>

The name of a sphinx index to search for this resource.

options<Hash>

A hash of available index options.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dm-sphinx-adapter/resource.rb', line 48

def index(name, options = {})
  index   = Index.new(self, name, options)
  indexes = sphinx_indexes(repository_name)
  indexes << index

  # TODO: I'm such a Ruby nub. In the meantime I've gone back to my Perl roots.
  # This is a Schwartzian transform to sort delta indexes to the bottom and natural sort by name.
  mapped  = indexes.map{|i| [(i.delta? ? 1 : 0), i.name, i]}
  sorted  = mapped.sort{|a, b| a[0] <=> b[0] || a[1] <=> b[1]}
  indexes.replace(sorted.map{|i| i[2]})

  index
end

#sphinx_attributes(repository_name = default_repository_name) ⇒ Object

List of declared sphinx attributes for this model.

Returns

Array<DataMapper::Adapters::Sphinx::Attribute>



91
92
93
# File 'lib/dm-sphinx-adapter/resource.rb', line 91

def sphinx_attributes(repository_name = default_repository_name)
  properties(repository_name).grep{|p| p.kind_of? Sphinx::Attribute}
end

#sphinx_indexes(repository_name = default_repository_name) ⇒ Object

List of declared sphinx indexes for this model.

Returns

Array<DataMapper::Adapters::Sphinx::Index>



66
67
68
# File 'lib/dm-sphinx-adapter/resource.rb', line 66

def sphinx_indexes(repository_name = default_repository_name)
  @sphinx_indexes[repository_name] ||= []
end