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:



41
42
43
44
# File 'lib/dm-sphinx-adapter/resource.rb', line 41

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.



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

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.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dm-sphinx-adapter/resource.rb', line 57

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>



100
101
102
# File 'lib/dm-sphinx-adapter/resource.rb', line 100

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

#sphinx_fields(repository_name = default_repository_name) ⇒ Object

List of properties (aka sphinx fields).

This list will be the inverse of properties not declared as attributes.

Returns



108
109
110
# File 'lib/dm-sphinx-adapter/resource.rb', line 108

def sphinx_fields(repository_name = default_repository_name)
  properties(repository_name).reject{|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>



75
76
77
# File 'lib/dm-sphinx-adapter/resource.rb', line 75

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