Class: Oedipus::DataMapper::Index
- Inherits:
-
Object
- Object
- Oedipus::DataMapper::Index
- 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
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#delete(resource) ⇒ Fixnum
Delete the given resource from a realtime index.
-
#fetch(id) ⇒ DataMapper::Resource
Fetch a specific record from the index, or nil if it’s not in the index.
-
#initialize(model, options = {}) {|_self| ... } ⇒ Index
constructor
Initialize a new Index for the given model.
-
#insert(resource) ⇒ Fixnum
Insert the given resource into a realtime index.
-
#map(attr, options = {}) ⇒ Object
Map an attribute in the index with a property on the model.
-
#multi_search(searches) ⇒ Hash
Perform multiple unrelated searches on the index.
-
#raw ⇒ Oedipus::Index
Returns the underlying Index, for carrying out low-level operations.
-
#replace(resource) ⇒ Fixnum
Fully replace the given resource in a realtime index.
-
#search(*args) ⇒ Collecton
Perform a fulltext and/or attribute search.
-
#update(resource) ⇒ Fixnum
Update the given resource in a realtime index.
Methods included from Pagination
#build_pager, #extract_pager_options, #pageable?
Methods included from Conversions
Constructor Details
#initialize(model, options = {}) {|_self| ... } ⇒ Index
Initialize a new Index for the given model.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/oedipus/data_mapper/index.rb', line 39 def initialize(model, = {}) @model = model @name = [:name] || model.storage_name @connection = [:connection] || Oedipus::DataMapper.connection @mappings = {} @key = model.key.first.name map(:id, with: @key) yield self if block_given? end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
19 20 21 |
# File 'lib/oedipus/data_mapper/index.rb', line 19 def connection @connection end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
17 18 19 |
# File 'lib/oedipus/data_mapper/index.rb', line 17 def model @model end |
#name ⇒ Object (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.
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.
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.
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.
232 233 234 |
# File 'lib/oedipus/data_mapper/index.rb', line 232 def map(attr, = {}) @mappings[attr.to_sym] = normalize_mapping(attr, .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.
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 |
#raw ⇒ Oedipus::Index
Returns the underlying Index, for carrying out low-level operations.
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.
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.
176 177 178 179 180 |
# File 'lib/oedipus/data_mapper/index.rb', line 176 def search(*args) filters = convert_filters(args) = (filters) build_collection(raw.search(*filters).merge(pager_options: )) end |
#update(resource) ⇒ Fixnum
Update the given resource in a realtime index.
Fields and attributes will be read from any configured mappings.
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 |