lib/elasticsearch/model/naming.rb in elasticsearch-model-0.1.5 vs lib/elasticsearch/model/naming.rb in elasticsearch-model-0.1.6
- old
+ new
@@ -13,18 +13,31 @@
#
# class Article
# index_name "articles-#{Rails.env}"
# end
#
+ # @example Set the index name for the `Article` model and re-evaluate it on each call
+ #
+ # class Article
+ # index_name { "articles-#{Time.now.year}" }
+ # end
+ #
# @example Directly set the index name for the `Article` model
#
# Article.index_name "articles-#{Rails.env}"
#
- # TODO: Dynamic names a la Tire -- `Article.index_name { "articles-#{Time.now.year}" }`
#
- def index_name name=nil
- @index_name = name || @index_name || self.model_name.collection.gsub(/\//, '-')
+ def index_name name=nil, &block
+ if name || block_given?
+ return (@index_name = name || block)
+ end
+
+ if @index_name.respond_to?(:call)
+ @index_name.call
+ else
+ @index_name || self.model_name.collection.gsub(/\//, '-')
+ end
end
# Set the index name
#
# @see index_name
@@ -65,11 +78,19 @@
# @example Set the index name for an instance of the `Article` model
#
# @article.index_name "articles-#{@article.user_id}"
# @article.__elasticsearch__.update_document
#
- def index_name name=nil
- @index_name = name || @index_name || self.class.index_name
+ def index_name name=nil, &block
+ if name || block_given?
+ return (@index_name = name || block)
+ end
+
+ if @index_name.respond_to?(:call)
+ @index_name.call
+ else
+ @index_name || self.class.index_name
+ end
end
# Set the index name
#
# @see index_name