Class: Elasticity::SegmentedDocument

Inherits:
BaseDocument show all
Defined in:
lib/elasticity/segmented_document.rb

Instance Attribute Summary

Attributes inherited from BaseDocument

#_explanation, #_id, #_score, #highlighted, #highlighted_attrs, #matched_queries, #sort

Class Method Summary collapse

Methods inherited from BaseDocument

#==, #attributes=, configure, #created?, #delete, #to_document, #update

Class Method Details

.segment(segment_name) ⇒ Object

Creates a new segment which behaves almost the same as a Document class dynamically configured to access a segmented index.

It creates a new class in runtime that inherits from your defined class, allowing methods defined in your class to be callable from the dynamic class.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/elasticity/segmented_document.rb', line 8

def self.segment(segment_name)
  qn = segment_name.camelize

  klass = Class.new(self) do
    class_attribute :mapper, :segment_name
    IndexMapper.set_delegates(singleton_class, :mapper)

    def self.inspect
      "#{superclass.name}{\"#{segment_name}\"}"
    end

    def inspect
      ivars = instance_variables.map do |name|
        "#{name}=#{instance_variable_get(name).inspect}"
      end

      "#<#{self.class.inspect}:0x#{object_id.to_s(15)} #{ivars.join(" ")}>"
    end
  end

  klass.segment_name = segment_name
  klass.mapper = IndexMapper.new(klass, config.segment(segment_name))
  klass
end