FTB-Gamepedia / MediaWiki-Butt-Ruby

A Ruby library for the MediaWiki API
https://rubygems.org/gems/mediawiki-butt
MIT License
9 stars 2 forks source link

Methods with specified hash arguments have annoying behavior #60

Closed elifoster closed 7 years ago

elifoster commented 7 years ago

For example

def edit(title, text, opts = { bot: true } )
  params = {
    title: title,
    text: text
  }  
  params[:bot] = '1' if opts[:bot]
  params[:summary] ||= opts[:summary]
  params[:minor] = '1' if opts[:minor]
  params
end  
# => :edit
edit('title', 'text')
# => {:title=>"title", :text=>"text", :bot=>"1", :summary=>nil}
edit('title', 'text', summary: 'summary')
# => {:title=>"title", :text=>"text", :summary=>"summary"}
edit('title', 'text', summary: 'summary', minor: true)
# => {:title=>"title", :text=>"text", :summary=>"summary", :minor=>"1"}

It happens because the hash argument is overwritten by the provided hash ({ summary: 'summary', minor: true }). Defaults should be specified like

def edit(title, text, opts = { } )
  opts[:bot] = opts[:bot].nil? ? true : opts[:bot]
  params = {
    title: title,
    text: text
  }  
  params[:bot] = '1' if opts[:bot]
  params[:summary] ||= opts[:summary]
  params[:minor] = '1' if opts[:minor]
  params
end  

instead, or something along those lines (it depends on the type of value).