cop-detective 0.0.5 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b798995e62622a81120bf2663a5255ed2dc3a2cc
4
- data.tar.gz: 16b49827e5098b0387f814a104e1a4d96e306b67
3
+ metadata.gz: ca77cdd89eaac87cba49126671a8350a3c592949
4
+ data.tar.gz: ee1745a7548c93a6806eb167b02d765a323c907d
5
5
  SHA512:
6
- metadata.gz: e24389e4ab559f8c7054256744b73940799b2d22bc64c6f338ddfbaa33f182d8fd4363503739cce12b49a352a6c46b7e2ade19c3d667ddbf130e0b86a18929c4
7
- data.tar.gz: deb866aaa231b916f4e294be7dc1fa47d871c3fa207d652f9e73d1cb81d9514116c3a95869f36e687276b40c4ead25210f5e2ee3c19fc55b87dd106bf3fa5079
6
+ metadata.gz: 8fdbaa7ae0bd3025b590c5ec2c416fbdc1f4267c54de17a5f3ded0813f9e73a0d43c8ef4d8dda48905c01a009af55901e1bfa7a68e50afb7eb119d257542f83a
7
+ data.tar.gz: 0c7e59fe8e62e9c511c634e779bc5e5eb8922fabf5b06e7e59949df1692878da06af72938037bf92945263a5443b91097de4d2297a93707767c54552dcbfc471
@@ -0,0 +1,54 @@
1
+ require 'active_support'
2
+ require 'active_record'
3
+ require_relative 'cop_detective'
4
+ class CopDetectiveAssigner
5
+
6
+ @@keychain = []
7
+ @@params = Hash.new(nil)
8
+
9
+ class << self
10
+
11
+ def build_params(params)
12
+ @@keychain
13
+ params.each do |k, v|
14
+ if v.is_a?(Hash)
15
+ build_params(v)
16
+ end
17
+ @@params[k] = v if @@keychain.include?(k)
18
+ end
19
+ @@params
20
+ end
21
+
22
+ def assign(params, keys)
23
+ set_keychain(keys)
24
+ build_params(params)
25
+ translate_keys
26
+ configure
27
+ end
28
+
29
+ def set_keychain(keys)
30
+ @@keys = keys
31
+ keys.each do |k, v|
32
+ @@keychain << v
33
+ end
34
+ @@keychain
35
+ end
36
+
37
+ def translate_keys
38
+ @@internal_keys = @@keys
39
+ @@internal_keys.each do |k, v|
40
+ @@internal_keys[k] = @@params[v]
41
+ end
42
+ @@internal_keys
43
+ end
44
+
45
+ def configure
46
+ old_password = @@internal_keys[:old_password] || nil
47
+ password = @@internal_keys[:password]
48
+ confirmation = @@internal_keys[:confirmation]
49
+ CopDetective.configure({old_password: old_password,
50
+ password: password,
51
+ confirmation: confirmation})
52
+ end
53
+ end
54
+ end
@@ -3,18 +3,45 @@ require 'active_support'
3
3
  require 'active_record'
4
4
  require_relative 'errors'
5
5
  require_relative 'validator'
6
+ require_relative 'assigner'
6
7
 
7
8
  class CopDetective
8
9
  cattr_reader :messages
9
- class << self
10
10
  include ActiveModel::SecurePassword
11
11
  @@messages = CopDetectiveValidator.messages
12
12
 
13
+ class << self
14
+
13
15
  def configure(options)
14
- if CopDetectiveValidator.options_valid?(options)
15
- @@old_password = options[:old_password] ||= 'not set'
16
- @@password = options[:password]
17
- @@confirmation = options[:confirmation]
16
+ @@old_password = options[:old_password]
17
+ @@password = options[:password]
18
+ @@confirmation = options[:confirmation]
19
+ end
20
+
21
+ def set_keys(keys)
22
+ raise CopDetective::ErrorMessages.wrong_datatype if keys.class != Hash
23
+ inspect_keys(keys)
24
+ inspect_values(keys)
25
+ @@keys = keys
26
+ end
27
+
28
+ def investigate(user, params)
29
+ assign(params, @@keys)
30
+ return create_user(user) if @@old_password == nil
31
+ update_user(user)
32
+ end
33
+
34
+ private
35
+
36
+ def inspect_keys(keys)
37
+ keys.each do |k, v|
38
+ raise CopDetective::ErrorMessages.formatting if k != :confirmation && k != :password && k != :old_password
39
+ end
40
+ end
41
+
42
+ def inspect_values(keys)
43
+ keys.each do |k, v|
44
+ raise CopDetective::ErrorMessages.options_error(k) if v.class != Symbol
18
45
  end
19
46
  end
20
47
 
@@ -32,7 +59,9 @@ class CopDetective
32
59
  end
33
60
  end
34
61
 
35
- private
62
+ def assign(params, keys)
63
+ CopDetectiveAssigner.assign(params, keys)
64
+ end
36
65
 
37
66
  def validate_new_passwords(user)
38
67
  CopDetectiveValidator.validate_new_passwords(user, @@password, @@confirmation)
@@ -9,6 +9,27 @@ class CopDetective
9
9
  "You entered your original password incorrectly."
10
10
  end
11
11
 
12
+ def wrong_datatype
13
+ 'You must pass a hash to the set_keys method'
14
+ end
15
+
16
+ def formatting
17
+ <<-MESSAGE
18
+ The keys in the hash you pass to #set_keys must be as follows:
19
+ password:
20
+ confirmation:
21
+ old_password:
22
+
23
+ the values you pass should reflect keys in your params hash.
24
+ a correctly configured hash would look similar to this:
25
+ {
26
+ password: :new_password,
27
+ confirmation: :password_again,
28
+ old_password: :original_password
29
+ }
30
+ MESSAGE
31
+ end
32
+
12
33
  def non_matching
13
34
  "Your new passwords don't match."
14
35
  end
@@ -17,8 +38,8 @@ class CopDetective
17
38
  flash[:error] = model.errors.full_messages
18
39
  end
19
40
 
20
- def options_error(key, error)
21
- raise "Option passed to #{key} #{error}"
41
+ def options_error(key)
42
+ "Option passed to #{key} must be a symbol."
22
43
  end
23
44
  end
24
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cop-detective
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Kamin
@@ -53,12 +53,13 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.1'
55
55
  description: A simple gem to use in your controller to remove some of the work of
56
- comparing passwords and confirmations.
56
+ comparing passwords and password confirmations.
57
57
  email: jordanakamin@gmail.com
58
58
  executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - lib/assigner.rb
62
63
  - lib/cop_detective.rb
63
64
  - lib/errors.rb
64
65
  - lib/validator.rb