mpw 2.0.3 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/mpw/item.rb CHANGED
@@ -1,109 +1,119 @@
1
1
  #!/usr/bin/ruby
2
- # author: nishiki
3
- # mail: nishiki@yaegashi.fr
2
+ # MPW is a software to crypt and manage your passwords
3
+ # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
4
+ #
5
+ # This program is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU General Public License
7
+ # as published by the Free Software Foundation; either version 2
8
+ # of the License, or (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
4
18
 
5
- require 'rubygems'
6
19
  require 'i18n'
7
20
 
8
21
  module MPW
9
- class Item
22
+ class Item
10
23
 
11
- attr_accessor :error_msg
24
+ attr_accessor :error_msg
12
25
 
13
- attr_accessor :id
14
- attr_accessor :name
15
- attr_accessor :group
16
- attr_accessor :host
17
- attr_accessor :protocol
18
- attr_accessor :user
19
- attr_accessor :password
20
- attr_accessor :port
21
- attr_accessor :comment
22
- attr_accessor :last_edit
23
- attr_accessor :last_sync
24
- attr_accessor :created
26
+ attr_accessor :id
27
+ attr_accessor :name
28
+ attr_accessor :group
29
+ attr_accessor :host
30
+ attr_accessor :protocol
31
+ attr_accessor :user
32
+ attr_accessor :port
33
+ attr_accessor :comment
34
+ attr_accessor :last_edit
35
+ attr_accessor :last_sync
36
+ attr_accessor :created
25
37
 
26
- # Constructor
27
- # Create a new item
28
- # @args: options -> a hash of parameter
29
- # raise an error if the hash hasn't the key name
30
- def initialize(options={})
31
- if not options.has_key?(:name) or options[:name].to_s.empty?
32
- @error_msg = I18n.t('error.update.name_empty')
33
- raise @error_msg
34
- end
35
-
36
- if not options.has_key?(:id) or options[:id].to_s.empty? or not options.has_key?(:created) or options[:created].to_s.empty?
37
- @id = generate_id
38
- @created = Time.now.to_i
39
- else
40
- @id = options[:id]
41
- @created = options[:created]
42
- @last_edit = options[:last_edit]
43
- options[:no_update_last_edit] = true
44
- end
45
-
46
- update(options)
38
+ # Constructor
39
+ # Create a new item
40
+ # @args: options -> a hash of parameter
41
+ # raise an error if the hash hasn't the key name
42
+ def initialize(options={})
43
+ if not options.has_key?(:name) or options[:name].to_s.empty?
44
+ @error_msg = I18n.t('error.update.name_empty')
45
+ raise @error_msg
47
46
  end
48
47
 
49
- # Update the item
50
- # @args: options -> a hash of parameter
51
- # @rtrn: true if the item is update
52
- def update(options={})
53
- if options.has_key?(:name) and options[:name].to_s.empty?
54
- @error_msg = I18n.t('error.update.name_empty')
55
- return false
56
- end
48
+ if not options.has_key?(:id) or options[:id].to_s.empty? or not options.has_key?(:created) or options[:created].to_s.empty?
49
+ @id = generate_id
50
+ @created = Time.now.to_i
51
+ else
52
+ @id = options[:id]
53
+ @created = options[:created]
54
+ @last_edit = options[:last_edit]
55
+ options[:no_update_last_edit] = true
56
+ end
57
57
 
58
- @name = options[:name] if options.has_key?(:name)
59
- @group = options[:group] if options.has_key?(:group)
60
- @host = options[:host] if options.has_key?(:host)
61
- @protocol = options[:protocol] if options.has_key?(:protocol)
62
- @user = options[:user] if options.has_key?(:user)
63
- @password = options[:password] if options.has_key?(:password)
64
- @port = options[:port].to_i if options.has_key?(:port) and not options[:port].to_s.empty?
65
- @comment = options[:comment] if options.has_key?(:comment)
66
- @last_edit = Time.now.to_i if not options.has_key?(:no_update_last_edit)
58
+ update(options)
59
+ end
67
60
 
68
- return true
61
+ # Update the item
62
+ # @args: options -> a hash of parameter
63
+ # @rtrn: true if the item is update
64
+ def update(options={})
65
+ if options.has_key?(:name) and options[:name].to_s.empty?
66
+ @error_msg = I18n.t('error.update.name_empty')
67
+ return false
69
68
  end
70
69
 
71
- # Update last_sync
72
- def set_last_sync
73
- @last_sync = Time.now.to_i
74
- end
70
+ @name = options[:name] if options.has_key?(:name)
71
+ @group = options[:group] if options.has_key?(:group)
72
+ @host = options[:host] if options.has_key?(:host)
73
+ @protocol = options[:protocol] if options.has_key?(:protocol)
74
+ @user = options[:user] if options.has_key?(:user)
75
+ @port = options[:port].to_i if options.has_key?(:port) and not options[:port].to_s.empty?
76
+ @comment = options[:comment] if options.has_key?(:comment)
77
+ @last_edit = Time.now.to_i if not options.has_key?(:no_update_last_edit)
75
78
 
76
- # Delete all data
77
- # @rtrn: true
78
- def delete
79
- @id = nil
80
- @name = nil
81
- @group = nil
82
- @host = nil
83
- @protocol = nil
84
- @user = nil
85
- @password = nil
86
- @port = nil
87
- @comment = nil
88
- @created = nil
89
- @last_edit = nil
90
- @last_sync = nil
79
+ return true
80
+ end
91
81
 
92
- return true
93
- end
82
+ # Update last_sync
83
+ def set_last_sync
84
+ @last_sync = Time.now.to_i
85
+ end
94
86
 
95
- def empty?
96
- return @name.to_s.empty?
97
- end
87
+ # Delete all data
88
+ # @rtrn: true
89
+ def delete
90
+ @id = nil
91
+ @name = nil
92
+ @group = nil
93
+ @host = nil
94
+ @protocol = nil
95
+ @user = nil
96
+ @port = nil
97
+ @comment = nil
98
+ @created = nil
99
+ @last_edit = nil
100
+ @last_sync = nil
98
101
 
99
- def nil?
100
- return false
101
- end
102
+ return true
103
+ end
102
104
 
103
- # Generate an random id
104
- private
105
- def generate_id
106
- return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(16).join
107
- end
105
+ def empty?
106
+ return @name.to_s.empty?
107
+ end
108
+
109
+ def nil?
110
+ return false
111
+ end
112
+
113
+ # Generate an random id
114
+ private
115
+ def generate_id
116
+ return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(16).join
108
117
  end
118
+ end
109
119
  end