toolchain 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +10 -0
  5. data/Gemfile.lock +34 -0
  6. data/LICENSE +22 -0
  7. data/README.md +254 -0
  8. data/Rakefile +1 -0
  9. data/lib/toolchain.rb +2 -0
  10. data/lib/toolchain/attributes.rb +154 -0
  11. data/lib/toolchain/attributes/configuration.rb +41 -0
  12. data/lib/toolchain/attributes/errors.rb +5 -0
  13. data/lib/toolchain/attributes/errors/invalid_hash_transformation.rb +4 -0
  14. data/lib/toolchain/attributes/errors/invalid_mass_assignment.rb +4 -0
  15. data/lib/toolchain/attributes/errors/type_mismatch.rb +4 -0
  16. data/lib/toolchain/attributes/ext/boolean.rb +4 -0
  17. data/lib/toolchain/attributes/helpers.rb +72 -0
  18. data/lib/toolchain/validations.rb +103 -0
  19. data/lib/toolchain/validations/delegation.rb +31 -0
  20. data/lib/toolchain/validations/delegator.rb +51 -0
  21. data/lib/toolchain/validations/helpers.rb +58 -0
  22. data/lib/toolchain/validations/validation_errors.rb +82 -0
  23. data/lib/toolchain/validations/validators.rb +12 -0
  24. data/lib/toolchain/validations/validators/acceptance.rb +25 -0
  25. data/lib/toolchain/validations/validators/base.rb +54 -0
  26. data/lib/toolchain/validations/validators/confirmation.rb +39 -0
  27. data/lib/toolchain/validations/validators/email.rb +26 -0
  28. data/lib/toolchain/validations/validators/exclusion.rb +29 -0
  29. data/lib/toolchain/validations/validators/format.rb +25 -0
  30. data/lib/toolchain/validations/validators/inclusion.rb +29 -0
  31. data/lib/toolchain/validations/validators/length.rb +60 -0
  32. data/lib/toolchain/validations/validators/presence.rb +25 -0
  33. data/lib/toolchain/version.rb +3 -0
  34. data/spec/spec_helper.rb +11 -0
  35. data/spec/toolchain/attributes/attribute_spec.rb +47 -0
  36. data/spec/toolchain/attributes/attributes_spec.rb +193 -0
  37. data/spec/toolchain/attributes/base_helper.rb +37 -0
  38. data/spec/toolchain/attributes/boolean_spec.rb +38 -0
  39. data/spec/toolchain/attributes/configuration_spec.rb +33 -0
  40. data/spec/toolchain/attributes/date_time_spec.rb +21 -0
  41. data/spec/toolchain/attributes/hash_spec.rb +61 -0
  42. data/spec/toolchain/attributes/include_attributes_spec.rb +19 -0
  43. data/spec/toolchain/attributes/initializer_spec.rb +32 -0
  44. data/spec/toolchain/validations/base_helper.rb +2 -0
  45. data/spec/toolchain/validations/custom_validations_spec.rb +26 -0
  46. data/spec/toolchain/validations/delegation_spec.rb +70 -0
  47. data/spec/toolchain/validations/include_validations_spec.rb +20 -0
  48. data/spec/toolchain/validations/inheritence_spec.rb +26 -0
  49. data/spec/toolchain/validations/multiple_validations_spec.rb +19 -0
  50. data/spec/toolchain/validations/nested_validations_spec.rb +68 -0
  51. data/spec/toolchain/validations/validation_errors_spec.rb +9 -0
  52. data/spec/toolchain/validations/validators/acceptance_spec.rb +48 -0
  53. data/spec/toolchain/validations/validators/confirmation_spec.rb +86 -0
  54. data/spec/toolchain/validations/validators/email_spec.rb +41 -0
  55. data/spec/toolchain/validations/validators/exclusion_spec.rb +53 -0
  56. data/spec/toolchain/validations/validators/format_spec.rb +44 -0
  57. data/spec/toolchain/validations/validators/inclusion_spec.rb +50 -0
  58. data/spec/toolchain/validations/validators/length_spec.rb +134 -0
  59. data/spec/toolchain/validations/validators/presence_spec.rb +34 -0
  60. data/toolchain.gemspec +21 -0
  61. metadata +161 -0
