Module: Hyperactive::Index::Indexable::ClassMethods

Defined in:
lib/hyperactive/index.rb

Instance Method Summary collapse

Instance Method Details

#index_by(*attributes) ⇒ Object

Create an index for this class.

Will create a method find_by_#Hyperactive::Index::Indexable::ClassMethods.attributesattributes.join(“<em>and</em>”) for this class that will return what you expect.



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/hyperactive/index.rb', line 146

def index_by(*attributes)
  klass = self
  self.class.class_eval do
    define_method("find_by_#{attributes.join("_and_")}") do |*args|
      CAPTAIN[IndexBuilder.get_key(klass, attributes, args)]
    end
  end
  index_builder = IndexBuilder.new(self, attributes)
  self.save_hooks << index_builder
  self.destroy_hooks << index_builder
end

#reject(name, &block) ⇒ Object

Will define a method called name that will include all existing instances of this class that when yielded to block does not return true. Will only return instances saved after this rejector is defined.



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/hyperactive/index.rb', line 182

def reject(name, &block) #:yields: instance
  key = collection_key(name)
  CAPTAIN[key] ||= Hyperactive::Hash::Head.get_instance
  self.class.class_eval do
    define_method(name) do
      CAPTAIN[key]
    end
  end
  self.save_hooks << MatchSaver.new(key, block, :reject)
  self.destroy_hooks << MatchSaver.new(key, block, :delete_unless_match)
end

#select(name, &block) ⇒ Object

Will define a method called name that will include all existing instances of this class that when yielded to the block returns true. Will only return instances saved after this selector is defined.



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/hyperactive/index.rb', line 164

def select(name, &block) #:yields: instance
  key = collection_key(name)
  CAPTAIN[key] ||= Hyperactive::Hash::Head.get_instance
  self.class.class_eval do
    define_method(name) do
      CAPTAIN[key]
    end
  end
  self.save_hooks << MatchSaver.new(key, block, :select)
  self.destroy_hooks << MatchSaver.new(key, block, :delete_if_match)
end