ghaki-account 2011.11.29.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. data/LICENSE +19 -0
  2. data/README +23 -0
  3. data/VERSION +1 -0
  4. data/lib/ghaki/account/base.rb +78 -0
  5. data/lib/ghaki/account/database.rb +66 -0
  6. data/lib/ghaki/account/db_connect.rb +45 -0
  7. data/lib/ghaki/account/db_driver.rb +22 -0
  8. data/lib/ghaki/account/db_name.rb +22 -0
  9. data/lib/ghaki/account/db_port.rb +22 -0
  10. data/lib/ghaki/account/domain_address.rb +39 -0
  11. data/lib/ghaki/account/email_address.rb +39 -0
  12. data/lib/ghaki/account/errors.rb +10 -0
  13. data/lib/ghaki/account/hostname.rb +95 -0
  14. data/lib/ghaki/account/password.rb +101 -0
  15. data/lib/ghaki/account/syn_opts.rb +42 -0
  16. data/lib/ghaki/account/userdomain.rb +88 -0
  17. data/lib/ghaki/account/username.rb +93 -0
  18. data/lib/ghaki/account/windos.rb +58 -0
  19. data/spec/ghaki/account/base_spec.rb +121 -0
  20. data/spec/ghaki/account/database_spec.rb +96 -0
  21. data/spec/ghaki/account/db_connect_spec.rb +79 -0
  22. data/spec/ghaki/account/db_driver_spec.rb +44 -0
  23. data/spec/ghaki/account/db_name_spec.rb +43 -0
  24. data/spec/ghaki/account/db_port_spec.rb +49 -0
  25. data/spec/ghaki/account/domain_address_spec.rb +30 -0
  26. data/spec/ghaki/account/email_address_spec.rb +30 -0
  27. data/spec/ghaki/account/hostname_spec.rb +110 -0
  28. data/spec/ghaki/account/password_spec.rb +155 -0
  29. data/spec/ghaki/account/syn_opts_helper.rb +90 -0
  30. data/spec/ghaki/account/syn_opts_spec.rb +36 -0
  31. data/spec/ghaki/account/userdomain_spec.rb +69 -0
  32. data/spec/ghaki/account/username_spec.rb +67 -0
  33. data/spec/ghaki/account/windos_spec.rb +107 -0
  34. data/spec/spec_helper.rb +6 -0
  35. metadata +139 -0
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Gerald Kalafut
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,23 @@
1
+ = Ghaki Account - Login account information.
2
+
3
+ Ghaki Account is a collection of user / host login information helper objects.
4
+
5
+ == Download
6
+
7
+ The latest version of Ghaki Account can be found at
8
+
9
+ * git@github.com:ghaki/ghaki-account.git
10
+
11
+ == Installation
12
+
13
+ The preferred method of installing Ghaki Account is through its GEM file.
14
+
15
+ % [sudo] gem install ghaki-account-1.0.0.gem
16
+
17
+ == License
18
+
19
+ Ghaki Account is released under the MIT license.
20
+
21
+ == Support
22
+
23
+ Contact mailto:gerald@kalafut.org
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2011.11.29.1
@@ -0,0 +1,78 @@
1
+ ############################################################################
2
+ require 'ghaki/account/hostname'
3
+ require 'ghaki/account/username'
4
+ require 'ghaki/account/password'
5
+ require 'ghaki/account/email_address'
6
+
7
+ module Ghaki #:nodoc:
8
+ module Account #:nodoc:
9
+
10
+ class Base
11
+ include Hostname
12
+ include Username
13
+ include Password
14
+ include EMailAddress
15
+
16
+ ######################################################################
17
+ # CLASS METHODS
18
+ ######################################################################
19
+
20
+ def self.from_opts opts
21
+ opts[:account] || Base.new(opts)
22
+ end
23
+
24
+ def self.purge_opts opts
25
+ Hostname.purge_opts opts
26
+ Username.purge_opts opts
27
+ Password.purge_opts opts
28
+ EMailAddress.purge_opts opts
29
+ opts.delete(:account)
30
+ opts
31
+ end
32
+
33
+ ######################################################################
34
+ # OBJECT METHODS
35
+ ######################################################################
36
+
37
+ def initialize opts={}
38
+ _from_opts opts
39
+ end
40
+
41
+ def expand_opts opts={}
42
+ opts.delete(:account)
43
+ opts[:hostname] = @hostname unless @hostname.nil?
44
+ opts[:username] = @username unless @username.nil?
45
+ opts[:password] = self.passwords unless passwords.empty?
46
+ opts[:email_address] = @email_address unless @email_address.nil?
47
+ opts
48
+ end
49
+
50
+ def collapse_opts opts={}
51
+ self.class.purge_opts opts
52
+ opts[:account] = self
53
+ opts
54
+ end
55
+
56
+ def to_s
57
+ (@username || '*') + '@' + (@hostname || '*')
58
+ end
59
+
60
+ def ask_all opts={}
61
+ ask_hostname opts[:hostname_opts] || {}
62
+ ask_username opts[:username_opts] || {}
63
+ ask_password opts[:password_opts] || {}
64
+ end
65
+
66
+ ######################################################################
67
+ protected
68
+ ######################################################################
69
+
70
+ def _from_opts opts
71
+ opt_hostname opts
72
+ opt_username opts
73
+ opt_password opts
74
+ opt_email_address opts
75
+ end
76
+
77
+ end
78
+ end end
@@ -0,0 +1,66 @@
1
+ ############################################################################
2
+ require 'ghaki/account/base'
3
+ require 'ghaki/account/db_name'
4
+ require 'ghaki/account/db_port'
5
+ require 'ghaki/account/db_driver'
6
+ require 'ghaki/account/db_connect'
7
+
8
+ ############################################################################
9
+ module Ghaki module Account
10
+ class Database < Base
11
+ include DB_Name
12
+ include DB_Port
13
+ include DB_Driver
14
+ include DB_Connect
15
+
16
+ ######################################################################
17
+ # CLASS METHODS
18
+ ######################################################################
19
+
20
+ #---------------------------------------------------------------------
21
+ def self.from_opts opts
22
+ opts[:account] || Database.new(opts)
23
+ end
24
+
25
+ #---------------------------------------------------------------------
26
+ def self.purge_opts opts ; super opts
27
+ DB_Name.purge_opts opts
28
+ DB_Port.purge_opts opts
29
+ DB_Driver.purge_opts opts
30
+ DB_Connect.purge_opts opts
31
+ opts
32
+ end
33
+
34
+ ######################################################################
35
+ # OBJECT METHODS
36
+ ######################################################################
37
+
38
+ #---------------------------------------------------------------------
39
+ def expand_opts opts={} ; super opts
40
+ opts[:db_name] = @db_name unless @db_name.nil?
41
+ opts[:db_port] = @db_port unless @db_port.nil?
42
+ opts[:db_driver] = @db_driver unless @db_driver.nil?
43
+ opts[:db_connect] = @db_connect unless @db_connect.nil?
44
+ opts
45
+ end
46
+
47
+ #---------------------------------------------------------------------
48
+ def to_s
49
+ @db_name + ':' + (@username || '*') + '@' + (@hostname || 'localhost')
50
+ end
51
+
52
+ ######################################################################
53
+ protected
54
+ ######################################################################
55
+
56
+ #---------------------------------------------------------------------
57
+ def _from_opts opts ; super opts
58
+ opt_db_name opts
59
+ opt_db_port opts
60
+ opt_db_driver opts
61
+ opt_db_connect opts
62
+ end
63
+
64
+ end # class
65
+ end end # package
66
+ ############################################################################
@@ -0,0 +1,45 @@
1
+ ############################################################################
2
+ require 'ghaki/account/syn_opts'
3
+
4
+ ############################################################################
5
+ module Ghaki module Account
6
+ module DB_Connect
7
+ extend SynOpts
8
+
9
+ #---------------------------------------------------------------------
10
+ attr_syn_opts :db_connect,
11
+ :db_connector, :database_connect, :database_connector
12
+ attr_writer :db_connect
13
+
14
+ #---------------------------------------------------------------------
15
+ def db_connect
16
+ @db_connect ||= self.make_db_connect
17
+ end
18
+
19
+ #---------------------------------------------------------------------
20
+ def valid_db_connect?
21
+ return false if @db_connect.nil?
22
+ return false if @db_connect.empty?
23
+ return true
24
+ end
25
+
26
+ ######################################################################
27
+ protected
28
+ ######################################################################
29
+
30
+ #---------------------------------------------------------------------
31
+ def make_db_connect
32
+ con = "dbi:#{@db_driver}:#{@db_name}"
33
+ if @hostname.nil?
34
+ con += ':localhost' unless @db_port.nil?
35
+ else
36
+ con += ':' + @hostname
37
+ end
38
+ con += ':' + @db_port.to_s unless @db_port.nil?
39
+ con
40
+ end
41
+
42
+ end # class
43
+ end # namespace
44
+ end # package
45
+ ############################################################################
@@ -0,0 +1,22 @@
1
+ ############################################################################
2
+ require 'ghaki/account/syn_opts'
3
+
4
+ ############################################################################
5
+ module Ghaki module Account
6
+ module DB_Driver
7
+ extend SynOpts
8
+
9
+ ######################################################################
10
+ attr_syn_opts :db_driver, :database_driver
11
+ attr_accessor :db_driver
12
+
13
+ #---------------------------------------------------------------------
14
+ def valid_db_driver?
15
+ return false if @db_driver.nil?
16
+ return false if @db_driver.empty?
17
+ return true
18
+ end
19
+
20
+ end # helper
21
+ end end # package
22
+ ############################################################################
@@ -0,0 +1,22 @@
1
+ ############################################################################
2
+ require 'ghaki/account/syn_opts'
3
+
4
+ ############################################################################
5
+ module Ghaki module Account
6
+ module DB_Name
7
+ extend SynOpts
8
+
9
+ ######################################################################
10
+ attr_syn_opts :db_name, :database_name
11
+ attr_accessor :db_name
12
+
13
+ #---------------------------------------------------------------------
14
+ def valid_db_name?
15
+ return false if @db_name.nil?
16
+ return false if @db_name.empty?
17
+ return true
18
+ end
19
+
20
+ end # helper
21
+ end end # package
22
+ ############################################################################
@@ -0,0 +1,22 @@
1
+ ############################################################################
2
+ require 'ghaki/account/syn_opts'
3
+
4
+ ############################################################################
5
+ module Ghaki module Account
6
+ module DB_Port
7
+ extend SynOpts
8
+
9
+ ######################################################################
10
+ attr_syn_opts :db_port, :database_port
11
+ attr_accessor :db_port
12
+
13
+ #---------------------------------------------------------------------
14
+ def valid_db_port?
15
+ return true if @db_port.nil?
16
+ return true if @db_port.to_s =~ %r{\A\d+\z}o
17
+ return false
18
+ end
19
+
20
+ end # helper
21
+ end end # package
22
+ ############################################################################
@@ -0,0 +1,39 @@
1
+ ############################################################################
2
+ require 'ghaki/account/syn_opts'
3
+
4
+ ############################################################################
5
+ module Ghaki
6
+ module Account
7
+ module DomainAddress
8
+
9
+ ######################################################################
10
+ extend SynOpts
11
+ attr_syn_opts :domain_address
12
+
13
+ ######################################################################
14
+ # OBJECT METHODS
15
+ ######################################################################
16
+ attr_writer :domain_address
17
+
18
+ #---------------------------------------------------------------------
19
+ def domain_address
20
+ @domain_address ||= self.make_domain_address
21
+ end
22
+
23
+ ######################################################################
24
+ protected
25
+ ######################################################################
26
+
27
+ #---------------------------------------------------------------------
28
+ def make_domain_address
29
+ if @userdomain.nil?
30
+ @username
31
+ else
32
+ "\\\\" + @userdomain + "\\" + @username
33
+ end
34
+ end
35
+
36
+ end # class
37
+ end # namespace
38
+ end # package
39
+ ############################################################################
@@ -0,0 +1,39 @@
1
+ ############################################################################
2
+ require 'ghaki/account/syn_opts'
3
+
4
+ ############################################################################
5
+ module Ghaki
6
+ module Account
7
+ module EMailAddress
8
+
9
+ ######################################################################
10
+ extend SynOpts
11
+ attr_syn_opts :email_address, :email
12
+
13
+ ######################################################################
14
+ # OBJECT METHODS
15
+ ######################################################################
16
+ attr_writer :email_address
17
+
18
+ #---------------------------------------------------------------------
19
+ def email_address
20
+ @email_address ||= self.make_email_address
21
+ end
22
+
23
+ ######################################################################
24
+ protected
25
+ ######################################################################
26
+
27
+ #---------------------------------------------------------------------
28
+ def make_email_address
29
+ if @hostname.nil?
30
+ @username
31
+ else
32
+ @username + '@' + @hostname
33
+ end
34
+ end
35
+
36
+ end # class
37
+ end # namespace
38
+ end # package
39
+ ############################################################################
@@ -0,0 +1,10 @@
1
+ module Ghaki
2
+ module Account
3
+
4
+ class InvalidHostnameError < RuntimeError; end
5
+ class InvalidPasswordError < RuntimeError; end
6
+ class InvalidUsernameError < RuntimeError; end
7
+ class InvalidUserDomainError < RuntimeError; end
8
+
9
+ end
10
+ end
@@ -0,0 +1,95 @@
1
+ ############################################################################
2
+ require 'highline'
3
+ require 'ghaki/account/errors'
4
+ require 'ghaki/account/syn_opts'
5
+
6
+ ############################################################################
7
+ module Ghaki
8
+ module Account
9
+ module Hostname
10
+
11
+ ######################################################################
12
+ # SYNONYM OPTIONS
13
+ ######################################################################
14
+ extend SynOpts
15
+ attr_syn_opts :hostname, :host_name, :host
16
+
17
+ ######################################################################
18
+ # CONSTANTS
19
+ ######################################################################
20
+ HOSTNAME_RETRY_MAX = 3
21
+
22
+ ######################################################################
23
+ # CLASS METHODS
24
+ ######################################################################
25
+
26
+ #---------------------------------------------------------------------
27
+ def Hostname.get_env
28
+ if ENV.has_key?('HOSTNAME')
29
+ ENV['HOSTNAME']
30
+ else
31
+ require 'socket'
32
+ Socket.gethostname
33
+ end
34
+ end
35
+
36
+ ######################################################################
37
+ # OBJECT METHODS
38
+ ######################################################################
39
+ attr_reader :hostname
40
+
41
+ #---------------------------------------------------------------------
42
+ def hostname= val
43
+ case val
44
+ when String
45
+ @hostname = val.strip
46
+ when :env
47
+ @hostname = Hostname.get_env
48
+ else
49
+ @hostname = val
50
+ end
51
+ end
52
+
53
+ #---------------------------------------------------------------------
54
+ def valid_hostname?
55
+ return false if @hostname.nil?
56
+ return false if @hostname.empty?
57
+ return false unless @hostname =~ %r{\A\S+\z}o
58
+ return true
59
+ end
60
+
61
+ #---------------------------------------------------------------------
62
+ def ask_hostname opts={}
63
+ o_prompt,r_prompt = _hostname_prompt(opts), nil
64
+ defval = opts[:default] || @hostname
65
+ defval = Hostname.get_env if defval == :env
66
+ (1 .. (opts[:retry_max] || HOSTNAME_RETRY_MAX) ).each do
67
+ self.hostname = HighLine.new.ask( r_prompt||o_prompt ) do |q|
68
+ q.echo = true
69
+ q.default = defval unless defval.nil?
70
+ end
71
+ return @hostname if valid_hostname?
72
+ r_prompt ||= '* TRY AGAIN * ' + o_prompt
73
+ end
74
+ raise InvalidHostnameError, "Invalid Hostname: #{@hostname}"
75
+ end
76
+
77
+ #=====================================================================
78
+ protected
79
+ #=====================================================================
80
+
81
+ #---------------------------------------------------------------------
82
+ def _hostname_prompt opts
83
+ prompt = 'Hostname: '
84
+ case opts[:prompt]
85
+ when nil, :default, :auto
86
+ else
87
+ prompt = opts[:prompt]
88
+ end
89
+ prompt
90
+ end
91
+
92
+ end # class
93
+ end # namespace
94
+ end # package
95
+ ############################################################################