@@ -0,0 +1,9 @@
1
+ require_relative "base_helper"
2
+
3
+ describe Toolchain::Validations::ValidationErrors do
4
+
5
+ it "should return the errors hash" do
6
+ errors = Toolchain::Validations::ValidationErrors.new
7
+ errors.to_hash.should equal(errors.errors)
8
+ end
9
+ end
@@ -0,0 +1,48 @@
1
+ require_relative "../base_helper"
2
+
3
+ describe Toolchain::Validations::Validators::Acceptance do
4
+
5
+ class UserAcceptance
6
+ include Toolchain::Validations
7
+ attr_accessor :terms
8
+ validates :terms, acceptance: true
9
+ end
10
+
11
+ it "should be valid if 1" do
12
+ user = UserAcceptance.new
13
+ user.terms = 1
14
+ user.should be_valid
15
+ end
16
+
17
+ it "should be valid if '1'" do
18
+ user = UserAcceptance.new
19
+ user.terms = "1"
20
+ user.should be_valid
21
+ end
22
+
23
+ it "should be valid if true" do
24
+ user = UserAcceptance.new
25
+ user.terms = true
26
+ user.should be_valid
27
+ end
28
+
29
+ it "should be invalid" do
30
+ user = UserAcceptance.new
31
+ user.terms = nil
32
+ user.should_not be_valid
33
+ user.errors[:terms].should == ["must be accepted"]
34
+ end
35
+
36
+ class UserAcceptanceMessage
37
+ include Toolchain::Validations
38
+ attr_accessor :terms
39
+ validates :terms, acceptance: { message: "acceptance required" }
40
+ end
41
+
42
+ it "should be invalid" do
43
+ user = UserAcceptanceMessage.new
44
+ user.terms = nil
45
+ user.should_not be_valid
46
+ user.errors[:terms].should == ["acceptance required"]
47
+ end
48
+ end
@@ -0,0 +1,86 @@
1
+ require_relative "../base_helper"
2
+
3
+ describe Toolchain::Validations::Validators::Confirmation do
4
+
5
+ class UserConfirmation
6
+ include Toolchain::Validations
7
+ attr_accessor :password
8
+ attr_accessor :password_confirmation
9
+ validates :password, confirmation: true
10
+ end
11
+
12
+ it "should be valid" do
13
+ user = UserConfirmation.new
14
+ user.password = "hackery"
15
+ user.password_confirmation = "hackery"
16
+ user.should be_valid
17
+ end
18
+
19
+ it "should be invalid if nil" do
20
+ user = UserConfirmation.new
21
+ user.password = nil
22
+ user.password_confirmation = nil
23
+ user.should_not be_valid
24
+ user.errors[:password].should == ["doesn't match confirmation"]
25
+ end
26
+
27
+ it "should be invalid if it doesn't match" do
28
+ user = UserConfirmation.new
29
+ user.password = "hackery"
30
+ user.password_confirmation = "h4ckery"
31
+ user.should_not be_valid
32
+ user.errors[:password].should == ["doesn't match confirmation"]
33
+ end
34
+
35
+ class UserNestedConfirmation
36
+ include Toolchain::Validations
37
+ attr_accessor :config
38
+ validates :config, :password, confirmation: true
39
+ end
40
+
41
+ it "should be valid" do
42
+ user = UserNestedConfirmation.new
43
+ user.config = {
44
+ password: "hackery",
45
+ password_confirmation: "hackery"
46
+ }
47
+ user.should be_valid
48
+ end
49
+
50
+ it "should be invalid if nil" do
51
+ user = UserNestedConfirmation.new
52
+ user.config = {}
53
+ user.should_not be_valid
54
+ user.errors[:config][:password].should == ["doesn't match confirmation"]
55
+ end
56
+
57
+ it "should be invalid if nil" do
58
+ user = UserNestedConfirmation.new
59
+ user.config = nil
60
+ user.should_not be_valid
61
+ user.errors[:config][:password].should == ["doesn't match confirmation"]
62
+ end
63
+
64
+ it "should be invalid if it doesn't match" do
65
+ user = UserNestedConfirmation.new
66
+ user.config = {
67
+ password: "hackery",
68
+ password_confirmation: "h4ckery"
69
+ }
70
+ user.should_not be_valid
71
+ user.errors[:config][:password].should == ["doesn't match confirmation"]
72
+ end
73
+
74
+ class UserConfirmationMessage
75
+ include Toolchain::Validations
76
+ attr_accessor :password
77
+ attr_accessor :password_confirmation
78
+ validates :password, confirmation: { message: "isn't the same as confirmation" }
79
+ end
80
+
81
+ it "should be invalid" do
82
+ user = UserConfirmationMessage.new
83
+ user.should_not be_valid
84
+ user.errors[:password].should == ["isn't the same as confirmation"]
85
+ end
86
+ end
@@ -0,0 +1,41 @@
1
+ require_relative "../base_helper"
2
+
3
+ describe Toolchain::Validations::Validators::Email do
4
+
5
+ class UserEmail
6
+ include Toolchain::Validations
7
+ attr_accessor :email
8
+ validates :email, email: true
9
+ end
10
+
11
+ it "should be valid" do
12
+ user = UserEmail.new
13
+ user.email = "michael@machinery.io"
14
+ user.should be_valid
15
+ end
16
+
17
+ it "is invalid if nil" do
18
+ user = UserEmail.new
19
+ user.should_not be_valid
20
+ user.errors[:email].should == ["is invalid"]
21
+ end
22
+
23
+ it "is invalid if invalid format" do
24
+ user = UserEmail.new
25
+ user.email = "michael.machinery.io"
26
+ user.should_not be_valid
27
+ user.errors[:email].should == ["is invalid"]
28
+ end
29
+
30
+ class UserEmailMessage
31
+ include Toolchain::Validations
32
+ attr_accessor :email
33
+ validates :email, email: { message: "incorrect format" }
34
+ end
35
+
36
+ it "should have a custom error message" do
37
+ user = UserEmailMessage.new
38
+ user.should_not be_valid
39
+ user.errors[:email].should == ["incorrect format"]
40
+ end
41
+ end
@@ -0,0 +1,53 @@
1
+ require_relative "../base_helper"
2
+
3
+ describe Toolchain::Validations::Validators::Exclusion do
4
+
5
+ class UserExclusion
6
+ include Toolchain::Validations
7
+ attr_accessor :color
8
+ validates :color, exclusion: { in: ["red", "green", "blue"] }
9
+ end
10
+
11
+ it "should be invalid if 'red'" do
12
+ user = UserExclusion.new
13
+ user.color = "red"
14
+ user.should_not be_valid
15
+ user.errors[:color].should == ["is invalid"]
16
+ end
17
+
18
+ it "should be invalid if 'green'" do
19
+ user = UserExclusion.new
20
+ user.color = "green"
21
+ user.should_not be_valid
22
+ user.errors[:color].should == ["is invalid"]
23
+ end
24
+
25
+ it "should be invalid if 'blue'" do
26
+ user = UserExclusion.new
27
+ user.color = "blue"
28
+ user.should_not be_valid
29
+ user.errors[:color].should == ["is invalid"]
30
+ end
31
+
32
+ it "should be valid if 'yellow'" do
33
+ user = UserExclusion.new
34
+ user.color = "yellow"
35
+ user.should be_valid
36
+ end
37
+
38
+ class UserExclusionMessage
39
+ include Toolchain::Validations
40
+ attr_accessor :color
41
+ validates :color, exclusion: {
42
+ in: ["red", "green", "blue"],
43
+ message: "don't pick red, green or blue"
44
+ }
45
+ end
46
+
47
+ it "should be invalid" do
48
+ user = UserExclusionMessage.new
49
+ user.color = "red"
50
+ user.should_not be_valid
51
+ user.errors[:color].should == ["don't pick red, green or blue"]
52
+ end
53
+ end
@@ -0,0 +1,44 @@
1
+ require_relative "../base_helper"
2
+
3
+ describe Toolchain::Validations::Validators::Format do
4
+
5
+ class UserFormat
6
+ include Toolchain::Validations
7
+ attr_accessor :cc
8
+ validates :cc, format: { with: /^\d{4}-\d{4}-\d{4}-\d{4}$/ }
9
+ end
10
+
11
+ it "should be valid" do
12
+ user = UserFormat.new
13
+ user.cc = "1234-5678-1234-5678"
14
+ user.should be_valid
15
+ end
16
+
17
+ it "is invalid if nil" do
18
+ user = UserFormat.new
19
+ user.should_not be_valid
20
+ user.errors[:cc].should == ["is invalid"]
21
+ end
22
+
23
+ it "is invalid if format doesn't match" do
24
+ user = UserFormat.new
25
+ user.cc = "12-34-56-78"
26
+ user.should_not be_valid
27
+ user.errors[:cc].should == ["is invalid"]
28
+ end
29
+
30
+ class UserFormatMessage
31
+ include Toolchain::Validations
32
+ attr_accessor :cc
33
+ validates :cc, format: {
34
+ with: /^\d{4}-\d{4}-\d{4}-\d{4}$/,
35
+ message: "not a cc"
36
+ }
37
+ end
38
+
39
+ it "should have a custom error message" do
40
+ user = UserFormatMessage.new
41
+ user.should_not be_valid
42
+ user.errors[:cc].should == ["not a cc"]
43
+ end
44
+ end
@@ -0,0 +1,50 @@
1
+ require_relative "../base_helper"
2
+
3
+ describe Toolchain::Validations::Validators::Inclusion do
4
+
5
+ class UserInclusion
6
+ include Toolchain::Validations
7
+ attr_accessor :color
8
+ validates :color, inclusion: ["red", "green", "blue"]
9
+ end
10
+
11
+ it "should be valid if 'red'" do
12
+ user = UserInclusion.new
13
+ user.color = "red"
14
+ user.should be_valid
15
+ end
16
+
17
+ it "should be valid if 'green'" do
18
+ user = UserInclusion.new
19
+ user.color = "green"
20
+ user.should be_valid
21
+ end
22
+
23
+ it "should be valid if 'blue'" do
24
+ user = UserInclusion.new
25
+ user.color = "blue"
26
+ user.should be_valid
27
+ end
28
+
29
+ it "should be invalid if 'yellow'" do
30
+ user = UserInclusion.new
31
+ user.color = "yellow"
32
+ user.should_not be_valid
33
+ user.errors[:color].should == ["is invalid"]
34
+ end
35
+
36
+ class UserInclusionMessage
37
+ include Toolchain::Validations
38
+ attr_accessor :color
39
+ validates :color, inclusion: {
40
+ in: ["red", "green", "blue"],
41
+ message: "only pick red, green or blue"
42
+ }
43
+ end
44
+
45
+ it "should be invalid" do
46
+ user = UserInclusionMessage.new
47
+ user.should_not be_valid
48
+ user.errors[:color].should == ["only pick red, green or blue"]
49
+ end
50
+ end
@@ -0,0 +1,134 @@
1
+ require_relative "../base_helper"
2
+
3
+ describe Toolchain::Validations::Validators::Length do
4
+
5
+ class UserLength
6
+ include Toolchain::Validations
7
+ attr_accessor :password
8
+ validates :password, length: { between: 5..100 }
9
+ end
10
+
11
+ it "should be valid" do
12
+ user = UserLength.new
13
+ user.password = "mypassword"
14
+ user.should be_valid
15
+ end
16
+
17
+ it "should be too short" do
18
+ user = UserLength.new
19
+ user.password = "pass"
20
+ user.should_not be_valid
21
+ user.errors[:password].should == ["is invalid"]
22
+ end
23
+
24
+ it "should be too long" do
25
+ user = UserLength.new
26
+ user.password = "mypassword" * 11
27
+ user.should_not be_valid
28
+ user.errors[:password].should == ["is invalid"]
29
+ end
30
+
31
+ class UserLengthGreaterThanAndLessThan
32
+ include Toolchain::Validations
33
+ attr_accessor :password
34
+ validates :password, length: {
35
+ greater_than: 5, less_than: 100
36
+ }
37
+ end
38
+
39
+ it "should be too short" do
40
+ user = UserLengthGreaterThanAndLessThan.new
41
+ user.password = "pass"
42
+ user.should_not be_valid
43
+ user.errors[:password].should == ["is invalid"]
44
+ end
45
+
46
+ it "should be too long" do
47
+ user = UserLengthGreaterThanAndLessThan.new
48
+ user.password = "mypassword" * 10
49
+ user.should_not be_valid
50
+ user.errors[:password].should == ["is invalid"]
51
+ end
52
+
53
+ class UserLengthGreaterThan
54
+ include Toolchain::Validations
55
+ attr_accessor :password
56
+ validates :password, length: { greater_than: 5 }
57
+ end
58
+
59
+ it "should be too short" do
60
+ user = UserLengthGreaterThan.new
61
+ user.password = "pass"
62
+ user.should_not be_valid
63
+ user.errors[:password].should == ["is invalid"]
64
+ end
65
+
66
+ class UserLengthLessThan
67
+ include Toolchain::Validations
68
+ attr_accessor :password
69
+ validates :password, length: { less_than: 100 }
70
+ end
71
+
72
+ it "should be too short" do
73
+ user = UserLengthLessThan.new
74
+ user.password = "mypassword" * 10
75
+ user.should_not be_valid
76
+ user.errors[:password].should == ["is invalid"]
77
+ end
78
+
79
+ class UserLengthOfDigit
80
+ include Toolchain::Validations
81
+ attr_accessor :number
82
+ validates :number, length: { between: 5..10 }
83
+ end
84
+
85
+ it "should be invalid" do
86
+ user = UserLengthOfDigit.new
87
+ user.number = 1
88
+ user.should_not be_valid
89
+ user.errors[:number].should == ["is invalid"]
90
+ end
91
+
92
+ it "should be valid" do
93
+ user = UserLengthOfDigit.new
94
+ user.number = 7
95
+ user.should be_valid
96
+ end
97
+
98
+ it "should be invalid" do
99
+ user = UserLengthOfDigit.new
100
+ user.number = "1"
101
+ user.should_not be_valid
102
+ user.errors[:number].should == ["is invalid"]
103
+ end
104
+
105
+ it "should be valid" do
106
+ user = UserLengthOfDigit.new
107
+ user.number = "7"
108
+ user.should be_valid
109
+ end
110
+
111
+ it "should be invalid" do
112
+ user = UserLengthOfDigit.new
113
+ user.number = "1.123"
114
+ user.should_not be_valid
115
+ user.errors[:number].should == ["is invalid"]
116
+ end
117
+
118
+ it "should be valid" do
119
+ user = UserLengthOfDigit.new
120
+ user.number = "7.512"
121
+ user.should be_valid
122
+ end
123
+
124
+ class UserLengthNoMatcher
125
+ include Toolchain::Validations
126
+ attr_accessor :number
127
+ validates :number, length: {}
128
+ end
129
+
130
+ it "should be invalid" do
131
+ user = UserLengthNoMatcher.new
132
+ user.should_not be_valid
133
+ end
134
+ end