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.



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/hyperactive/index.rb', line 156

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.



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/hyperactive/index.rb', line 192

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.



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/hyperactive/index.rb', line 174

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