Class: Oedipus::DataMapper::Index

Inherits:
Object
  • Object
show all
Includes:
Conversions, Pagination
Defined in:
lib/oedipus/data_mapper/index.rb

Overview

Provides a gateway between a DataMapper model and Oedipus.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Pagination

#build_pager, #extract_pager_options, #pageable?

Methods included from Conversions

#convert_filters

Constructor Details

#initialize(model, options = {}) {|_self| ... } ⇒ Index

Initialize a new Index for the given model.

Parameters:

  • model (DataMapper::Model)

    the model stored in the sphinx index

  • options (Hash) (defaults to: {})

    additonal configuration options

  • [Symbol] (Hash)

    a customizable set of options

  • [Connection] (Hash)

    a customizable set of options

Yields:

  • (_self)

Yield Parameters:



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/oedipus/data_mapper/index.rb', line 39

def initialize(model, options = {})
  @model       = model
  @name        = options[:name]       || model.storage_name
  @connection  = options[:connection] || Oedipus::DataMapper.connection
  @mappings    = {}
  @key         = model.key.first.name

  map(:id, with: @key)

  yield self if block_given?
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



19
20
21
# File 'lib/oedipus/data_mapper/index.rb', line 19

def connection
  @connection
end

#modelObject (readonly)

Returns the value of attribute model.



17
18
19
# File 'lib/oedipus/data_mapper/index.rb', line 17

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/oedipus/data_mapper/index.rb', line 18

def name
  @name
end

Instance Method Details

#delete(resource) ⇒ Fixnum

Delete the given resource from a realtime index.

Parameters:

  • resource (DataMapper::Resource)

    an instance of the model this index manages

Returns:

  • (Fixnum)

    the number of resources updated (currently always 1 or 0)



108
109
110
111
112
113
114
# File 'lib/oedipus/data_mapper/index.rb', line 108

def delete(resource)
  unless id = @mappings[:id][:get].call(resource)
    raise ArgumentError, "Attempted to delete a record without an ID"
  end

  raw.delete(id)
end

#fetch(id) ⇒ DataMapper::Resource

Fetch a specific record from the index, or nil if it’s not in the index.

Parameters:

  • id (Fixnum)

    the id of the resource to fetch

Returns:

  • (DataMapper::Resource)

    the resource, or nil if not found



210
211
212
# File 'lib/oedipus/data_mapper/index.rb', line 210

def fetch(id)
  build_resource(raw.fetch(id))
end

#insert(resource) ⇒ Fixnum

Insert the given resource into a realtime index.

Fields and attributes will be read from any configured mappings.

Parameters:

  • resource (DataMapper::Resource)

    an instance of the model this index manages

Returns:

  • (Fixnum)

    the number of resources inserted (currently always 1)



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/oedipus/data_mapper/index.rb', line 68

def insert(resource)
  record = @mappings.inject({}) do |r, (k, mapping)|
    r.merge!(k => mapping[:get].call(resource))
  end

  unless id = record.delete(:id)
    raise ArgumentError, "Attempted to insert a record without an ID"
  end

  raw.insert(id, record)
end

#map(attr, options = {}) ⇒ Object

Map an attribute in the index with a property on the model.

Parameters:

  • attr (Symbol)

    the attribute in the sphinx index

  • options (Hash) (defaults to: {})

    mapping options

  • [Symbol] (Hash)

    a customizable set of options

  • [Proc] (Hash)

    a customizable set of options



232
233
234
# File 'lib/oedipus/data_mapper/index.rb', line 232

def map(attr, options = {})
  @mappings[attr.to_sym] = normalize_mapping(attr, options.dup)
end

#multi_search(searches) ⇒ Hash

Perform multiple unrelated searches on the index.

Accepts a Hash of varying searches and returns a Hash of results.

Parameters:

  • searches (Hash)

    a Hash, whose keys are named searches and whose values are arguments to #search

Returns:

  • (Hash)

    a Hash whose keys are the same as the inputs and whose values are the corresponding results

Raises:

  • (ArgumentError)


193
194
195
196
197
198
199
200
201
# File 'lib/oedipus/data_mapper/index.rb', line 193

def multi_search(searches)
  raise ArgumentError, "Argument 1 for #multi_search must be a Hash" unless Hash === searches

  raw.multi_search(
    searches.inject({}) { |o, (k, v)| o.merge!(k => convert_filters(v)) }
  ).inject({}) { |o, (k, v)|
    o.merge!(k => build_collection(v))
  }
end

#rawOedipus::Index

Returns the underlying Index, for carrying out low-level operations.

Returns:

  • (Oedipus::Index)

    the underlying Index, used by Oedipus



55
56
57
# File 'lib/oedipus/data_mapper/index.rb', line 55

def raw
  @raw ||= connection[name]
end

#replace(resource) ⇒ Fixnum

Fully replace the given resource in a realtime index.

Fields and attributes will be read from any configured mappings.

Parameters:

  • resource (DataMapper::Resource)

    an instance of the model this index manages

Returns:

  • (Fixnum)

    the number of resources replaced (currently always 1)



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/oedipus/data_mapper/index.rb', line 125

def replace(resource)
  record = @mappings.inject({}) do |r, (k, mapping)|
    r.merge!(k => mapping[:get].call(resource))
  end

  unless id = record.delete(:id)
    raise ArgumentError, "Attempted to replace a record without an ID"
  end

  raw.replace(id, record)
end

#search(*args) ⇒ Collecton

Perform a fulltext and/or attribute search.

This method searches in the sphinx index, using Oedipus then returns the corresponding collection of DataMapper records.

No query is issued directly to the DataMapper repository, though only the handled attributes will be loaded, meaning lazy-loading will occur should any other attributes be accessed.

A faceted search may be performed by passing in the :facets option. All facets are returned via a #facets accessor on the collection.

Parameters:

  • fulltext_query (String)

    a fulltext query to send to sphinx, optional

  • options (Hash)

    options for filtering, facets, sorting and range-limiting

  • [Array] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

  • [Fixnum] (Hash)

    a customizable set of options

  • [Object] (Hash)

    a customizable set of options

Returns:

  • (Collecton)

    a collection object containing the given resources



176
177
178
179
180
# File 'lib/oedipus/data_mapper/index.rb', line 176

def search(*args)
  filters       = convert_filters(args)
  pager_options = extract_pager_options(filters)
  build_collection(raw.search(*filters).merge(pager_options: pager_options))
end

#update(resource) ⇒ Fixnum

Update the given resource in a realtime index.

Fields and attributes will be read from any configured mappings.

Parameters:

  • resource (DataMapper::Resource)

    an instance of the model this index manages

Returns:

  • (Fixnum)

    the number of resources updated (currently always 1 or 0)



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/oedipus/data_mapper/index.rb', line 89

def update(resource)
  record = @mappings.inject({}) do |r, (k, mapping)|
    r.merge!(k => mapping[:get].call(resource))
  end

  unless id = record.delete(:id)
    raise ArgumentError, "Attempted to update a record without an ID"
  end

  raw.update(id, record)
end