Skip to content

Commit

Permalink
[MODEL] Added, that index settings can be loaded from any object that…
Browse files Browse the repository at this point in the history
… responds to `:read`

Related: #346
Related: #351
  • Loading branch information
David Padilla authored and karmi committed May 21, 2015
1 parent 5c7cd12 commit b969783
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
25 changes: 22 additions & 3 deletions elasticsearch-model/lib/elasticsearch/model/indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ def mapping(options={}, &block)
#
# # => {:index=>{:number_of_shards=>1}}
#
# You can specify a YAML file with settings
# You can read settings from any object that responds to :read
# as long as its return value can be parsed as either YAML or JSON.
#
# @example Define index settings from YAML file
#
Expand All @@ -163,14 +164,28 @@ def mapping(options={}, &block)
# # number_of_shards: 1
# #
#
# Article.settings "config/elasticsearch/articles.yml"
# Article.settings File.open("config/elasticsearch/articles.yml")
#
# Article.settings.to_hash
#
# # => { "index" => { "number_of_shards" => 1 } }
#
#
# @example Define index settings from JSON file
#
# # config/elasticsearch/articles.json:
# #
# # { "index": { "number_of_shards": 1 } }
# #
#
# Article.settings File.open("config/elasticsearch/articles.json")
#
# Article.settings.to_hash
#
# # => { "index" => { "number_of_shards" => 1 } }
#
def settings(settings={}, &block)
settings = YAML.load_file(settings) if settings.is_a?(String)
settings = YAML.load(settings.read) if settings.respond_to?(:read)
@settings ||= Settings.new(settings)

@settings.settings.update(settings) unless settings.empty?
Expand All @@ -183,6 +198,10 @@ def settings(settings={}, &block)
end
end

def load_settings_from_io(settings)
YAML.load(settings.read)
end

# Creates an index with correct name, automatically passing
# `settings` and `mappings` defined in the model
#
Expand Down
1 change: 1 addition & 0 deletions elasticsearch-model/test/support/model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "baz": "qux" }
9 changes: 8 additions & 1 deletion elasticsearch-model/test/unit/indexing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ class NotFound < Exception; end
end

should "update and return the index settings from a yml file" do
DummyIndexingModel.settings "test/support/model.yml"
DummyIndexingModel.settings File.open("test/support/model.yml")
DummyIndexingModel.settings bar: 'bam'

assert_equal( {foo: 'boo', bar: 'bam', 'baz' => 'qux'}, DummyIndexingModel.settings.to_hash)
end

should "update and return the index settings from a json file" do
DummyIndexingModel.settings File.open("test/support/model.json")
DummyIndexingModel.settings bar: 'bam'

assert_equal( {foo: 'boo', bar: 'bam', 'baz' => 'qux'}, DummyIndexingModel.settings.to_hash)
Expand Down

0 comments on commit b969783

Please sign in to comment.