fail_fast 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. data/CHANGELOG.txt +5 -0
  2. data/README.markdown +109 -49
  3. data/VERSION +1 -1
  4. data/fail_fast.gemspec +36 -24
  5. data/lib/fail_fast/base/base.rb +44 -0
  6. data/lib/fail_fast/base/utils.rb +58 -0
  7. data/lib/fail_fast/base/z_only_for_tests.rb +10 -0
  8. data/lib/fail_fast/{misc.rb → extensions/base_commands.rb} +3 -3
  9. data/lib/fail_fast/{check_active_record_db.rb → extensions/check_active_record_db.rb} +17 -52
  10. data/lib/fail_fast/{check_email.rb → extensions/check_email.rb} +1 -1
  11. data/lib/fail_fast/{check_file_system.rb → extensions/check_file_system.rb} +4 -4
  12. data/lib/fail_fast/{check_mongo_db.rb → extensions/check_mongo_db.rb} +4 -4
  13. data/lib/fail_fast/{check_url.rb → extensions/check_url.rb} +2 -2
  14. data/lib/fail_fast/extensions/check_value.rb +40 -0
  15. data/lib/fail_fast/main.rb +16 -71
  16. data/lib/fail_fast/report.txt.erb +45 -6
  17. data/lib/fail_fast/support/error_db.rb +23 -0
  18. data/lib/fail_fast/support/error_details.rb +10 -0
  19. data/lib/fail_fast/support/z_only_for_tests.rb +6 -0
  20. data/lib/fail_fast.rb +6 -10
  21. data/show_all_errors.rb +12 -1
  22. data/spec/errors_storage_spec.rb +18 -0
  23. data/spec/file_is_empty_spec.rb +32 -4
  24. data/spec/file_is_missing_spec.rb +5 -3
  25. data/spec/multiple_blocks_support_spec.rb +37 -0
  26. data/spec/report_printing_spec.rb +29 -0
  27. data/spec/spec_helper.rb +44 -18
  28. metadata +37 -25
  29. data/lib/fail_fast/check_value.rb +0 -38
  30. /data/spec/{misc_spec.rb → base_commands_spec.rb} +0 -0
  31. /data/spec/{has_active_record_db_spec.rb → check_active_record_db_spec.rb} +0 -0
  32. /data/spec/{has_email_for_spec.rb → check_email_spec.rb} +0 -0
  33. /data/spec/{file_system_spec.rb → check_file_system_spec.rb} +0 -0
  34. /data/spec/{has_mongoDB_for_spec.rb → check_mongo_db_spec.rb} +0 -0
  35. /data/spec/{has_url_for_spec.rb → check_url_spec.rb} +0 -0
  36. /data/spec/{has_value_for_spec.rb → check_value_spec.rb} +0 -0
