Checked 0.1.4 → 1.0.0
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.
- data/Checked.gemspec +2 -3
- data/lib/Checked.rb +21 -9
- data/lib/Checked/Ask/Arrays.rb +31 -0
- data/lib/Checked/Ask/Ask.rb +21 -0
- data/lib/Checked/Ask/Strings.rb +27 -0
- data/lib/Checked/Ask/Vars.rb +21 -0
- data/lib/Checked/Base/Arch.rb +20 -0
- data/lib/Checked/Base/Base.rb +110 -0
- data/lib/Checked/Base/DSL.rb +73 -0
- data/lib/Checked/Base/DSL_Obj.rb +35 -0
- data/lib/Checked/{Clean.rb → Clean/Clean.rb} +1 -4
- data/lib/Checked/Clean/Strings.rb +94 -0
- data/lib/Checked/Demand/Arrays.rb +78 -0
- data/lib/Checked/Demand/Bools.rb +28 -0
- data/lib/Checked/{Demand.rb → Demand/Demand.rb} +12 -11
- data/lib/Checked/Demand/File_Paths.rb +63 -0
- data/lib/Checked/Demand/Hashs.rb +29 -0
- data/lib/Checked/Demand/Strings.rb +66 -0
- data/lib/Checked/Demand/Symbols.rb +21 -0
- data/lib/Checked/Demand/Vars.rb +65 -0
- data/lib/Checked/version.rb +1 -1
- data/spec/main.rb +5 -1
- data/spec/tests/Ask.rb +126 -8
- data/spec/tests/Clean.rb +66 -8
- data/spec/tests/DSL.rb +31 -0
- data/spec/tests/Demand.rb +220 -29
- metadata +38 -32
- data/lib/Checked/Args.rb +0 -55
- data/lib/Checked/Ask.rb +0 -69
- data/lib/Checked/Ask/DSL.rb +0 -31
- data/lib/Checked/Ask/Mods/Arrays.rb +0 -25
- data/lib/Checked/Ask/Mods/Strings.rb +0 -26
- data/lib/Checked/Ask/Mods/Vars.rb +0 -12
- data/lib/Checked/Base.rb +0 -119
- data/lib/Checked/Clean/DSL.rb +0 -16
- data/lib/Checked/Clean/Mods/Strings.rb +0 -104
- data/lib/Checked/Demand/DSL.rb +0 -29
- data/lib/Checked/Demand/Mods/Arrays.rb +0 -72
- data/lib/Checked/Demand/Mods/Bools.rb +0 -37
- data/lib/Checked/Demand/Mods/File_Addresses.rb +0 -59
- data/lib/Checked/Demand/Mods/Strings.rb +0 -51
- data/lib/Checked/Demand/Mods/Symbols.rb +0 -20
- data/lib/Checked/Demand/Mods/Vars.rb +0 -91
- data/spec/tests/Ask_Strings.rb +0 -57
- data/spec/tests/Checked.rb +0 -43
- data/spec/tests/Clean_Strings.rb +0 -76
- data/spec/tests/Demand_Arrays.rb +0 -31
- data/spec/tests/Demand_File_Addresses.rb +0 -61
- data/spec/tests/Demand_Vars.rb +0 -19
@@ -0,0 +1,78 @@
|
|
1
|
+
module Checked
|
2
|
+
class Demand
|
3
|
+
class Arrays
|
4
|
+
|
5
|
+
include Demand::Base
|
6
|
+
|
7
|
+
namespace '/array!'
|
8
|
+
|
9
|
+
before
|
10
|
+
def validate_target_class
|
11
|
+
fail! "...is not an Array." unless array?(target)
|
12
|
+
end
|
13
|
+
|
14
|
+
route
|
15
|
+
def no_nils!
|
16
|
+
if target.include?(nil)
|
17
|
+
fail!("...can't contain nils.")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
route
|
22
|
+
def no_empty_strings!
|
23
|
+
target.each { |s|
|
24
|
+
|
25
|
+
if s.respond_to?(:rewind)
|
26
|
+
s.rewind
|
27
|
+
end
|
28
|
+
|
29
|
+
final = if s.respond_to?(:readlines)
|
30
|
+
s.readlines
|
31
|
+
else
|
32
|
+
s
|
33
|
+
end
|
34
|
+
|
35
|
+
if !final.is_a?(::String)
|
36
|
+
fail!("Array contains unknown class: #{final.inspect}")
|
37
|
+
end
|
38
|
+
|
39
|
+
if final.is_a?(String) && final.strip.empty?
|
40
|
+
fail!("...can't contain empty strings.")
|
41
|
+
end
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
route
|
46
|
+
def symbols!
|
47
|
+
not_empty!
|
48
|
+
if !target.all? { |v| v.is_a?(Symbol) }
|
49
|
+
fail! "...contains a non-symbol."
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
route
|
54
|
+
def include!
|
55
|
+
return true if target.include?(matcher)
|
56
|
+
fail!("...must contain: #{matcher.inspect}")
|
57
|
+
end
|
58
|
+
|
59
|
+
route
|
60
|
+
def exclude!
|
61
|
+
raise_e = val.include?(matcher)
|
62
|
+
return true unless raise_e
|
63
|
+
fail!("...can't contain #{matcher.inspect}")
|
64
|
+
end
|
65
|
+
|
66
|
+
route
|
67
|
+
def matches_only!
|
68
|
+
arr = target.reject { |val| val == matcher }
|
69
|
+
fail!( "...invalid elements: #{arr.inspect}" ) if !arr.empty?
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
|
75
|
+
end # === class Arrays
|
76
|
+
end # === class Demand
|
77
|
+
end # === module Checked
|
78
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Checked
|
2
|
+
class Demand
|
3
|
+
class Bools
|
4
|
+
|
5
|
+
include Demand::Base
|
6
|
+
|
7
|
+
namespace '/bool!'
|
8
|
+
|
9
|
+
before
|
10
|
+
def validate
|
11
|
+
fail!("...must be either of TrueClass or FalseClass.") unless [TrueClass, FalseClass].include?(target.class)
|
12
|
+
end
|
13
|
+
|
14
|
+
route
|
15
|
+
def true!
|
16
|
+
fail! "...must be true (TrueClass)." unless target.class == TrueClass
|
17
|
+
end
|
18
|
+
|
19
|
+
route
|
20
|
+
def false!
|
21
|
+
fail! "...must be false (FalseClass)." unless target.class == FalseClass
|
22
|
+
end
|
23
|
+
|
24
|
+
end # === class Bools
|
25
|
+
end # === class Demand
|
26
|
+
end # === module Checked
|
27
|
+
|
28
|
+
|
@@ -1,19 +1,19 @@
|
|
1
|
-
require "Checked/Base"
|
2
|
-
require "Checked/Demand/DSL"
|
3
1
|
|
4
2
|
|
5
3
|
module Checked
|
6
|
-
|
7
4
|
class Demand
|
8
5
|
|
9
6
|
Failed = Class.new(RuntimeError)
|
10
|
-
|
7
|
+
|
11
8
|
module Base
|
12
9
|
|
13
|
-
include
|
14
|
-
|
15
|
-
|
16
|
-
|
10
|
+
include Checked::Base
|
11
|
+
|
12
|
+
route "/!w!!/check!/"
|
13
|
+
def check!
|
14
|
+
# do nothing
|
15
|
+
end
|
16
|
+
|
17
17
|
def err_msg msg = "...is invalid."
|
18
18
|
message = if msg.strip[ %r!^\.\.\.! ]
|
19
19
|
msg.sub('...', '').strip
|
@@ -21,7 +21,7 @@ module Checked
|
|
21
21
|
msg
|
22
22
|
end
|
23
23
|
|
24
|
-
@err_msg || "#{target_name} #{message}"
|
24
|
+
@err_msg || "#{target_name}, #{original_target.inspect}, #{message}"
|
25
25
|
end
|
26
26
|
|
27
27
|
def err_msg= msg
|
@@ -37,10 +37,11 @@ module Checked
|
|
37
37
|
|
38
38
|
end # === module Base
|
39
39
|
|
40
|
-
|
40
|
+
def initialize *args
|
41
|
+
raise "Demand not allowed to be used."
|
42
|
+
end
|
41
43
|
|
42
44
|
end # === class Demand
|
43
|
-
|
44
45
|
end # === module Checked
|
45
46
|
|
46
47
|
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Checked
|
2
|
+
class Demand
|
3
|
+
class File_Paths
|
4
|
+
|
5
|
+
include Demand::Base
|
6
|
+
|
7
|
+
namespace '/file_path!'
|
8
|
+
|
9
|
+
before
|
10
|
+
def validate
|
11
|
+
fail!('...must be a String.') unless target.is_a?(String)
|
12
|
+
|
13
|
+
strip_target
|
14
|
+
not_empty!
|
15
|
+
validate_format!
|
16
|
+
expand_target if fs_path?
|
17
|
+
end
|
18
|
+
|
19
|
+
route
|
20
|
+
def hostname!
|
21
|
+
matches_only! %r![\dA-Za-z_-]!
|
22
|
+
end
|
23
|
+
|
24
|
+
route
|
25
|
+
def not_dir!
|
26
|
+
if File.directory?(target)
|
27
|
+
fail! "...can't be an existing directory."
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
route
|
32
|
+
def not_file!
|
33
|
+
fail! "...can't be a file." if File.file?(target)
|
34
|
+
end
|
35
|
+
|
36
|
+
route
|
37
|
+
def dir!
|
38
|
+
fail! "...must be an existing directory." unless File.directory?(target)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def validate_format!
|
44
|
+
if target[%r!([^a-zA-Z0-9\.\_\-\/~,]+)!]
|
45
|
+
fail! "...has invalid characters: #{$1.inspect}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def expand_target
|
50
|
+
request.response.body= File.expand_path(target)
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# fs_path => File system object path
|
55
|
+
#
|
56
|
+
def fs_path?
|
57
|
+
request.path[%r!(_|/)(dir|file)[^a-zA-Z]+\Z!]
|
58
|
+
end
|
59
|
+
|
60
|
+
end # === class File_Addresses
|
61
|
+
end # === class Demand
|
62
|
+
end # === module Checked
|
63
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Checked
|
2
|
+
class Demand
|
3
|
+
class Hashs
|
4
|
+
|
5
|
+
include Demand::Base
|
6
|
+
|
7
|
+
namespace '/hash!'
|
8
|
+
|
9
|
+
before
|
10
|
+
def validate_target_class
|
11
|
+
fail!("...must be a Hash") unless hash?(target)
|
12
|
+
end
|
13
|
+
|
14
|
+
route
|
15
|
+
def symbol_keys!
|
16
|
+
keys = target.keys
|
17
|
+
|
18
|
+
if keys.all? { |k| k.is_a?(Symbol) }
|
19
|
+
# do nothing
|
20
|
+
else
|
21
|
+
fail! '...must have all symbol keys.'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end # === class Hashs
|
26
|
+
end # === class Demand
|
27
|
+
end # === module Checked
|
28
|
+
|
29
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
|
2
|
+
module Checked
|
3
|
+
class Demand
|
4
|
+
class Strings
|
5
|
+
|
6
|
+
include Demand::Base
|
7
|
+
|
8
|
+
namespace '/string!'
|
9
|
+
|
10
|
+
before
|
11
|
+
def validate
|
12
|
+
case target
|
13
|
+
when String
|
14
|
+
when StringIO
|
15
|
+
target.rewind
|
16
|
+
request.response.body= target.readlines
|
17
|
+
target.rewind
|
18
|
+
else
|
19
|
+
fail! "...must be a String or StringIO"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
route
|
24
|
+
def include!
|
25
|
+
included = target[matcher]
|
26
|
+
return true if included
|
27
|
+
fail!("...must contain: #{matcher.inspect}")
|
28
|
+
end
|
29
|
+
|
30
|
+
route
|
31
|
+
def exclude! matcher
|
32
|
+
raise_e = val[matcher]
|
33
|
+
return true unless raise_e
|
34
|
+
fail!("...can't contain #{matcher.inspect}")
|
35
|
+
end
|
36
|
+
|
37
|
+
route
|
38
|
+
def matches_only!
|
39
|
+
str = target.gsub(matcher, '')
|
40
|
+
if !str.empty?
|
41
|
+
fail!( "...has invalid characters: #{str.inspect}" )
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
route
|
46
|
+
def not_empty!
|
47
|
+
if target.strip.empty?
|
48
|
+
fail!("...can't be empty.")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
route
|
53
|
+
def file_read!
|
54
|
+
request.response.body= target.gsub("\r\n", "\n")
|
55
|
+
end
|
56
|
+
|
57
|
+
route
|
58
|
+
def file_content!
|
59
|
+
not_empty!
|
60
|
+
file_read!
|
61
|
+
end
|
62
|
+
|
63
|
+
end # === class String
|
64
|
+
end # === class Demand
|
65
|
+
end # === module Checked
|
66
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
module Checked
|
3
|
+
class Demand
|
4
|
+
class Symbols
|
5
|
+
|
6
|
+
include Demand::Base
|
7
|
+
|
8
|
+
namespace '/symbol!'
|
9
|
+
|
10
|
+
before
|
11
|
+
def validate
|
12
|
+
case target
|
13
|
+
when Symbol
|
14
|
+
else
|
15
|
+
fail! '...must be a Symbol.'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end # === class Symbols
|
20
|
+
end # === class Demand
|
21
|
+
end # === module Checked
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Checked
|
2
|
+
class Demand
|
3
|
+
class Vars
|
4
|
+
|
5
|
+
include Demand::Base
|
6
|
+
|
7
|
+
namespace '/var!'
|
8
|
+
|
9
|
+
route
|
10
|
+
def either!
|
11
|
+
request.headers.args.flatten.detect { |m|
|
12
|
+
begin
|
13
|
+
target.send m
|
14
|
+
true
|
15
|
+
rescue Failed
|
16
|
+
false
|
17
|
+
end
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
route
|
22
|
+
def be!
|
23
|
+
rejected = request.headers.args.flatten.select { |m|
|
24
|
+
!(target.send m)
|
25
|
+
}
|
26
|
+
fail!("...must be: #{rejected.map(&:to_s).join(', ')}") unless rejected.empty?
|
27
|
+
end
|
28
|
+
|
29
|
+
route
|
30
|
+
def not_be!
|
31
|
+
rejected = request.headers.args.flatten.select { |m|
|
32
|
+
!!(target.send m)
|
33
|
+
}
|
34
|
+
fail!("...must not be: #{rejected.map(&:to_s).join(', ')}") unless rejected.empty?
|
35
|
+
end
|
36
|
+
|
37
|
+
route
|
38
|
+
def one_of!
|
39
|
+
klasses = request.headers.args
|
40
|
+
return true if klasses.flatten.any? { |k| target.is_a?(k) }
|
41
|
+
fail! "...can only be of class/module: #{klasses.map(&:to_s).join(', ')}"
|
42
|
+
end
|
43
|
+
|
44
|
+
route
|
45
|
+
def nil!
|
46
|
+
fail!("...must be nil.") unless target.nil?
|
47
|
+
end
|
48
|
+
|
49
|
+
route
|
50
|
+
def not_nil!
|
51
|
+
fail!("...can't be nil.") if target.nil?
|
52
|
+
end
|
53
|
+
|
54
|
+
route
|
55
|
+
def respond_to!
|
56
|
+
rejected = request.headers.args.reject { |m|
|
57
|
+
!target.respond_to?(m)
|
58
|
+
}
|
59
|
+
fail!("...must respond to #{rejected.inspect}") unless rejected.empty?
|
60
|
+
end
|
61
|
+
|
62
|
+
end # === class Vars
|
63
|
+
end # === class Demand
|
64
|
+
end # === module Checked
|
65
|
+
|
data/lib/Checked/version.rb
CHANGED
data/spec/main.rb
CHANGED
@@ -3,6 +3,11 @@ require File.expand_path('spec/helper')
|
|
3
3
|
require "Bacon_Colored"
|
4
4
|
require 'Checked'
|
5
5
|
|
6
|
+
class Box
|
7
|
+
include Checked::DSL
|
8
|
+
end
|
9
|
+
|
10
|
+
BOX = Box.new
|
6
11
|
|
7
12
|
FOLDER = ("/tmp/Checked_Test")
|
8
13
|
%x! mkdir -p #{FOLDER}!
|
@@ -26,7 +31,6 @@ def ruby_e cmd
|
|
26
31
|
end
|
27
32
|
end
|
28
33
|
|
29
|
-
|
30
34
|
Dir.glob('spec/tests/*.rb').each { |file|
|
31
35
|
require File.expand_path(file.sub('.rb', '')) if File.file?(file)
|
32
36
|
}
|
data/spec/tests/Ask.rb
CHANGED
@@ -1,13 +1,131 @@
|
|
1
1
|
|
2
2
|
|
3
|
-
|
3
|
+
# ============================
|
4
|
+
# ============================ ARRAYS
|
5
|
+
# ============================
|
6
|
+
|
7
|
+
describe "array! :include?" do
|
8
|
+
|
9
|
+
it "returns true if array contains element." do
|
10
|
+
BOX.array!([:a]).include?(:a)
|
11
|
+
.should.be == true
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns false if array does not contain element" do
|
15
|
+
BOX.array!([:a]).include?(:b)
|
16
|
+
.should.be == false
|
17
|
+
end
|
18
|
+
|
19
|
+
end # === describe Ask :includes
|
20
|
+
|
21
|
+
describe "array! :exclude?" do
|
22
|
+
|
23
|
+
it "returns true if array excludes element." do
|
24
|
+
BOX.array!([:a]).exclude?(:b)
|
25
|
+
.should.be == true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns false if array does contains element" do
|
29
|
+
BOX.array!([:a]).exclude?(:a)
|
30
|
+
.should.be == false
|
31
|
+
end
|
32
|
+
|
33
|
+
end # === describe Ask :exclude
|
34
|
+
|
35
|
+
describe "array! symbols?" do
|
36
|
+
|
37
|
+
it 'returns true if all elements are symbols' do
|
38
|
+
BOX.array!([:a, :b]).symbols?
|
39
|
+
.should.be == true
|
40
|
+
end
|
41
|
+
|
42
|
+
end # === describe array! symbols?
|
43
|
+
#
|
44
|
+
# describe "Ask :excludes" do
|
45
|
+
#
|
46
|
+
# behaves_like 'Ask'
|
47
|
+
#
|
48
|
+
# it "returns true if string excludes a Regexp matcher" do
|
49
|
+
# Checked::Ask.new(" :a ") { |a|
|
50
|
+
# a.< :excludes?, / :b /
|
51
|
+
# }.true?.should.be == true
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# it 'returns false if string includes a Regexp matcher' do
|
55
|
+
# Checked::Ask.new(" :a ") { |a|
|
56
|
+
# a.< :excludes?, / :a /
|
57
|
+
# }.true?.should.be == false
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# end # === describe Ask :excludes
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
# ============================
|
65
|
+
# ============================ STRINGS
|
66
|
+
# ============================
|
67
|
+
|
68
|
+
describe "string! :include?" do
|
69
|
+
|
70
|
+
it "returns true if string contains regexp." do
|
71
|
+
BOX.string!(": a").include?(/: a/)
|
72
|
+
.should.be == true
|
73
|
+
end
|
74
|
+
|
75
|
+
it "returns false if string does not element" do
|
76
|
+
BOX.string!(" :a ").include?(/ :b /)
|
77
|
+
.should.be == false
|
78
|
+
end
|
79
|
+
|
80
|
+
end # === describe Ask :includes
|
81
|
+
|
82
|
+
describe "string! :exclude?" do
|
83
|
+
|
84
|
+
it "returns true if string excludes regexp." do
|
85
|
+
BOX.string!(": a").exclude?(/: b/)
|
86
|
+
.should.be == true
|
87
|
+
end
|
88
|
+
|
89
|
+
it "returns false if string does not excludes element" do
|
90
|
+
BOX.string!(" :a ").exclude?(/ :a /)
|
91
|
+
.should.be == false
|
92
|
+
end
|
93
|
+
|
94
|
+
end # === describe Ask :excludes
|
95
|
+
|
96
|
+
|
97
|
+
describe "ask empty?" do
|
98
|
+
|
99
|
+
it "returns true if string is :empty? after applying :strip" do
|
100
|
+
BOX.string!(" \n ").empty?.should.be === true
|
101
|
+
end
|
102
|
+
|
103
|
+
it "returns false if string is not :empty? after applying :strip" do
|
104
|
+
BOX.string!(" n ").empty?.should.be === false
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
end # === describe Ask Strings
|
109
|
+
|
110
|
+
# ============================
|
111
|
+
# ============================ VARS
|
112
|
+
# ============================
|
113
|
+
|
114
|
+
describe "var! :respond_to?" do
|
115
|
+
|
116
|
+
it 'returns true if it responds to methods' do
|
117
|
+
BOX.var!( [] ).respond_to?(:[], :to_s, :pop)
|
118
|
+
.should == true
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'returns false if it does not to at least one method' do
|
122
|
+
BOX.var!( [] ).respond_to?(:[], :to_s, :keys)
|
123
|
+
.should == false
|
124
|
+
end
|
4
125
|
|
5
|
-
it '
|
6
|
-
|
7
|
-
|
8
|
-
puts Checked::Ask::DSL.to_s
|
9
|
-
!)
|
10
|
-
.should.be == 'Checked::Ask::DSL'
|
126
|
+
it 'returns false if arg list is empty' do
|
127
|
+
BOX.var!( [] ).respond_to?
|
128
|
+
.should == false
|
11
129
|
end
|
12
130
|
|
13
|
-
end # === describe
|
131
|
+
end # === describe var! :respond_to?
|