Checked 1.2.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Checked.gemspec +1 -1
- data/lib/Checked/Arch.rb +63 -0
- data/lib/Checked/Ask/Arrays.rb +11 -12
- data/lib/Checked/Ask/Ask.rb +2 -13
- data/lib/Checked/Ask/Strings.rb +5 -21
- data/lib/Checked/Ask/Vars.rb +21 -13
- data/lib/Checked/Clean/Clean.rb +5 -33
- data/lib/Checked/Clean/Strings.rb +38 -36
- data/lib/Checked/DSL.rb +238 -0
- data/lib/Checked/Demand/Arrays.rb +37 -42
- data/lib/Checked/Demand/Bools.rb +14 -12
- data/lib/Checked/Demand/Demand.rb +4 -39
- data/lib/Checked/Demand/File_Paths.rb +22 -23
- data/lib/Checked/Demand/Hashs.rb +11 -16
- data/lib/Checked/Demand/Strings.rb +29 -34
- data/lib/Checked/Demand/Symbols.rb +11 -17
- data/lib/Checked/Demand/Vars.rb +68 -28
- data/lib/Checked/version.rb +4 -2
- data/lib/Checked.rb +49 -13
- data/spec/main.rb +7 -3
- data/spec/tests/Ask.rb +39 -48
- data/spec/tests/Clean.rb +14 -9
- data/spec/tests/DSL.rb +138 -19
- data/spec/tests/Demand.rb +64 -41
- metadata +13 -15
- data/lib/Checked/Base/Arch.rb +0 -33
- data/lib/Checked/Base/Base.rb +0 -108
- data/lib/Checked/Base/DSL.rb +0 -81
- data/lib/Checked/Base/DSL_Obj.rb +0 -35
@@ -1,78 +1,73 @@
|
|
1
|
-
|
1
|
+
class Checked
|
2
2
|
class Demand
|
3
|
-
class Arrays
|
3
|
+
class Arrays < Sinatra::Base
|
4
4
|
|
5
|
-
include
|
6
|
-
|
7
|
-
namespace '/array!'
|
5
|
+
include Checked::Arch
|
6
|
+
map '/array!'
|
8
7
|
|
9
|
-
|
8
|
+
get
|
10
9
|
def check!
|
11
|
-
|
10
|
+
array! return!
|
12
11
|
end
|
13
12
|
|
14
|
-
|
13
|
+
get
|
15
14
|
def no_nils!
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
demand \
|
16
|
+
return!.include?(nil), \
|
17
|
+
"...can't contain nils."
|
19
18
|
end
|
20
19
|
|
21
|
-
|
20
|
+
get
|
22
21
|
def no_empty_strings!
|
23
|
-
|
24
|
-
|
25
|
-
if s.respond_to?(:rewind)
|
26
|
-
s.rewind
|
27
|
-
end
|
22
|
+
return!.each { |memo,s|
|
28
23
|
|
29
24
|
final = if s.respond_to?(:readlines)
|
25
|
+
s.rewind
|
30
26
|
s.readlines
|
31
27
|
else
|
32
28
|
s
|
33
29
|
end
|
34
30
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
31
|
+
demand \
|
32
|
+
final.is_a?(::String), \
|
33
|
+
"...can't contain unknown class: #{final.inspect}"
|
34
|
+
|
35
|
+
demand \
|
36
|
+
final.is_a?(::String) && final.strip.empty?, \
|
37
|
+
"...can't contain empty strings."
|
38
|
+
|
42
39
|
}
|
40
|
+
return!
|
43
41
|
end
|
44
42
|
|
45
|
-
|
43
|
+
get
|
46
44
|
def symbols!
|
47
|
-
not_empty!
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
Checked::App.new.get!("/array!/not_empty!", 'name'=>target_name, 'value'=>return!, 'args'=>[])
|
46
|
+
demand \
|
47
|
+
return!.all? { |v| v.is_a?(Symbol) }, \
|
48
|
+
"...contains a non-symbol."
|
51
49
|
end
|
52
50
|
|
53
|
-
|
51
|
+
get
|
54
52
|
def include!
|
55
|
-
return
|
56
|
-
|
53
|
+
demand return!.include?(matcher), \
|
54
|
+
"...must contain: #{matcher.inspect}"
|
57
55
|
end
|
58
56
|
|
59
|
-
|
57
|
+
get
|
60
58
|
def exclude!
|
61
|
-
|
62
|
-
return true unless raise_e
|
63
|
-
fail!("...can't contain #{matcher.inspect}")
|
59
|
+
demand val.include?(matcher), "...can't contain #{matcher.inspect}"
|
64
60
|
end
|
65
61
|
|
66
|
-
|
62
|
+
get
|
67
63
|
def matches_only!
|
68
|
-
|
69
|
-
|
64
|
+
demand \
|
65
|
+
return!.reject { |val| val == matcher }.empty?, \
|
66
|
+
"...invalid elements: #{arr.inspect}"
|
70
67
|
end
|
71
68
|
|
72
|
-
private
|
73
|
-
|
74
69
|
|
75
70
|
end # === class Arrays
|
76
71
|
end # === class Demand
|
77
|
-
end # ===
|
72
|
+
end # === class Checked
|
78
73
|
|
data/lib/Checked/Demand/Bools.rb
CHANGED
@@ -1,28 +1,30 @@
|
|
1
|
-
|
1
|
+
class Checked
|
2
2
|
class Demand
|
3
|
-
class Bools
|
3
|
+
class Bools < Sinatra::Base
|
4
4
|
|
5
|
-
include
|
6
|
-
|
7
|
-
namespace '/bool!'
|
5
|
+
include Checked::Arch
|
6
|
+
map '/bool!'
|
8
7
|
|
9
|
-
|
8
|
+
get
|
10
9
|
def check!
|
11
|
-
|
10
|
+
is_bool = [TrueClass, FalseClass].include?(return!.class)
|
11
|
+
demand is_bool, "...must be either of TrueClass or FalseClass."
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
get
|
15
15
|
def true!
|
16
|
-
|
16
|
+
is_true = return!.class == TrueClass
|
17
|
+
demand is_true, "...must be true (TrueClass)."
|
17
18
|
end
|
18
19
|
|
19
|
-
|
20
|
+
get
|
20
21
|
def false!
|
21
|
-
|
22
|
+
is_false = return!.class == FalseClass
|
23
|
+
demand is_false, "...must be false (FalseClass)."
|
22
24
|
end
|
23
25
|
|
24
26
|
end # === class Bools
|
25
27
|
end # === class Demand
|
26
|
-
end # ===
|
28
|
+
end # === class Checked
|
27
29
|
|
28
30
|
|
@@ -1,47 +1,12 @@
|
|
1
1
|
|
2
|
-
|
3
|
-
module Checked
|
2
|
+
class Checked
|
4
3
|
class Demand
|
5
|
-
|
6
4
|
Failed = Class.new(RuntimeError)
|
7
|
-
|
8
|
-
module Base
|
9
|
-
|
10
|
-
include Checked::Base
|
11
|
-
|
12
|
-
def err_msg msg = "...is invalid."
|
13
|
-
message = if msg.strip[ %r!^\.\.\.! ]
|
14
|
-
msg.sub('...', '').strip
|
15
|
-
else
|
16
|
-
msg
|
17
|
-
end
|
18
|
-
|
19
|
-
t = if original_target != target
|
20
|
-
"#{original_target.inspect} (#{target})"
|
21
|
-
else
|
22
|
-
original_target.inspect
|
23
|
-
end
|
24
|
-
@err_msg || "#{target_name}, #{t}, #{message}"
|
25
|
-
end
|
26
|
-
|
27
|
-
def err_msg= msg
|
28
|
-
demand! msg, :string!, :not_empty!
|
29
|
-
@err_msg = msg
|
30
|
-
end
|
31
|
-
|
32
|
-
private # ==========================================
|
33
|
-
|
34
|
-
def fail! msg
|
35
|
-
raise Failed, err_msg(msg)
|
36
|
-
end
|
37
|
-
|
38
|
-
end # === module Base
|
39
|
-
|
5
|
+
|
40
6
|
def initialize *args
|
41
7
|
raise "Demand not allowed to be used."
|
42
8
|
end
|
43
|
-
|
44
|
-
|
45
|
-
end # === module Checked
|
9
|
+
end # === class Demand
|
10
|
+
end # === class Checked
|
46
11
|
|
47
12
|
|
@@ -1,48 +1,47 @@
|
|
1
|
-
|
1
|
+
class Checked
|
2
2
|
class Demand
|
3
|
-
class File_Paths
|
3
|
+
class File_Paths < Sinatra::Base
|
4
4
|
|
5
|
-
|
6
|
-
include
|
7
|
-
|
5
|
+
INVALID_CHARS = %r!([^a-zA-Z0-9\.\_\-\/~,]+)!
|
6
|
+
include Checked::Arch
|
7
|
+
map '/file_path!'
|
8
8
|
|
9
|
-
|
9
|
+
get
|
10
10
|
def check!
|
11
|
-
|
11
|
+
string! return!
|
12
|
+
|
13
|
+
return! return!.strip
|
14
|
+
not_empty! return!
|
12
15
|
|
13
|
-
strip_target
|
14
|
-
not_empty!
|
15
16
|
validate_format!
|
16
|
-
expand_target if
|
17
|
+
expand_target if File.exists?(File.expand_path return!)
|
18
|
+
|
19
|
+
return!
|
17
20
|
end
|
18
21
|
|
19
|
-
|
22
|
+
get
|
20
23
|
def not_dir!
|
21
|
-
|
22
|
-
fail! "...can't be an existing directory."
|
23
|
-
end
|
24
|
+
demand !File.directory?(return!), "...can't be an existing directory."
|
24
25
|
end
|
25
26
|
|
26
|
-
|
27
|
+
get
|
27
28
|
def not_file!
|
28
|
-
|
29
|
+
demand !File.file?(return!), "...can't be a file."
|
29
30
|
end
|
30
31
|
|
31
|
-
|
32
|
+
get
|
32
33
|
def dir!
|
33
|
-
|
34
|
+
demand File.directory?(return!), "...must be an existing directory."
|
34
35
|
end
|
35
36
|
|
36
37
|
private
|
37
38
|
|
38
39
|
def validate_format!
|
39
|
-
|
40
|
-
fail! "...has invalid characters: #{$1.inspect}"
|
41
|
-
end
|
40
|
+
demand !( return![INVALID_CHARS] ), "...has invalid characters: #{$1.inspect}"
|
42
41
|
end
|
43
42
|
|
44
43
|
def expand_target
|
45
|
-
|
44
|
+
return! File.expand_path(return!)
|
46
45
|
end
|
47
46
|
|
48
47
|
#
|
@@ -54,5 +53,5 @@ module Checked
|
|
54
53
|
|
55
54
|
end # === class File_Addresses
|
56
55
|
end # === class Demand
|
57
|
-
end # ===
|
56
|
+
end # === class Checked
|
58
57
|
|
data/lib/Checked/Demand/Hashs.rb
CHANGED
@@ -1,29 +1,24 @@
|
|
1
|
-
|
1
|
+
class Checked
|
2
2
|
class Demand
|
3
|
-
class Hashs
|
3
|
+
class Hashs < Sinatra::Base
|
4
4
|
|
5
|
-
|
6
|
-
include
|
7
|
-
|
5
|
+
|
6
|
+
include Checked::Arch
|
7
|
+
map '/hash!'
|
8
8
|
|
9
|
-
|
9
|
+
get
|
10
10
|
def check!
|
11
|
-
|
11
|
+
demand hash?(return!), "...is not a Hash."
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
get
|
15
15
|
def symbol_keys!
|
16
|
-
|
17
|
-
|
18
|
-
if keys.all? { |k| k.is_a?(Symbol) }
|
19
|
-
# do nothing
|
20
|
-
else
|
21
|
-
fail! '...must have all symbol keys.'
|
22
|
-
end
|
16
|
+
all_syms = return!.keys.all? { |k| k.is_a?(Symbol) }
|
17
|
+
demand all_syms, '...must have all symbol keys.'
|
23
18
|
end
|
24
19
|
|
25
20
|
end # === class Hashs
|
26
21
|
end # === class Demand
|
27
|
-
end # ===
|
22
|
+
end # === class Checked
|
28
23
|
|
29
24
|
|
@@ -1,77 +1,72 @@
|
|
1
1
|
|
2
|
-
|
2
|
+
class Checked
|
3
3
|
class Demand
|
4
|
-
class Strings
|
4
|
+
class Strings < Sinatra::Base
|
5
5
|
|
6
|
-
include
|
7
|
-
|
8
|
-
namespace '/string!'
|
6
|
+
include Checked::Arch
|
7
|
+
map '/string!'
|
9
8
|
|
10
|
-
|
9
|
+
get
|
11
10
|
def check!
|
12
|
-
case
|
11
|
+
case return!
|
13
12
|
when String
|
13
|
+
|
14
|
+
return!
|
14
15
|
when StringIO
|
15
16
|
target.rewind
|
16
|
-
|
17
|
+
return! target.readlines
|
17
18
|
target.rewind
|
19
|
+
|
20
|
+
return!
|
18
21
|
else
|
19
|
-
|
22
|
+
demand false, "...must be a String or StringIO."
|
20
23
|
end
|
21
24
|
end
|
22
25
|
|
23
|
-
|
26
|
+
get
|
24
27
|
def include!
|
25
|
-
|
26
|
-
return true if included
|
27
|
-
fail!("...must contain: #{matcher.inspect}")
|
28
|
+
demand return![matcher], "...must contain: #{matcher.inspect}"
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
+
get
|
31
32
|
def exclude! matcher
|
32
|
-
|
33
|
-
return true unless raise_e
|
34
|
-
fail!("...can't contain #{matcher.inspect}")
|
33
|
+
demand !(return![matcher]), "...can't contain #{matcher.inspect}"
|
35
34
|
end
|
36
35
|
|
37
|
-
|
36
|
+
get
|
38
37
|
def matches_only!
|
39
|
-
|
40
|
-
|
41
|
-
fail!( "...has invalid characters: #{str.inspect}" )
|
42
|
-
end
|
38
|
+
invalid = return!.gsub(matcher, '')
|
39
|
+
demand invalid.empty?, "...has invalid characters: #{str.inspect}"
|
43
40
|
end
|
44
41
|
|
45
|
-
|
42
|
+
get
|
46
43
|
def not_empty!
|
47
|
-
|
48
|
-
fail!("...can't be empty.")
|
49
|
-
end
|
44
|
+
demand !(return!.strip.empty?), "...can't be empty."
|
50
45
|
end
|
51
46
|
|
52
|
-
|
47
|
+
get
|
53
48
|
def file_read!
|
54
|
-
|
49
|
+
return!.gsub("\r\n", "\n")
|
55
50
|
end
|
56
51
|
|
57
|
-
|
52
|
+
get
|
58
53
|
def new_content!
|
59
54
|
not_empty!
|
60
55
|
file_read!
|
61
56
|
end
|
62
57
|
|
63
|
-
|
58
|
+
get
|
64
59
|
def file_content!
|
65
60
|
new_content!
|
66
61
|
end
|
67
62
|
|
68
|
-
|
63
|
+
get
|
69
64
|
def hostname!
|
70
|
-
|
71
|
-
|
65
|
+
invalid = return![ %r!([^\dA-Za-z_-]+)! ]
|
66
|
+
demand !invalid, "...has invalid characters: #{$1.inspect}"
|
72
67
|
end
|
73
68
|
|
74
69
|
end # === class String
|
75
70
|
end # === class Demand
|
76
|
-
end # ===
|
71
|
+
end # === class Checked
|
77
72
|
|
@@ -1,28 +1,22 @@
|
|
1
1
|
|
2
|
-
|
2
|
+
class Checked
|
3
3
|
class Demand
|
4
|
-
class Symbols
|
4
|
+
class Symbols < Sinatra::Base
|
5
5
|
|
6
|
-
include
|
7
|
-
|
8
|
-
namespace '/symbol!'
|
6
|
+
include Checked::Arch
|
7
|
+
map '/symbol!'
|
9
8
|
|
10
|
-
|
9
|
+
get
|
11
10
|
def check!
|
12
|
-
|
13
|
-
when Symbol
|
14
|
-
else
|
15
|
-
fail! '...must be a Symbol.'
|
16
|
-
end
|
11
|
+
demand return!.is_a?(Symbol), '...must be a symbol.'
|
17
12
|
end
|
18
13
|
|
19
|
-
|
20
|
-
def in!
|
21
|
-
|
14
|
+
get
|
15
|
+
def in!
|
16
|
+
arr = args_hash['args'].first
|
17
|
+
demand arr.include?(return!), "...must be in array: #{arr}"
|
22
18
|
end # === def in!
|
23
19
|
|
24
|
-
|
25
|
-
|
26
20
|
end # === class Symbols
|
27
21
|
end # === class Demand
|
28
|
-
end # ===
|
22
|
+
end # === class Checked
|
data/lib/Checked/Demand/Vars.rb
CHANGED
@@ -1,62 +1,102 @@
|
|
1
|
-
|
1
|
+
class Checked
|
2
2
|
class Demand
|
3
|
-
class Vars
|
3
|
+
class Vars < Sinatra::Base
|
4
4
|
|
5
|
-
include
|
6
|
-
|
7
|
-
|
5
|
+
include Checked::Arch
|
6
|
+
|
7
|
+
map '/:type!'
|
8
|
+
|
9
|
+
get
|
10
|
+
def not_empty!
|
11
|
+
demand !return!.empty?, "...can't be empty."
|
12
|
+
end
|
13
|
+
|
14
|
+
get
|
15
|
+
def be!
|
16
|
+
meth, vals = args_hash['args']
|
17
|
+
answer = return!.send meth, *vals
|
18
|
+
bool! answer
|
19
|
+
demand answer, "...failed #{meth} with #{vals.inspect}"
|
20
|
+
end
|
21
|
+
|
22
|
+
get
|
23
|
+
def not_be!
|
24
|
+
meth, vals = args_hash['args']
|
25
|
+
answer = return!.send(meth, *vals)
|
26
|
+
bool! answer
|
27
|
+
demand !answer, "...#{meth} should not be true with #{vals.inspect}"
|
28
|
+
end
|
29
|
+
|
30
|
+
get
|
31
|
+
def empty!
|
32
|
+
demand return!.empty?, "...must be empty."
|
33
|
+
end
|
34
|
+
|
35
|
+
get
|
36
|
+
def not_empty!
|
37
|
+
demand !return!.empty?, "...can't be empty."
|
38
|
+
end
|
39
|
+
|
40
|
+
map '/var!' # ===============================
|
41
|
+
|
42
|
+
get
|
43
|
+
def check!
|
44
|
+
return!
|
45
|
+
end
|
8
46
|
|
9
|
-
|
47
|
+
get
|
10
48
|
def either!
|
11
|
-
|
49
|
+
answer = args_hash['args'].flatten.detect { |m|
|
12
50
|
begin
|
13
|
-
|
51
|
+
return!.send m
|
14
52
|
true
|
15
53
|
rescue Failed
|
16
54
|
false
|
17
55
|
end
|
18
56
|
}
|
57
|
+
demand answer, "...is not any: #{args_hash['args'].inspect}"
|
19
58
|
end
|
20
59
|
|
21
|
-
|
60
|
+
get
|
22
61
|
def be!
|
23
|
-
rejected =
|
24
|
-
!(
|
62
|
+
rejected = args_hash['args'].flatten.select { |m|
|
63
|
+
!(return!.send m)
|
25
64
|
}
|
26
|
-
|
65
|
+
demand rejected.empty?, "...must be all of these: #{rejected.map(&:to_s).join(', ')}"
|
27
66
|
end
|
28
67
|
|
29
|
-
|
68
|
+
get
|
30
69
|
def not_be!
|
31
|
-
rejected =
|
32
|
-
!!(
|
70
|
+
rejected = args_hash['args'].flatten.select { |m|
|
71
|
+
!!(return!.send m)
|
33
72
|
}
|
34
|
-
|
73
|
+
demand rejected.empty?, "...must not be: #{rejected.map(&:to_s).join(', ')}"
|
35
74
|
end
|
36
75
|
|
37
|
-
|
76
|
+
get
|
38
77
|
def one_of!
|
39
|
-
klasses =
|
40
|
-
|
41
|
-
|
78
|
+
klasses = args_hash['args']
|
79
|
+
demand \
|
80
|
+
klasses.flatten.any? { |k| return!.is_a?(k) }, \
|
81
|
+
"...can only be of class/module: #{klasses.map(&:to_s).join(', ')}"
|
42
82
|
end
|
43
83
|
|
44
|
-
|
84
|
+
get
|
45
85
|
def nil!
|
46
|
-
|
86
|
+
demand return!.nil?, "...must be nil."
|
47
87
|
end
|
48
88
|
|
49
|
-
|
89
|
+
get
|
50
90
|
def not_nil!
|
51
|
-
|
91
|
+
demand !return!.nil?, "...can't be nil."
|
52
92
|
end
|
53
93
|
|
54
|
-
|
94
|
+
get
|
55
95
|
def respond_to!
|
56
|
-
rejected =
|
57
|
-
!
|
96
|
+
rejected = args_hash['args'].reject { |m|
|
97
|
+
!return!.respond_to?(m)
|
58
98
|
}
|
59
|
-
|
99
|
+
demand rejected.empty?, "...must respond to #{rejected.inspect}"
|
60
100
|
end
|
61
101
|
|
62
102
|
end # === class Vars
|
data/lib/Checked/version.rb
CHANGED