From 31aba3b13c4a9d87cbfad1fee8aa16abdec4c0d9 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Tue, 28 Mar 2017 22:22:09 +0200 Subject: [PATCH] replace hash method has_key? by key? --- bin/mpw-config | 2 +- lib/mpw/cli.rb | 8 ++++---- lib/mpw/config.rb | 4 ++-- lib/mpw/item.rb | 22 +++++++++++----------- lib/mpw/mpw.rb | 18 +++++++++--------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/bin/mpw-config b/bin/mpw-config index 14387d9..4740ed1 100644 --- a/bin/mpw-config +++ b/bin/mpw-config @@ -96,7 +96,7 @@ end.parse! config = MPW::Config.new(options[:config]) cli = MPW::Cli.new(config, nil) -if options.has_key?(:init) +if options.key?(:init) cli.setup(values) cli.load_config cli.get_wallet diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index ecd98e3..90f7665 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -399,8 +399,8 @@ class Cli options[:password] = MPW::password(@config.password) if password @mpw.add(item) - @mpw.set_password(item.id, options[:password]) if options.has_key?(:password) - @mpw.set_otp_key(item.id, options[:otp_key]) if options.has_key?(:otp_key) + @mpw.set_password(item.id, options[:password]) if options.key?(:password) + @mpw.set_otp_key(item.id, options[:otp_key]) if options.key?(:otp_key) @mpw.write_data puts "#{I18n.t('form.add_item.valid')}".green @@ -424,8 +424,8 @@ class Cli options[:password] = MPW::password(@config.password) if password item.update(options) - @mpw.set_password(item.id, options[:password]) if options.has_key?(:password) - @mpw.set_otp_key(item.id, options[:otp_key]) if options.has_key?(:otp_key) + @mpw.set_password(item.id, options[:password]) if options.key?(:password) + @mpw.set_otp_key(item.id, options[:otp_key]) if options.key?(:otp_key) @mpw.write_data puts "#{I18n.t('form.update_item.valid')}".green diff --git a/lib/mpw/config.rb b/lib/mpw/config.rb index b23842e..b7cf5e8 100644 --- a/lib/mpw/config.rb +++ b/lib/mpw/config.rb @@ -66,9 +66,9 @@ class Config } ['numeric', 'special', 'alpha', 'length'].each do |k| - if options.has_key?("pwd_#{k}".to_sym) + if options.key?("pwd_#{k}".to_sym) password[k.to_sym] = options["pwd_#{k}".to_sym] - elsif !@password.nil? && @password.has_key?(k.to_sym) + elsif !@password.nil? && @password.key?(k.to_sym) password[k.to_sym] = @password[k.to_sym] end end diff --git a/lib/mpw/item.rb b/lib/mpw/item.rb index bd1445b..6e99f6a 100644 --- a/lib/mpw/item.rb +++ b/lib/mpw/item.rb @@ -37,11 +37,11 @@ class Item # @args: options -> a hash of parameter # raise an error if the hash hasn't the key name def initialize(options={}) - if !options.has_key?(:host) || options[:host].to_s.empty? + if !options.key?(:host) || options[:host].to_s.empty? raise I18n.t('error.update.host_empty') end - if !options.has_key?(:id) || options[:id].to_s.empty? || !options.has_key?(:created) || options[:created].to_s.empty? + if !options.key?(:id) || options[:id].to_s.empty? || !options.key?(:created) || options[:created].to_s.empty? @id = generate_id @created = Time.now.to_i else @@ -57,18 +57,18 @@ class Item # Update the item # @args: options -> a hash of parameter def update(options={}) - if options.has_key?(:host) && options[:host].to_s.empty? + if options.key?(:host) && options[:host].to_s.empty? raise I18n.t('error.update.host_empty') end - @group = options[:group] if options.has_key?(:group) - @host = options[:host] if options.has_key?(:host) - @protocol = options[:protocol] if options.has_key?(:protocol) - @user = options[:user] if options.has_key?(:user) - @port = options[:port].to_i if options.has_key?(:port) && !options[:port].to_s.empty? - @otp = options[:otp] if options.has_key?(:otp) - @comment = options[:comment] if options.has_key?(:comment) - @last_edit = Time.now.to_i unless options.has_key?(:no_update_last_edit) + @group = options[:group] if options.key?(:group) + @host = options[:host] if options.key?(:host) + @protocol = options[:protocol] if options.key?(:protocol) + @user = options[:user] if options.key?(:user) + @port = options[:port].to_i if options.key?(:port) && !options[:port].to_s.empty? + @otp = options[:otp] if options.key?(:otp) + @comment = options[:comment] if options.key?(:comment) + @last_edit = Time.now.to_i unless options.key?(:no_update_last_edit) end # Delete all data diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index de99440..a957ef0 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -88,7 +88,7 @@ class MPW protocol: d['protocol'], user: d['user'], port: d['port'], - otp: @otp_keys.has_key?(d['id']), + otp: @otp_keys.key?(d['id']), comment: d['comment'], last_edit: d['last_edit'], created: d['created'], @@ -223,12 +223,12 @@ class MPW def set_config(options={}) @config = {} if @config.nil? - @config['protocol'] = options[:protocol] if options.has_key?(:protocol) - @config['host'] = options[:host] if options.has_key?(:host) - @config['port'] = options[:port] if options.has_key?(:port) - @config['user'] = options[:user] if options.has_key?(:user) - @config['password'] = options[:password] if options.has_key?(:password) - @config['path'] = options[:path] if options.has_key?(:path) + @config['protocol'] = options[:protocol] if options.key?(:protocol) + @config['host'] = options[:host] if options.key?(:host) + @config['port'] = options[:port] if options.key?(:port) + @config['user'] = options[:user] if options.key?(:user) + @config['password'] = options[:password] if options.key?(:password) + @config['path'] = options[:path] if options.key?(:path) end # Add a new item @@ -286,14 +286,14 @@ class MPW # args: id -> the item id # key -> the new key def get_otp_key(id) - @otp_keys.has_key?(id) ? decrypt(@otp_keys[id]) : nil + @otp_keys.key?(id) ? decrypt(@otp_keys[id]) : nil end # Get an otp code # @args: id -> the item id # @rtrn: an otp code def get_otp_code(id) - @otp_keys.has_key?(id) ? 0 : ROTP::TOTP.new(decrypt(@otp_keys[id])).now + @otp_keys.key?(id) ? 0 : ROTP::TOTP.new(decrypt(@otp_keys[id])).now end # Get remaining time before expire otp code