Skip to content

Commit f202f13

Browse files
bors[bot]bb
andauthored
Merge #118
118: Introduce idiomatic setters r=curquiza a=bb This introduces idiomatic setters. ### Summary of the new aliases Instead of writing e.g. ```ruby index.update_distinct_attribute 'my_attribute' ``` you can now also write ```ruby index.distinct_attribute = 'my_attribute' ``` ### (No) setter for documents I additionally considered `documents=` but decided against it because it's adding and replacing and not deleting all and then adding the new ones. ### Is setting to nil always resetting? I considered additionally allowing setting to `nil` for resetting because (at for least `distinct_attribute`) setting to nil raises an error and this might be an adequate usage: ```ruby index.distinct_attribute = nil MeiliSearch::ApiError: 400 Bad Request - Invalid JSON: EOF while parsing a value at line 1 column 0. See https://docs.meilisearch.com/errors#bad_request. from /Users/bb/.rvm/gems/ruby-2.6.6/gems/meilisearch-0.14.0/lib/meilisearch/http_request.rb:77:in `validate' ``` Instead of the error, reset_distinct_attribute could be invoked. What do you think. Should we implement it? Any settings where this might not be a good idea? ### General thoughts on the API style The Ruby API for index and client is full of aliases. I'm torn between having/keeping them (hey, I just added more 😉) and not liking them, especially the non-idiomatic `get_foo` ones because they fill up the code browser/introspection: ```ruby $ rails c Running via Spring preloader in process 4567 Loading development environment (Rails 6.0.3.4) > client = MeiliSearch::Client.new('http://127.0.0.1:7700', 'masterKey') => #<MeiliSearch::Client:0x00007fb66cc6ff28 @api_key="masterKey", @base_url="http://127.0.0.1:7700", @headers={"Content-Type"=>"application/json", "X-Meili-API-Key"=>"masterKey"}, @options={}> > index = client.index('my_index') => #<MeiliSearch::Index:0x00007fb66cd0d0e8 @api_key="masterKey", @base_url="http://127.0.0.1:7700", @headers={"Content-Type"=>"application/json", "X-Meili-API-Key"=>"masterKey"}, @options={}, @uid="posts"> > ls index ActiveSupport::ToJsonWithActiveSupportEncoder#methods: to_json ActiveSupport::Dependencies::ZeitwerkIntegration::RequireDependency#methods: require_dependency MeiliSearch::HTTPRequest#methods: http_delete http_get http_post http_put MeiliSearch::Index#methods: add_documents displayed_attributes get_one_document primary_key search update_displayed_attributes add_or_replace_documents distinct_attribute get_primary_key ranking_rules searchable_attributes update_distinct_attribute add_or_update_documents document get_ranking_rules replace_documents settings update_documents attributes_for_faceting documents get_searchable_attributes reset_attributes_for_faceting show update_index delete fields_distribution get_settings reset_displayed_attributes show_index update_ranking_rules delete_all_documents get_all_update_status get_stop_words reset_distinct_attribute stats update_searchable_attributes delete_document get_attributes_for_faceting get_synonyms reset_ranking_rules stop_words update_settings delete_documents get_displayed_attributes get_update_status reset_searchable_attributes synonyms update_stop_words delete_index get_distinct_attribute indexing? reset_settings uid update_synonyms delete_multiple_documents get_document last_update reset_stop_words update wait_for_pending_update delete_one_document get_documents number_of_documents reset_synonyms update_attributes_for_faceting instance variables: @api_key @base_url @headers @options @uid > ``` Note: The `ls` command comes from [pry](https://github.com/pry/pry), see [pry's documentation on code browsing](https://github.com/pry/pry#code-browsing). If you just want to get the idea without digging deeper into pry, it can be partially replicated using e.g. ```ruby > index.methods.sort - index.class.superclass.instance_methods => [:add_documents, :add_or_replace_documents, :add_or_update_documents, :attributes_for_faceting, :delete, :delete_all_documents, ... ``` Co-authored-by: Benjamin Bock <[email protected]>
2 parents 84e7095 + e05be72 commit f202f13

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

lib/meilisearch/index.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def settings
140140
def update_settings(settings)
141141
http_post "/indexes/#{@uid}/settings", settings
142142
end
143+
alias settings= update_settings
143144

144145
def reset_settings
145146
http_delete "/indexes/#{@uid}/settings"
@@ -155,6 +156,7 @@ def ranking_rules
155156
def update_ranking_rules(ranking_rules)
156157
http_post "/indexes/#{@uid}/settings/ranking-rules", ranking_rules
157158
end
159+
alias ranking_rules= update_ranking_rules
158160

159161
def reset_ranking_rules
160162
http_delete "/indexes/#{@uid}/settings/ranking-rules"
@@ -170,6 +172,7 @@ def synonyms
170172
def update_synonyms(synonyms)
171173
http_post "/indexes/#{@uid}/settings/synonyms", synonyms
172174
end
175+
alias synonyms= update_synonyms
173176

174177
def reset_synonyms
175178
http_delete "/indexes/#{@uid}/settings/synonyms"
@@ -186,6 +189,7 @@ def update_stop_words(stop_words)
186189
body = stop_words.is_a?(Array) ? stop_words : [stop_words]
187190
http_post "/indexes/#{@uid}/settings/stop-words", body
188191
end
192+
alias stop_words= update_stop_words
189193

190194
def reset_stop_words
191195
http_delete "/indexes/#{@uid}/settings/stop-words"
@@ -201,6 +205,7 @@ def distinct_attribute
201205
def update_distinct_attribute(distinct_attribute)
202206
http_post "/indexes/#{@uid}/settings/distinct-attribute", distinct_attribute
203207
end
208+
alias distinct_attribute= update_distinct_attribute
204209

205210
def reset_distinct_attribute
206211
http_delete "/indexes/#{@uid}/settings/distinct-attribute"
@@ -216,6 +221,7 @@ def searchable_attributes
216221
def update_searchable_attributes(searchable_attributes)
217222
http_post "/indexes/#{@uid}/settings/searchable-attributes", searchable_attributes
218223
end
224+
alias searchable_attributes= update_searchable_attributes
219225

220226
def reset_searchable_attributes
221227
http_delete "/indexes/#{@uid}/settings/searchable-attributes"
@@ -231,6 +237,7 @@ def displayed_attributes
231237
def update_displayed_attributes(displayed_attributes)
232238
http_post "/indexes/#{@uid}/settings/displayed-attributes", displayed_attributes
233239
end
240+
alias displayed_attributes= update_displayed_attributes
234241

235242
def reset_displayed_attributes
236243
http_delete "/indexes/#{@uid}/settings/displayed-attributes"
@@ -246,6 +253,7 @@ def attributes_for_faceting
246253
def update_attributes_for_faceting(attributes_for_faceting)
247254
http_post "/indexes/#{@uid}/settings/attributes-for-faceting", attributes_for_faceting
248255
end
256+
alias attributes_for_faceting= update_attributes_for_faceting
249257

250258
def reset_attributes_for_faceting
251259
http_delete "/indexes/#{@uid}/settings/attributes-for-faceting"

0 commit comments

Comments
 (0)