Class: DragonsKeep::AccountController

Inherits:
Object
  • Object
show all
Defined in:
lib/dragons_keep/account_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database = nil, password = nil) ⇒ AccountController

Returns a new instance of AccountController.



42
43
44
45
46
47
48
49
50
# File 'lib/dragons_keep/account_controller.rb', line 42

def initialize(database=nil, password=nil)
  @connection = false
  if(!(database.nil?))
    self.database = File.expand_path database
  end
  if (!(password.nil?))
    self.encrypt_pass = password
  end
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



12
13
14
# File 'lib/dragons_keep/account_controller.rb', line 12

def database
  @database
end

#encrypt_passObject

Returns the value of attribute encrypt_pass.



11
12
13
# File 'lib/dragons_keep/account_controller.rb', line 11

def encrypt_pass
  @encrypt_pass
end

Instance Method Details

#decrypt!(account) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/dragons_keep/account_controller.rb', line 73

def decrypt!()
  begin
    .decrpyt_password self.encrypt_pass
  rescue OpenSSL::Cipher::CipherError
    raise PasswordException, "Password is invalid"
  end
end

#delete(account) ⇒ Object



81
82
83
# File 'lib/dragons_keep/account_controller.rb', line 81

def delete()
  .destroy
end

#establish_connectionObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dragons_keep/account_controller.rb', line 18

def establish_connection
  migrate = false
  if !(File.exist?(self.database))
    migrate = true
  end
 
  #DataMapper::Logger.new $stdout, :debug

  DataMapper.setup :default, "sqlite3://#{self.database}"
  if migrate
    DataMapper.auto_migrate!
  else
    validate_connection
  end
  @connection = true
end

#get(id) ⇒ Object



67
68
69
70
71
# File 'lib/dragons_keep/account_controller.rb', line 67

def get(id)
  if @connection
    return Account.get id
  end
end

#listObject



52
53
54
55
56
57
# File 'lib/dragons_keep/account_controller.rb', line 52

def list()
  if @connection
    return Account.all
  end
  
end

#save!(account) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/dragons_keep/account_controller.rb', line 58

def save!()
  if @connection
    if .unencrypted?
      .encrypt_password(self.encrypt_pass)
    end
    return .save()
  end      
end

#validate_connectionObject

Check the first record in the database and decrypt the password to see if the password is valid



35
36
37
38
39
40
# File 'lib/dragons_keep/account_controller.rb', line 35

def validate_connection
   = Account.first
  if ! .nil?
    self.decrypt!()
  end
end