Skip to content

Commit 666e60e

Browse files
committed
add argument validation to job::execute and job::repeat statements
Tried [contracts.ruby](https://github.com/egonSchiele/contracts.ruby) but does not allow mixing of default values and blocks [contracts.ruby/214](egonSchiele/contracts.ruby#214). Additionally, contracts.ruby gives developer friendly messages but does not give user friendly messages. Instead, rolled by own argument validation. Error messages include expected type, actual value, actual type, and source location. Addresses part of issue #5. Need validation on remaining DSL statements to close.
1 parent da37aa7 commit 666e60e

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/jobrnr/dsl/job_builder.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ def initialize(id:, predecessors:)
66
end
77

88
def execute(command = nil, &block)
9+
if command.nil? && block.nil?
10+
raise Jobrnr::ArgumentError, "'execute' expects a String or a block @ #{source}"
11+
elsif !command.nil? && !block.nil?
12+
raise Jobrnr::ArgumentError, "'execute' expects a String or block not both @ #{source}"
13+
elsif !command.nil? && !command.is_a?(String)
14+
raise Jobrnr::ArgumentError, "'execute' expects a String or a block but was given value of '#{command}' of type '#{command.class.name}' @ #{source}"
15+
end
16+
917
if command.nil?
1018
@obj.command = block
1119
else
@@ -14,9 +22,17 @@ def execute(command = nil, &block)
1422
end
1523

1624
def repeat(times)
25+
if !times.is_a?(Integer) || times < 0
26+
raise Jobrnr::TypeError, "'repeat' expects positive Integer value but was given value of '#{times}' of type '#{times.class.name}' @ #{source}"
27+
end
28+
1729
@obj.iterations = times
1830
end
1931

32+
def source
33+
caller[2].split(/:/)[0..1].join(':')
34+
end
35+
2036
def build
2137
@obj
2238
end

lib/jobrnr/error.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ module Jobrnr
44
class Error < StandardError; end
55
class OptionsError < Error; end
66
class ImportError < Error; end
7+
class ArgumentError < Error; end
78
end

0 commit comments

Comments
 (0)