Checked 1.2.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,78 +1,73 @@
1
- module Checked
1
+ class Checked
2
2
  class Demand
3
- class Arrays
3
+ class Arrays < Sinatra::Base
4
4
 
5
- include Uni_Arch::Base
6
- include Demand::Base
7
- namespace '/array!'
5
+ include Checked::Arch
6
+ map '/array!'
8
7
 
9
- route
8
+ get
10
9
  def check!
11
- fail! "...is not an Array." unless array?(target)
10
+ array! return!
12
11
  end
13
12
 
14
- route
13
+ get
15
14
  def no_nils!
16
- if target.include?(nil)
17
- fail!("...can't contain nils.")
18
- end
15
+ demand \
16
+ return!.include?(nil), \
17
+ "...can't contain nils."
19
18
  end
20
19
 
21
- route
20
+ get
22
21
  def no_empty_strings!
23
- target.each { |s|
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
- 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
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
- route
43
+ get
46
44
  def symbols!
47
- not_empty!
48
- if !target.all? { |v| v.is_a?(Symbol) }
49
- fail! "...contains a non-symbol."
50
- end
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
- route
51
+ get
54
52
  def include!
55
- return true if target.include?(matcher)
56
- fail!("...must contain: #{matcher.inspect}")
53
+ demand return!.include?(matcher), \
54
+ "...must contain: #{matcher.inspect}"
57
55
  end
58
56
 
59
- route
57
+ get
60
58
  def exclude!
61
- raise_e = val.include?(matcher)
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
- route
62
+ get
67
63
  def matches_only!
68
- arr = target.reject { |val| val == matcher }
69
- fail!( "...invalid elements: #{arr.inspect}" ) if !arr.empty?
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 # === module Checked
72
+ end # === class Checked
78
73
 
@@ -1,28 +1,30 @@
1
- module Checked
1
+ class Checked
2
2
  class Demand
3
- class Bools
3
+ class Bools < Sinatra::Base
4
4
 
5
- include Uni_Arch::Base
6
- include Demand::Base
7
- namespace '/bool!'
5
+ include Checked::Arch
6
+ map '/bool!'
8
7
 
9
- route
8
+ get
10
9
  def check!
11
- fail!("...must be either of TrueClass or FalseClass.") unless [TrueClass, FalseClass].include?(target.class)
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
- route
14
+ get
15
15
  def true!
16
- fail! "...must be true (TrueClass)." unless target.class == TrueClass
16
+ is_true = return!.class == TrueClass
17
+ demand is_true, "...must be true (TrueClass)."
17
18
  end
18
19
 
19
- route
20
+ get
20
21
  def false!
21
- fail! "...must be false (FalseClass)." unless target.class == FalseClass
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 # === module Checked
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
- end # === class Demand
45
- end # === module Checked
9
+ end # === class Demand
10
+ end # === class Checked
46
11
 
47
12
 
@@ -1,48 +1,47 @@
1
- module Checked
1
+ class Checked
2
2
  class Demand
3
- class File_Paths
3
+ class File_Paths < Sinatra::Base
4
4
 
5
- include Uni_Arch::Base
6
- include Demand::Base
7
- namespace '/file_path!'
5
+ INVALID_CHARS = %r!([^a-zA-Z0-9\.\_\-\/~,]+)!
6
+ include Checked::Arch
7
+ map '/file_path!'
8
8
 
9
- route
9
+ get
10
10
  def check!
11
- fail!('...must be a String.') unless target.is_a?(String)
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 fs_path?
17
+ expand_target if File.exists?(File.expand_path return!)
18
+
19
+ return!
17
20
  end
18
21
 
19
- route
22
+ get
20
23
  def not_dir!
21
- if File.directory?(target)
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
- route
27
+ get
27
28
  def not_file!
28
- fail! "...can't be a file." if File.file?(target)
29
+ demand !File.file?(return!), "...can't be a file."
29
30
  end
30
31
 
31
- route
32
+ get
32
33
  def dir!
33
- fail! "...must be an existing directory." unless File.directory?(target)
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
- if target[%r!([^a-zA-Z0-9\.\_\-\/~,]+)!]
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
- request.response.body= File.expand_path(target)
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 # === module Checked
56
+ end # === class Checked
58
57
 
@@ -1,29 +1,24 @@
1
- module Checked
1
+ class Checked
2
2
  class Demand
3
- class Hashs
3
+ class Hashs < Sinatra::Base
4
4
 
5
- include Uni_Arch::Base
6
- include Demand::Base
7
- namespace '/hash!'
5
+
6
+ include Checked::Arch
7
+ map '/hash!'
8
8
 
9
- route
9
+ get
10
10
  def check!
11
- fail!("...must be a Hash") unless hash?(target)
11
+ demand hash?(return!), "...is not a Hash."
12
12
  end
13
13
 
14
- route
14
+ get
15
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
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 # === module Checked
22
+ end # === class Checked
28
23
 
29
24
 
@@ -1,77 +1,72 @@
1
1
 
2
- module Checked
2
+ class Checked
3
3
  class Demand
4
- class Strings
4
+ class Strings < Sinatra::Base
5
5
 
6
- include Uni_Arch::Base
7
- include Demand::Base
8
- namespace '/string!'
6
+ include Checked::Arch
7
+ map '/string!'
9
8
 
10
- route
9
+ get
11
10
  def check!
12
- case target
11
+ case return!
13
12
  when String
13
+
14
+ return!
14
15
  when StringIO
15
16
  target.rewind
16
- request.response.body= target.readlines
17
+ return! target.readlines
17
18
  target.rewind
19
+
20
+ return!
18
21
  else
19
- fail! "...must be a String or StringIO"
22
+ demand false, "...must be a String or StringIO."
20
23
  end
21
24
  end
22
25
 
23
- route
26
+ get
24
27
  def include!
25
- included = target[matcher]
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
- route
31
+ get
31
32
  def exclude! matcher
32
- raise_e = val[matcher]
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
- route
36
+ get
38
37
  def matches_only!
39
- str = target.gsub(matcher, '')
40
- if !str.empty?
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
- route
42
+ get
46
43
  def not_empty!
47
- if target.strip.empty?
48
- fail!("...can't be empty.")
49
- end
44
+ demand !(return!.strip.empty?), "...can't be empty."
50
45
  end
51
46
 
52
- route
47
+ get
53
48
  def file_read!
54
- request.response.body= target.gsub("\r\n", "\n")
49
+ return!.gsub("\r\n", "\n")
55
50
  end
56
51
 
57
- route
52
+ get
58
53
  def new_content!
59
54
  not_empty!
60
55
  file_read!
61
56
  end
62
57
 
63
- route
58
+ get
64
59
  def file_content!
65
60
  new_content!
66
61
  end
67
62
 
68
- route
63
+ get
69
64
  def hostname!
70
- puts target
71
- fail!("...has invalid characters: #{$1.inspect}") if target[ %r!([^\dA-Za-z_-]+)! ]
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 # === module Checked
71
+ end # === class Checked
77
72
 
@@ -1,28 +1,22 @@
1
1
 
2
- module Checked
2
+ class Checked
3
3
  class Demand
4
- class Symbols
4
+ class Symbols < Sinatra::Base
5
5
 
6
- include Uni_Arch::Base
7
- include Demand::Base
8
- namespace '/symbol!'
6
+ include Checked::Arch
7
+ map '/symbol!'
9
8
 
10
- route
9
+ get
11
10
  def check!
12
- case target
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
- route
20
- def in! arr
21
- fail! "...must be in array: #{arr}" unless arr.include?(target)
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 # === module Checked
22
+ end # === class Checked
@@ -1,62 +1,102 @@
1
- module Checked
1
+ class Checked
2
2
  class Demand
3
- class Vars
3
+ class Vars < Sinatra::Base
4
4
 
5
- include Uni_Arch::Base
6
- include Demand::Base
7
- namespace '/var!'
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
- route
47
+ get
10
48
  def either!
11
- request.headers.args.flatten.detect { |m|
49
+ answer = args_hash['args'].flatten.detect { |m|
12
50
  begin
13
- target.send m
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
- route
60
+ get
22
61
  def be!
23
- rejected = request.headers.args.flatten.select { |m|
24
- !(target.send m)
62
+ rejected = args_hash['args'].flatten.select { |m|
63
+ !(return!.send m)
25
64
  }
26
- fail!("...must be: #{rejected.map(&:to_s).join(', ')}") unless rejected.empty?
65
+ demand rejected.empty?, "...must be all of these: #{rejected.map(&:to_s).join(', ')}"
27
66
  end
28
67
 
29
- route
68
+ get
30
69
  def not_be!
31
- rejected = request.headers.args.flatten.select { |m|
32
- !!(target.send m)
70
+ rejected = args_hash['args'].flatten.select { |m|
71
+ !!(return!.send m)
33
72
  }
34
- fail!("...must not be: #{rejected.map(&:to_s).join(', ')}") unless rejected.empty?
73
+ demand rejected.empty?, "...must not be: #{rejected.map(&:to_s).join(', ')}"
35
74
  end
36
75
 
37
- route
76
+ get
38
77
  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(', ')}"
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
- route
84
+ get
45
85
  def nil!
46
- fail!("...must be nil.") unless target.nil?
86
+ demand return!.nil?, "...must be nil."
47
87
  end
48
88
 
49
- route
89
+ get
50
90
  def not_nil!
51
- fail!("...can't be nil.") if target.nil?
91
+ demand !return!.nil?, "...can't be nil."
52
92
  end
53
93
 
54
- route
94
+ get
55
95
  def respond_to!
56
- rejected = request.headers.args.reject { |m|
57
- !target.respond_to?(m)
96
+ rejected = args_hash['args'].reject { |m|
97
+ !return!.respond_to?(m)
58
98
  }
59
- fail!("...must respond to #{rejected.inspect}") unless rejected.empty?
99
+ demand rejected.empty?, "...must respond to #{rejected.inspect}"
60
100
  end
61
101
 
62
102
  end # === class Vars
@@ -1,3 +1,5 @@
1
- module Checked
2
- VERSION = "1.2.3"
1
+ require 'Sin_Arch'
2
+
3
+ class Checked < Sinatra::Base
4
+ VERSION = "2.0.0"
3
5
  end