Module: DataMapper::SphinxResource::ClassMethods

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(model) ⇒ Object

:nodoc:



30
31
32
33
# File 'lib/dm-sphinx-adapter/sphinx_resource.rb', line 30

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.

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(Symbol => String)) (defaults to: {})

    a hash of available options

See Also:



71
72
73
74
75
76
77
# File 'lib/dm-sphinx-adapter/sphinx_resource.rb', line 71

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 = SphinxAttribute.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.

Parameters:

  • name (Symbol)

    the name of a sphinx index to search for this resource

  • options (Hash(Symbol => String)) (defaults to: {})

    a hash of available options

See Also:



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dm-sphinx-adapter/sphinx_resource.rb', line 44

def index(name, options = {})
  index   = SphinxIndex.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.



81
82
83
# File 'lib/dm-sphinx-adapter/sphinx_resource.rb', line 81

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

#sphinx_indexes(repository_name = default_repository_name) ⇒ Object

List of declared sphinx indexes for this model.



60
61
62
# File 'lib/dm-sphinx-adapter/sphinx_resource.rb', line 60

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