Class: Vidibus::Pureftpd::User
- Inherits:
-
Object
- Object
- Vidibus::Pureftpd::User
show all
- Includes:
- ActiveModel::Dirty, ActiveModel::Validations
- Defined in:
- lib/vidibus/pureftpd/user.rb
Defined Under Namespace
Classes: DocumentNotFound, Error
Constant Summary
collapse
- ACCESSSORS =
[:login, :password, :directory]
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(values = {}) ⇒ User
Returns a new instance of User.
34
35
36
37
|
# File 'lib/vidibus/pureftpd/user.rb', line 34
def initialize(values = {})
self.attributes = values
@persisted = false
end
|
Class Method Details
.create(attributes) ⇒ Object
109
110
111
112
113
|
# File 'lib/vidibus/pureftpd/user.rb', line 109
def create(attributes)
new(attributes).tap do |user|
user.save
end
end
|
.find_by_login(login) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/vidibus/pureftpd/user.rb', line 85
def find_by_login(login)
cmd = "show #{login} -f %{password_file}" %
Vidibus::Pureftpd.settings
begin
data = Vidibus::Pureftpd.perform(cmd)
rescue Vidibus::Pureftpd::Error => e
if e.message.match('Unable to fetch info about user')
return nil
else
raise e
end
end
attributes = {}
data.scan(/(#{ACCESSSORS.join('|')})\s*:\s*(.+)/i).each do |key, value|
attributes[key.downcase] = value
end
User.new(attributes).tap do |user|
user.instance_variable_set('@persisted', true)
user.instance_variable_set('@changed_attributes', {})
end
end
|
Instance Method Details
#attributes ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/vidibus/pureftpd/user.rb', line 59
def attributes
hash = {}
[:login, :password, :directory].each do |a|
hash[a] = send(a)
end
hash
end
|
#attributes=(hash) ⇒ Object
67
68
69
70
71
|
# File 'lib/vidibus/pureftpd/user.rb', line 67
def attributes=(hash)
hash.each do |key, value|
send("#{key}=", value)
end
end
|
#destroy ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/vidibus/pureftpd/user.rb', line 47
def destroy
return false unless persisted? && login_was
cmd = "userdel #{login_was} -f %{password_file} -m" % settings
perform(cmd)
instance_variable_set('@persisted', false)
self
end
|
#persisted? ⇒ Boolean
55
56
57
|
# File 'lib/vidibus/pureftpd/user.rb', line 55
def persisted?
@persisted
end
|
#reload ⇒ Object
73
74
75
76
77
78
79
80
81
|
# File 'lib/vidibus/pureftpd/user.rb', line 73
def reload
if persisted? && login_was && (user = User.find_by_login(login_was))
self.attributes = user.attributes
@changed_attributes.clear
self
else
raise DocumentNotFound
end
end
|
#save ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/vidibus/pureftpd/user.rb', line 39
def save
return false unless valid? && changed?
persisted? ? update : create
@previously_changed = changes
@changed_attributes.clear
@persisted = true
end
|