Created
April 7, 2009 00:23
-
-
Save hosh/91025 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Monkey-patch for Ruby Datamapper 0.9.11 to add index support | |
# Load this after gems have been loaded | |
# Ho-Sheng Hsiao <twitter:hosheng> | |
# | |
# index :user_id | |
# index :user_id, :forum_id | |
# This monkeypatch does not have support for something like index :user_index, [ :user_id, :forum_id ] | |
module DataMapper | |
class PropertySet | |
# FIXME: Kludge | |
alias_method :indexes_without_outside_indexes, :indexes | |
def indexes | |
index_hash = indexes_without_outside_indexes | |
repository_name = repository.name | |
extra_indexes.each do |index, index_properties| | |
# Probably a faster way to do this | |
index_set = (index_hash[index.to_s] || []) | |
index_properties.each { |property| index_set << property.field(repository_name) } | |
index_hash[index.to_s] = index_set | |
end | |
index_hash | |
end | |
def extra_indexes | |
@indexes ||= [] | |
end | |
def add_index(*index_properties) | |
index_properties.flatten! | |
index_name = index_properties.map { |p| p.to_s }.join('_').to_sym | |
extra_indexes << [ index_name, index_properties.map { |p| property_for(p) }.compact ] | |
end | |
end | |
# I have no idea how to do this the proper way. I'd like to do this like the is_ plugins | |
module Model | |
# Add indexes outside of property declaration | |
# | |
# Example: | |
# index :user_id | |
# index :user_id, forum_id | |
# index :forum_id, :user_id | |
# | |
# This automatically generates a index name, but does not support naming your own index | |
def index(*index_properties) | |
properties(repository.name).add_index(*index_properties) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment