From d5d4091bc23a83c47950af08d4d80c062c415a0b Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sat, 15 Oct 2016 17:40:03 +0200 Subject: [PATCH] remove id and name for an item --- lib/mpw/item.rb | 19 +++---------- lib/mpw/mpw.rb | 61 +++++++++++++++++------------------------- templates/add_form.erb | 5 ++-- 3 files changed, 30 insertions(+), 55 deletions(-) diff --git a/lib/mpw/item.rb b/lib/mpw/item.rb index ef1309e..626db72 100644 --- a/lib/mpw/item.rb +++ b/lib/mpw/item.rb @@ -21,8 +21,6 @@ require 'i18n' module MPW class Item - attr_accessor :id - attr_accessor :name attr_accessor :group attr_accessor :host attr_accessor :protocol @@ -38,15 +36,13 @@ class Item # @args: options -> a hash of parameter # raise an error if the hash hasn't the key name def initialize(options={}) - if not options.has_key?(:name) or options[:name].to_s.empty? + if not options.has_key?(:host) or options[:host].to_s.empty? raise I18n.t('error.update.name_empty') end if not options.has_key?(:id) or options[:id].to_s.empty? or not options.has_key?(:created) or options[:created].to_s.empty? - @id = generate_id @created = Time.now.to_i else - @id = options[:id] @created = options[:created] @last_edit = options[:last_edit] options[:no_update_last_edit] = true @@ -58,11 +54,10 @@ class Item # Update the item # @args: options -> a hash of parameter def update(options={}) - if options.has_key?(:name) and options[:name].to_s.empty? + if options.has_key?(:host) and options[:host].to_s.empty? raise I18n.t('error.update.name_empty') end - @name = options[:name] if options.has_key?(:name) @group = options[:group] if options.has_key?(:group) @host = options[:host] if options.has_key?(:host) @protocol = options[:protocol] if options.has_key?(:protocol) @@ -79,8 +74,6 @@ class Item # Delete all data def delete - @id = nil - @name = nil @group = nil @host = nil @protocol = nil @@ -93,17 +86,11 @@ class Item end def empty? - return @name.to_s.empty? + return @host.to_s.empty? end def nil? return false end - - # Generate an random id - private - def generate_id - return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(16).join - end end end diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index 5adeb0c..fe80f40 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -82,9 +82,7 @@ class MPW if not data.nil? and not data.empty? YAML.load(data).each_value do |d| - @data.push(Item.new(id: d['id'], - name: d['name'], - group: d['group'], + @data.push(Item.new(group: d['group'], host: d['host'], protocol: d['protocol'], user: d['user'], @@ -110,17 +108,15 @@ class MPW @data.each do |item| next if item.empty? - data.merge!(item.id => {'id' => item.id, - 'name' => item.name, - 'group' => item.group, - 'host' => item.host, - 'protocol' => item.protocol, - 'user' => item.user, - 'port' => item.port, - 'comment' => item.comment, - 'last_edit' => item.last_edit, - 'created' => item.created, - } + data.merge!("#{item.user}@#{item.host}" => {'group' => item.group, + 'host' => item.host, + 'protocol' => item.protocol, + 'user' => item.user, + 'port' => item.port, + 'comment' => item.comment, + 'last_edit' => item.last_edit, + 'created' => item.created, + } ) end @@ -251,11 +247,10 @@ class MPW next if item.empty? next if not group.empty? and not group.eql?(item.group.downcase) - name = item.name.to_s.downcase host = item.host.to_s.downcase comment = item.comment.to_s.downcase - if not name =~ /^.*#{search}.*$/ and not host =~ /^.*#{search}.*$/ and not comment =~ /^.*#{search}.*$/ + if not host =~ /^.*#{search}.*$/ and not comment =~ /^.*#{search}.*$/ next end @@ -281,19 +276,17 @@ class MPW def export(file) data = {} @data.each do |item| - data.merge!(item.id => {'id' => item.id, - 'name' => item.name, - 'group' => item.group, - 'host' => item.host, - 'protocol' => item.protocol, - 'user' => item.user, - 'password' => get_password(item.id), - 'port' => item.port, - 'comment' => item.comment, - 'otp_key' => get_otp_code(item.id), - 'last_edit' => item.last_edit, - 'created' => item.created, - } + data.merge!("#{item.login}@#{item.host}" => {'group' => item.group, + 'host' => item.host, + 'protocol' => item.protocol, + 'user' => item.user, + 'password' => get_password(item.id), + 'port' => item.port, + 'comment' => item.comment, + 'otp_key' => get_otp_code(item.id), + 'last_edit' => item.last_edit, + 'created' => item.created, + } ) end @@ -306,8 +299,7 @@ class MPW # @args: file -> path to file import def import(file) YAML::load_file(file).each_value do |row| - item = Item.new(name: row['name'], - group: row['group'], + item = Item.new(group: row['group'], host: row['host'], protocol: row['protocol'], user: row['user'], @@ -370,8 +362,7 @@ class MPW # Update item if item.last_edit < r.last_edit - item.update(name: r.name, - group: r.group, + item.update(group: r.group, host: r.host, protocol: r.protocol, user: r.user, @@ -398,9 +389,7 @@ class MPW remote.list.each do |r| next if r.last_edit <= get_last_sync - item = Item.new(id: r.id, - name: r.name, - group: r.group, + item = Item.new(group: r.group, host: r.host, protocol: r.protocol, user: r.user, diff --git a/templates/add_form.erb b/templates/add_form.erb index 53c0333..447788e 100644 --- a/templates/add_form.erb +++ b/templates/add_form.erb @@ -1,9 +1,8 @@ --- -name: # <%= I18n.t('form.add_item.name') %> -group: # <%= I18n.t('form.add_item.group') %> host: # <%= I18n.t('form.add_item.host') %> -protocol: # <%= I18n.t('form.add_item.protocol') %> user: # <%= I18n.t('form.add_item.login') %> +group: # <%= I18n.t('form.add_item.group') %> +protocol: # <%= I18n.t('form.add_item.protocol') %> password: # <%= I18n.t('form.add_item.password') %> port: # <%= I18n.t('form.add_item.port') %> comment: # <%= I18n.t('form.add_item.comment') %>