Module: Zeng::Indexer

Defined in:
lib/zeng/indexer.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Constant Summary collapse

Map =
Class.new
Collection =
Class.new(Array)

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
# File 'lib/zeng/indexer.rb', line 6

def self.included(base)
  base.extend ClassMethods
  base.send :include, InstanceMethods
end

.map(class_attrs = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/zeng/indexer.rb', line 74

def map(class_attrs={})
  c = class_attrs.keys[0]
  attrs = class_attrs.values.flatten
  key = "#{c}-index-#{attrs.join('-')}"
  index_map = Map.send(:include, self)
  index_map.connect(c)
  klass = class << c; self; end
  klass.instance_eval {
    define_method(:indexes){
      map = index_map.find_or_create(key)
      map.index_attrs = attrs
      map.objects
    }
    attrs.each {|attr|
      define_method("find_all_by_#{attr}"){|value|
        c.indexes.map {|index|
          c.find(index[-1]['id']) if index[-1][attr.to_s] == value
        }.compact
      }
    }
  }
  true
end