1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-12-18 07:23:30 +00:00
mpw/lib/Item.rb

110 lines
2.6 KiB
Ruby
Raw Normal View History

2015-01-27 22:17:16 +00:00
#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
require 'rubygems'
require 'i18n'
module MPW
class Item
attr_accessor :error_msg
attr_accessor :id
attr_accessor :name
attr_accessor :group
attr_accessor :host
attr_accessor :protocol
attr_accessor :user
attr_accessor :password
attr_accessor :port
attr_accessor :comment
attr_accessor :last_edit
attr_accessor :last_sync
2015-01-27 22:17:16 +00:00
attr_accessor :created
2015-02-13 18:28:51 +00:00
# Constructor
# Create a new item
# @args: options -> a hash of parameter
# raise an error if the hash hasn't the key name
2015-01-27 22:17:16 +00:00
def initialize(options={})
2015-02-12 22:02:09 +00:00
if not options.has_key?(:name) or options[:name].to_s.empty?
@error_msg = I18n.t('error.update.name_empty')
2015-02-13 18:28:51 +00:00
raise @error_msg
2015-02-12 22:02:09 +00:00
end
2015-02-01 11:23:50 +00:00
if not options.has_key?(:id) or options[:id].to_s.empty? or not options.has_key?(:created) or options[:created].to_s.empty?
2015-01-27 22:17:16 +00:00
@id = generate_id
@created = Time.now.to_i
else
2015-03-22 21:43:18 +00:00
@id = options[:id]
@created = options[:created]
@last_edit = options[:last_edit]
options[:no_update_last_edit] = true
2015-01-27 22:17:16 +00:00
end
update(options)
end
2015-02-13 18:28:51 +00:00
# Update the item
# @args: options -> a hash of parameter
# @rtrn: true if the item is update
2015-01-27 22:17:16 +00:00
def update(options={})
2015-02-01 11:23:50 +00:00
if options.has_key?(:name) and options[:name].to_s.empty?
2015-01-27 22:17:16 +00:00
@error_msg = I18n.t('error.update.name_empty')
return false
end
2015-02-01 11:23:50 +00:00
@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)
@user = options[:user] if options.has_key?(:user)
@password = options[:password] if options.has_key?(:password)
@port = options[:port].to_i if options.has_key?(:port) and not options[:port].to_s.empty?
@comment = options[:comment] if options.has_key?(:comment)
2015-03-22 21:43:18 +00:00
@last_edit = Time.now.to_i if not options.has_key?(:no_update_last_edit)
2015-01-27 22:17:16 +00:00
return true
end
# Update last_sync
def set_last_sync
@last_sync = Time.now.to_i
end
2015-02-13 18:28:51 +00:00
# Delete all data
# @rtrn: true
2015-02-01 11:35:42 +00:00
def delete
@id = nil
@name = nil
@group = nil
@host = nil
@protocol = nil
@user = nil
@password = nil
@port = nil
2015-02-12 17:01:58 +00:00
@comment = nil
2015-02-01 11:35:42 +00:00
@created = nil
@last_edit = nil
@last_sync = nil
2015-02-12 17:01:58 +00:00
return true
2015-02-01 11:35:42 +00:00
end
2015-01-27 22:17:16 +00:00
def empty?
return @name.to_s.empty?
end
def nil?
return false
end
2015-02-13 18:28:51 +00:00
# Generate an random id
2015-01-27 22:17:16 +00:00
private
def generate_id
return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(16).join
end
end
end