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.



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/hyperactive/index.rb', line 160

def index_by(*attributes)
  klass = self
  self.class.class_eval do
    define_method("find_by_#{attributes.join("_and_")}") do |*args|
      Archipelago::Pirate::BLACKBEARD[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.



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/hyperactive/index.rb', line 196

def reject(name, &block) #:yields: instance
  key = collection_key(name)
  Archipelago::Pirate::BLACKBEARD[key] ||= Hyperactive::Hash::Head.get_instance
  self.class.class_eval do
    define_method(name) do
      Archipelago::Pirate::BLACKBEARD[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.



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/hyperactive/index.rb', line 178

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