data/CHANGELOG.txt CHANGED
@@ -1,3 +1,8 @@
1
+ 0.2.1
2
+ - improve + colour the report template
3
+ 0.2.0
4
+ - added `FailFast(<afile>).check_now.but_fail_later` + FailFast.fail_now
5
+ - added `FailFast.failed?'
1
6
  0.1.2
2
7
  - added commands : only_if and skip_if
3
8
  0.1.1
data/README.markdown CHANGED
@@ -6,38 +6,85 @@
6
6
 
7
7
  ## How to use :
8
8
 
9
- Early in your project boot sequence insert code like
10
9
 
11
- require 'fail_fast'
12
- FailFast('database.yml').check do
13
- has_active_record_db_for Rails.env
14
- end
10
+ ### Case 1 : don't start the application if the DB cannot be reached.
15
11
 
16
- FailFast('database.mongo.yml').check do
17
- has_mongoDB_for Rails.env
18
- end
12
+ Early in your project boot sequence insert this code :
19
13
 
20
- FailFast('path_to/config.yml', prefix=Rails.env).check do
21
- has_values_for 'author/fname', 'author/lname'
22
- has_email_for 'newsletter/to_address'
14
+ require 'fail_fast'
15
+ FailFast("path/to/database.yml").check do
16
+ has_active_record_db_for 'production'
17
+ end
23
18
 
24
- only_if Rails.env.production? do
25
- has_url_for 'bug_tracker/url', :reachable => true
26
- end
27
19
 
28
- directory_exists_for '/tmp'
29
- file_exists_for 'public/nda.pdf'
20
+ If the DB connection fails,
21
+ * the application will exit immediately and
22
+ * you'll see this report :
23
+
24
+
25
+ +------------------------------------------------------------------------------------------
26
+ | FAIL_FAST error : precondition(s) not met in
27
+ | -----------------
28
+ | file : "path/to/database.yml"
29
+ | keys prefix : (none)
30
+ +------------------------------------------------------------------------------------------
31
+ | error key value
32
+ +------------------------------------------------------------------------------------------
33
+ | * active_record_db_connection_error production Unknown database 'a_db'
34
+ +------------------------------------------------------------------------------------------
35
+
36
+ Remark : `check` will call `exit(1)` at the end of the first block with an error.
37
+ If you want to collect and report all the errors before exiting, use `check_now.but_fail_later` (see case 2 below).
38
+
39
+ ### Case 2 : collect errors in multiple blocks.
40
+
41
+
42
+ require 'fail_fast'
43
+ FailFast('database.yml').check_now.but_fail_later do
44
+ has_active_record_db_for 'production'
45
+ end
46
+
47
+ FailFast('database.mongo.yml').check_now.but_fail_later do
48
+ has_mongoDB_for Rails.env
49
+ end
50
+
51
+ FailFast('path_to/config.yml', prefix=Rails.env).check_now.but_fail_later do
52
+ has_values_for 'author/fname', 'author/lname'
53
+ has_email_for 'newsletter/to_address'
54
+
55
+ only_if Rails.env.production? do
56
+ has_url_for 'bug_tracker/url', :reachable => true
57
+ end
58
+
59
+ directory_exists_for '/tmp'
60
+ file_exists_for 'public/nda.pdf'
61
+
62
+ skip_if Rails.env.development? do
63
+ fail "I don't work on Sunday" if 0 == Time.now.wday
64
+ end
65
+ end
66
+
67
+ FailFast.fail_now # exit it an error was detected in any of the 3 blocks above.
30
68
 
31
- skip_if Rails.env.development? do
32
- fail "I don't work on Sunday" if 0 == Time.now.wday
33
- end
34
- end
35
69
 
36
70
  If it fails, you'll get a report like this :
37
71
 
38
72
  +------------------------------------------------------------------------------------------
39
- | FAIL_FAST error : "./spec/fixtures/simple.yml"
40
- | key prefix = nil
73
+ | FAIL_FAST error : precondition(s) not met in
74
+ | -----------------
75
+ | file : "path/to/database.yml"
76
+ | keys prefix : (none)
77
+ +------------------------------------------------------------------------------------------
78
+ | error key value
79
+ +------------------------------------------------------------------------------------------
80
+ | * active_record_db_connection_error production Unknown database 'a_db'
81
+ +------------------------------------------------------------------------------------------
82
+
83
+ +------------------------------------------------------------------------------------------
84
+ | FAIL_FAST error : precondition(s) not met in
85
+ | -----------------
86
+ | file : "./spec/fixtures/simple.yml"
87
+ | keys prefix : none
41
88
  +------------------------------------------------------------------------------------------
42
89
  | error key value
43
90
  +------------------------------------------------------------------------------------------
@@ -62,6 +109,17 @@ If it fails, you'll get a report like this :
62
109
  | * fail a custom failure message
63
110
  +------------------------------------------------------------------------------------------
64
111
 
112
+ ### Case 3 : print an additional custom message if errors were detected
113
+
114
+ ... # code like in the cases above.
115
+
116
+ if FailFast.failed?
117
+ puts "Cannot start the application due to the problems mentioned above."
118
+ puts "You can skip those test with the SKIP_FAIL_FAST environment variable"
119
+ FailFast.fail_now unless 'true'==ENV['SKIP_FAIL_FAST']
120
+ end
121
+
122
+
65
123
 
66
124
  ## Info :
67
125
 
@@ -89,11 +147,12 @@ You can also add custom rules, not related to any config files :
89
147
 
90
148
  ### _free/direct_ commands (not linked to the yaml file contents) :
91
149
 
92
- directory_exists '/tmp'
93
- file_exists '/Users/me/.bash_profile'
94
- has_mongoDB 'localhost', 'db_app_1'
95
- has_active_record_db :host => 'dbserv', :adapter => 'mysql', :database => 'db'
96
- fail "I don't work on Sunday" if (0 == Time.now.wday)
150
+ fail "I don't work on Sunday" if (0 == Time.now.wday)
151
+
152
+ directory_exists '/tmp'
153
+ file_exists '/Users/me/.bash_profile'
154
+ has_mongoDB 'localhost', 'db_app_1'
155
+ has_active_record_db :host => 'dbserv', :adapter => 'mysql', :database => 'db'
97
156
 
98
157
  ### _keyed_ commands (linked to a value found in a yaml file) :
99
158
 
@@ -101,37 +160,38 @@ You can also add custom rules, not related to any config files :
101
160
 
102
161
  *presence :*
103
162
 
104
- has_value_for :application_name
105
- has_values_for 'author/fname', 'author/lname'
163
+ has_value_for :application_name
164
+ has_values_for 'author/fname', 'author/lname'
106
165
 
107
166
  *contents <-> a regexp or pattern :*
108
167
 
109
- has_value_for 'level', /(alpha|beta|production)/
110
- has_url_for 'bug_tracker/url'
111
- has_email_for 'newsletter/to_address'
168
+ has_value_for 'level', /(alpha|beta|production)/
169
+ has_url_for 'bug_tracker/url'
170
+ has_email_for 'newsletter/to_address'
112
171
 
113
172
  Test the file system :
114
173
 
115
- directory_exists_for 'assets-upload_dir'
116
- file_exists_for 'assets-nda_pdf_file'
174
+ directory_exists_for 'assets-upload_dir'
175
+ file_exists_for 'assets-nda_pdf_file'
117
176
 
118
177
  Test external services :
119
178
 
120
- # is a webserver up ?
121
- has_url_for 'bug_tracker/url', :reachable => true
122
- has_url_for 'bug_tracker/url', :reachable => true, :may_add_trailing_slash => true
123
-
124
-
125
- # can we connect to a mongoDB db/server :
126
- has_mongoDB_for 'test/mongoDB'
127
- has_mongoDB_for 'test/unknown_mongoDB_db', :check_database => false
128
-
129
- # can we connect to a SQL db :
130
- has_active_record_db_for 'production/db_connection'
131
-
179
+ # is a webserver up ?
180
+ has_url_for 'bug_tracker/url', :reachable => true
181
+ has_url_for 'bug_tracker/url', :reachable => true, :may_add_trailing_slash => true
182
+
183
+ # can we connect to a mongoDB db/server :
184
+ has_mongoDB_for 'test/mongoDB'
185
+ has_mongoDB_for 'test/unknown_mongoDB_db', :check_database => false
186
+
187
+ # can we connect to a SQL db :
188
+ has_active_record_db_for 'production/db_connection'
189
+
132
190
  Misc :
133
- fail 'message'
191
+
192
+ fail "I don't work on Sunday" if 0 == Time.now.wday
134
193
 
135
194
  Control commands :
136
- skip_if <condition> do .. end
137
- only_if <condition> do .. end
195
+
196
+ skip_if <condition> do .. end
197
+ only_if <condition> do .. end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.1
data/fail_fast.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{fail_fast}
8
- s.version = "0.1.2"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alain Ravet"]
12
- s.date = %q{2010-06-22}
12
+ s.date = %q{2010-07-05}
13
13
  s.description = %q{raises an error if the yaml contents of a config file does not pass a test script.}
14
14
  s.email = %q{alainravet@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -26,28 +26,37 @@ Gem::Specification.new do |s|
26
26
  "VERSION",
27
27
  "fail_fast.gemspec",
28
28
  "lib/fail_fast.rb",
29
- "lib/fail_fast/check_active_record_db.rb",
30
- "lib/fail_fast/check_email.rb",
31
- "lib/fail_fast/check_file_system.rb",
32
- "lib/fail_fast/check_mongo_db.rb",
33
- "lib/fail_fast/check_url.rb",
34
- "lib/fail_fast/check_value.rb",
29
+ "lib/fail_fast/base/base.rb",
30
+ "lib/fail_fast/base/utils.rb",
31
+ "lib/fail_fast/base/z_only_for_tests.rb",
32
+ "lib/fail_fast/extensions/base_commands.rb",
33
+ "lib/fail_fast/extensions/check_active_record_db.rb",
34
+ "lib/fail_fast/extensions/check_email.rb",
35
+ "lib/fail_fast/extensions/check_file_system.rb",
36
+ "lib/fail_fast/extensions/check_mongo_db.rb",
37
+ "lib/fail_fast/extensions/check_url.rb",
38
+ "lib/fail_fast/extensions/check_value.rb",
35
39
  "lib/fail_fast/main.rb",
36
- "lib/fail_fast/misc.rb",
37
40
  "lib/fail_fast/report.txt.erb",
41
+ "lib/fail_fast/support/error_db.rb",
42
+ "lib/fail_fast/support/error_details.rb",
43
+ "lib/fail_fast/support/z_only_for_tests.rb",
38
44
  "show_all_errors.rb",
45
+ "spec/base_commands_spec.rb",
46
+ "spec/check_active_record_db_spec.rb",
47
+ "spec/check_email_spec.rb",
48
+ "spec/check_file_system_spec.rb",
49
+ "spec/check_mongo_db_spec.rb",
50
+ "spec/check_url_spec.rb",
51
+ "spec/check_value_spec.rb",
52
+ "spec/errors_storage_spec.rb",
39
53
  "spec/file_is_empty_spec.rb",
40
54
  "spec/file_is_missing_spec.rb",
41
- "spec/file_system_spec.rb",
42
55
  "spec/fixtures/empty.yml",
43
56
  "spec/fixtures/simple.yml",
44
- "spec/has_active_record_db_spec.rb",
45
- "spec/has_email_for_spec.rb",
46
- "spec/has_mongoDB_for_spec.rb",
47
- "spec/has_url_for_spec.rb",
48
- "spec/has_value_for_spec.rb",
49
57
  "spec/how_to_use_spec.rb",
50
- "spec/misc_spec.rb",
58
+ "spec/multiple_blocks_support_spec.rb",
59
+ "spec/report_printing_spec.rb",
51
60
  "spec/spec.opts",
52
61
  "spec/spec_helper.rb"
53
62
  ]
@@ -57,16 +66,19 @@ Gem::Specification.new do |s|
57
66
  s.rubygems_version = %q{1.3.7}
58
67
  s.summary = %q{raises an error if the yaml contents of a config file does pass a test script.}
59
68
  s.test_files = [
60
- "spec/file_is_empty_spec.rb",
69
+ "spec/base_commands_spec.rb",
70
+ "spec/check_active_record_db_spec.rb",
71
+ "spec/check_email_spec.rb",
72
+ "spec/check_file_system_spec.rb",
73
+ "spec/check_mongo_db_spec.rb",
74
+ "spec/check_url_spec.rb",
75
+ "spec/check_value_spec.rb",
76
+ "spec/errors_storage_spec.rb",
77
+ "spec/file_is_empty_spec.rb",
61
78
  "spec/file_is_missing_spec.rb",
62
- "spec/file_system_spec.rb",
63
- "spec/has_active_record_db_spec.rb",
64
- "spec/has_email_for_spec.rb",
65
- "spec/has_mongoDB_for_spec.rb",
66
- "spec/has_url_for_spec.rb",
67
- "spec/has_value_for_spec.rb",
68
79
  "spec/how_to_use_spec.rb",
69
- "spec/misc_spec.rb",
80
+ "spec/multiple_blocks_support_spec.rb",
81
+ "spec/report_printing_spec.rb",
70
82
  "spec/spec_helper.rb"
71
83
  ]
72
84
 
@@ -0,0 +1,44 @@
1
+ class FailFast
2
+ module Base
3
+ ERB_TEMPLATE = File.dirname(__FILE__) + '/../report.txt.erb'
4
+
5
+ def check(&block)
6
+ fail_now_mode = block_given? # false in the case of *.check_now.but_fail_now do .. end
7
+
8
+ @config_file_not_found = !File.exist?(@config_file_path)
9
+ if @config_file_not_found
10
+ add_error ErrorDetails.new(nil, :config_file_not_found, @config_file_path)
11
+ else
12
+ check_all_rules(&block) if block_given?
13
+ end
14
+ unless errors.empty?
15
+ print_errors
16
+ exit(1) if fail_now_mode
17
+ end
18
+ self
19
+ end
20
+
21
+ alias check_now check
22
+
23
+ def but_fail_later(&block)
24
+ return if @config_file_not_found
25
+ check_all_rules(&block) if block_given?
26
+ unless errors.empty?
27
+ print_errors
28
+ end
29
+ end
30
+ private
31
+
32
+ def check_all_rules(&block)
33
+ @yaml_config_as_hash = YAML.load(ERB.new(File.read(@config_file_path)).result) || {}
34
+ self.instance_eval(&block)
35
+ end
36
+
37
+ def print_errors
38
+ @errors = errors
39
+ puts "\n\n\n" + ERB.new(File.read(ERB_TEMPLATE)).result(binding) + "\n\n"
40
+ end
41
+ end
42
+ end
43
+
44
+ FailFast.send :include, FailFast::Base
@@ -0,0 +1,58 @@
1
+ class FailFast
2
+
3
+ class Params < Struct.new(:key, :value, :regexp, :options) ; end
4
+
5
+ module Utils
6
+
7
+ def blank?(value)
8
+ value.nil? || (value.is_a?(String) && '' == value.strip)
9
+ end
10
+
11
+ # Usage
12
+ # value_for_deep_key('one/two/three')
13
+ # returns
14
+ # @yaml_config_as_hash['one']['two']['three']
15
+ #
16
+ def value_for_deep_key(key)
17
+ key.to_s.split('/').inject(@yaml_config_as_hash) { |h, k| h[k] } rescue nil
18
+ end
19
+
20
+
21
+ def key_value_regexp_options(key, params)
22
+ last = params.pop
23
+ if last.is_a?(Hash)
24
+ options = last
25
+ else
26
+ params << last
27
+ options = {}
28
+ end
29
+
30
+ last = params.pop
31
+ if last.is_a?(Regexp)
32
+ regexp = last
33
+ else
34
+ params << last
35
+ end
36
+
37
+ key = "#{@keys_prefix}/#{key}" if @keys_prefix
38
+ value = value_for_deep_key(key)
39
+
40
+ Params.new(key, value, regexp, options)
41
+ end
42
+
43
+ NO_COLOUR="\033[0m"
44
+ RED ="\033[31m"
45
+ LRED ="\033[1;31m"
46
+ BLUE ="\033[34m"
47
+ GREEN ="\033[32m"
48
+ YELLOW ="\033[1;33m"
49
+
50
+ def red( str) [RED, str, NO_COLOUR].join end
51
+ def lred( str) [LRED, str, NO_COLOUR].join end
52
+ def blue( str) [BLUE, str, NO_COLOUR].join end
53
+ def green( str) [GREEN, str, NO_COLOUR].join end
54
+ def yellow(str) [YELLOW, str, NO_COLOUR].join end
55
+ end
56
+ end
57
+
58
+ FailFast.send :include, FailFast::Utils
@@ -0,0 +1,10 @@
1
+ # ONLY USED BY TESTS !!
2
+ class FailFast
3
+ def self.global_errors
4
+ @@_errors_db.global_data
5
+ end
6
+
7
+ def self.reset_error_db!
8
+ @@_errors_db = ErrorDb.new
9
+ end
10
+ end
@@ -1,12 +1,12 @@
1
1
  class FailFast
2
- module Misc
2
+ module BaseCommands
3
3
 
4
4
  # Usage
5
5
  # if 0 == Time.now.wday
6
6
  # fail "I don't work on Sunday""
7
7
  # end
8
8
  def fail(message)
9
- FailFast.errors << ErrorDetails.new(nil, :fail, message)
9
+ add_error ErrorDetails.new(nil, :fail, message)
10
10
  end
11
11
 
12
12
  def only_if(condition, &block)
@@ -18,4 +18,4 @@ class FailFast
18
18
  end
19
19
  end
20
20
  end
21
- FailFast.send :include, FailFast::Misc
21
+ FailFast.send :include, FailFast::BaseCommands
@@ -1,19 +1,4 @@
1
1
  require 'active_record'
2
- # ActiveRecord::Base.establish_connection(
3
- # :adapter => "mysql",
4
- # :host => "localhost",
5
- # :username => "myuser",
6
- # :password => "mypass",
7
- # :database => "somedatabase"
8
- # )
9
- # ActiveRecord::Base.establish_connection(
10
- # :adapter => "sqlite",
11
- # :database => "path/to/dbfile"
12
- # )
13
- # ActiveRecord::Base.establish_connection(
14
- # :adapter => "sqlite3",
15
- # :dbfile => ":memory:"
16
- #)
17
2
 
18
3
  class FailFast
19
4
  module CheckActiveRecordDB
@@ -34,7 +19,7 @@ class FailFast
34
19
  @error_message = e.message
35
20
  end
36
21
  unless @success
37
- FailFast.errors << ErrorDetails.new(nil, :active_record_db_connection_error, @error_message)
22
+ add_error ErrorDetails.new(nil, :active_record_db_connection_error, @error_message)
38
23
  end
39
24
  end
40
25
 
@@ -61,7 +46,7 @@ class FailFast
61
46
  @error_message = e.message
62
47
  end
63
48
  unless @success
64
- FailFast.errors << ErrorDetails.new(key, :active_record_db_connection_error, @error_message)
49
+ add_error ErrorDetails.new(key, :active_record_db_connection_error, @error_message)
65
50
  end
66
51
  end
67
52
 
@@ -70,38 +55,18 @@ end
70
55
 
71
56
  FailFast.send :include, FailFast::CheckActiveRecordDB
72
57
 
73
- __END__
74
- # Ensure the mongoDB server can be reached, and the db could be opened :
75
- #
76
- # Usage :
77
- # has_mongoDB_for 'test/mongoDB'
78
- # has_mongoDB_for 'test/unknown_mongoDB_db', :check_database => false
79
- #
80
- def has_mongoDB_for(key, *params)
81
- return unless has_value_for key
82
- return unless has_value_for "#{key}/host"
83
- return unless has_value_for "#{key}/database"
84
-
85
- p = key_value_regexp_options(key, params)
86
- key, options = p.key, p.options
87
-
88
- value = value_for_deep_key(key)
89
- host, port, db = value['host'], value['port'], value['database']
90
-
91
- begin
92
- @conn = Mongo::Connection.new(host, port)
93
- rescue Mongo::ConnectionFailure
94
- FailFast.errors << ErrorDetails.new(key, :mongoDB_server_not_found, host)
95
- return
96
- end
97
-
98
- must_check_db = !(false == options[:check_database])
99
- if must_check_db && !@conn.database_names.include?(db)
100
- FailFast.errors << ErrorDetails.new(key, :mongoDB_db_not_found, db)
101
- end
102
- end
103
-
104
- end
105
- end
106
-
107
-
58
+ # ActiveRecord::Base.establish_connection(
59
+ # :adapter => "mysql",
60
+ # :host => "localhost",
61
+ # :username => "myuser",
62
+ # :password => "mypass",
63
+ # :database => "somedatabase"
64
+ # )
65
+ # ActiveRecord::Base.establish_connection(
66
+ # :adapter => "sqlite",
67
+ # :database => "path/to/dbfile"
68
+ # )
69
+ # ActiveRecord::Base.establish_connection(
70
+ # :adapter => "sqlite3",
71
+ # :dbfile => ":memory:"
72
+ #)
@@ -12,7 +12,7 @@ class FailFast
12
12
 
13
13
  value = value_for_deep_key(key)
14
14
  if EmailValidator.invalid_email_address?(value)
15
- FailFast.errors << ErrorDetails.new(key, :not_an_email, value)
15
+ add_error ErrorDetails.new(key, :not_an_email, value)
16
16
  end
17
17
  end
18
18
  end
@@ -8,7 +8,7 @@ class FailFast
8
8
  #
9
9
  def directory_exists(path, *params)
10
10
  unless File.exists?(path) && File.directory?(path)
11
- FailFast.errors << ErrorDetails.new(nil, :directory_not_found, path)
11
+ add_error ErrorDetails.new(nil, :directory_not_found, path)
12
12
  end
13
13
  end
14
14
 
@@ -19,7 +19,7 @@ class FailFast
19
19
  #
20
20
  def file_exists(path, *params)
21
21
  unless File.exists?(path) && File.file?(path)
22
- FailFast.errors << ErrorDetails.new(nil, :file_not_found, path)
22
+ add_error ErrorDetails.new(nil, :file_not_found, path)
23
23
  end
24
24
  end
25
25
 
@@ -37,7 +37,7 @@ class FailFast
37
37
  path = value_for_deep_key(key)
38
38
 
39
39
  unless File.exists?(path) && File.directory?(path)
40
- FailFast.errors << ErrorDetails.new(key, :directory_not_found, p.value)
40
+ add_error ErrorDetails.new(key, :directory_not_found, p.value)
41
41
  end
42
42
  end
43
43
 
@@ -55,7 +55,7 @@ class FailFast
55
55
  path = value_for_deep_key(key)
56
56
 
57
57
  unless File.exists?(path) && File.file?(path)
58
- FailFast.errors << ErrorDetails.new(key, :file_not_found, p.value)
58
+ add_error ErrorDetails.new(key, :file_not_found, p.value)
59
59
  end
60
60
  end
61
61
 
@@ -16,12 +16,12 @@ class FailFast
16
16
  port = options.delete(:port)
17
17
  @conn = Mongo::Connection.new(host, port, options)
18
18
  rescue Mongo::ConnectionFailure
19
- FailFast.errors << ErrorDetails.new(nil, :mongoDB_server_not_found, host)
19
+ add_error ErrorDetails.new(nil, :mongoDB_server_not_found, host)
20
20
  return
21
21
  end
22
22
 
23
23
  if db && !@conn.database_names.include?(db)
24
- FailFast.errors << ErrorDetails.new(nil, :mongoDB_db_not_found, db)
24
+ add_error ErrorDetails.new(nil, :mongoDB_db_not_found, db)
25
25
  end
26
26
  end
27
27
 
@@ -45,13 +45,13 @@ class FailFast
45
45
  begin
46
46
  @conn = Mongo::Connection.new(host, port)
47
47
  rescue Mongo::ConnectionFailure
48
- FailFast.errors << ErrorDetails.new(key, :mongoDB_server_not_found, host)
48
+ add_error ErrorDetails.new(key, :mongoDB_server_not_found, host)
49
49
  return
50
50
  end
51
51
 
52
52
  must_check_db = !(false == options[:check_database])
53
53
  if must_check_db && !@conn.database_names.include?(db)
54
- FailFast.errors << ErrorDetails.new(key, :mongoDB_db_not_found, db)
54
+ add_error ErrorDetails.new(key, :mongoDB_db_not_found, db)
55
55
  end
56
56
  end
57
57
 
@@ -18,11 +18,11 @@ class FailFast
18
18
 
19
19
  value = value_for_deep_key(key)
20
20
  if UrlValidator.invalid_url?(value)
21
- FailFast.errors << ErrorDetails.new(key, :not_a_url, value)
21
+ add_error ErrorDetails.new(key, :not_a_url, value)
22
22
  return
23
23
  end
24
24
  if true==options.delete(:reachable) && UrlValidator.unreachable_url?(value, options)
25
- FailFast.errors << ErrorDetails.new(key, :url_not_reachable, value)
25
+ add_error ErrorDetails.new(key, :url_not_reachable, value)
26
26
  end
27
27
  end
28
28
  end
@@ -0,0 +1,40 @@
1
+ class FailFast
2
+ module CheckValue
3
+
4
+ # Usage
5
+ # has_values_for :sym_key, 'str_key'
6
+ #
7
+ def has_values_for(*keys)
8
+ keys.each{|key| has_value_for(key)}
9
+ end
10
+
11
+
12
+ # Usage
13
+ # has_value_for 'str_key'
14
+ # has_value_for :sym_key, /localhost/
15
+ # returns
16
+ # true if succesful, false otherwise
17
+ def has_value_for(key, *params)
18
+ p = key_value_regexp_options(key, params)
19
+ key, options = p.key, p.options
20
+
21
+ nof_errors = errors.size
22
+ if blank?(p.value)
23
+ add_error ErrorDetails.new(key, :missing_value, nil)
24
+
25
+ elsif p.regexp
26
+ add_error ErrorDetails.new(key, :value_does_not_match, p.value) unless p.value =~ p.regexp
27
+
28
+ elsif options.is_a?(Hash) && options[:in].is_a?(Range)
29
+ add_error ErrorDetails.new(key, :value_not_in_range, p.value) unless options[:in].include?(p.value)
30
+
31
+ elsif options.is_a?(Hash) && options[:in].is_a?(Array)
32
+ add_error ErrorDetails.new(key, :value_not_in_array, p.value) unless options[:in].include?(p.value)
33
+ end
34
+ no_new_error = nof_errors == errors.size
35
+ end
36
+
37
+ end
38
+ end
39
+
40
+ FailFast.send :include, FailFast::CheckValue