Checked 2.0.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,57 +1,50 @@
1
1
  class Checked
2
- class Demand
3
- class File_Paths < Sinatra::Base
4
-
5
- INVALID_CHARS = %r!([^a-zA-Z0-9\.\_\-\/~,]+)!
6
- include Checked::Arch
7
- map '/file_path!'
8
-
9
- get
10
- def check!
11
- string! return!
12
-
13
- return! return!.strip
14
- not_empty! return!
15
-
16
- validate_format!
17
- expand_target if File.exists?(File.expand_path return!)
18
-
19
- return!
20
- end
21
-
22
- get
23
- def not_dir!
24
- demand !File.directory?(return!), "...can't be an existing directory."
25
- end
26
-
27
- get
28
- def not_file!
29
- demand !File.file?(return!), "...can't be a file."
30
- end
31
-
32
- get
33
- def dir!
34
- demand File.directory?(return!), "...must be an existing directory."
35
- end
36
-
37
- private
38
-
39
- def validate_format!
40
- demand !( return![INVALID_CHARS] ), "...has invalid characters: #{$1.inspect}"
41
- end
42
-
43
- def expand_target
44
- return! File.expand_path(return!)
45
- end
46
-
47
- #
48
- # fs_path => File system object path
49
- #
50
- def fs_path?
51
- request.path[%r!(_|/)(dir|file|check)[^a-zA-Z]+\Z!]
52
- end
53
-
54
- end # === class File_Addresses
55
- end # === class Demand
2
+ class File_Paths < Strings
3
+
4
+ INVALID_CHARS = %r!([^a-zA-Z0-9\.\_\-\/~,]+)!
5
+
6
+ def check!
7
+ super
8
+ string! return!
9
+
10
+ return! return!.strip
11
+ not_empty! return!
12
+
13
+ validate_format!
14
+ expand_target if File.exists?(File.expand_path return!)
15
+
16
+ return!
17
+ end
18
+
19
+ def not_dir!
20
+ demand !File.directory?(return!), "...can't be an existing directory."
21
+ end
22
+
23
+ def not_file!
24
+ demand !File.file?(return!), "...can't be a file."
25
+ end
26
+
27
+ def dir!
28
+ demand File.directory?(return!), "...must be an existing directory."
29
+ end
30
+
31
+ private
32
+
33
+ def validate_format!
34
+ demand !( return![INVALID_CHARS] ), "...has invalid characters: #{$1.inspect}"
35
+ end
36
+
37
+ def expand_target
38
+ return! File.expand_path(return!)
39
+ end
40
+
41
+ #
42
+ # fs_path => File system object path
43
+ #
44
+ def fs_path?
45
+ request.path[%r!(_|/)(dir|file|check)[^a-zA-Z]+\Z!]
46
+ end
47
+
48
+ end # === class File_Addresses
56
49
  end # === class Checked
57
50
 
@@ -1,24 +1,16 @@
1
1
  class Checked
2
- class Demand
3
- class Hashs < Sinatra::Base
4
-
5
-
6
- include Checked::Arch
7
- map '/hash!'
2
+ class Hashs
8
3
 
9
- get
10
- def check!
11
- demand hash?(return!), "...is not a Hash."
12
- end
4
+ def check!
5
+ demand hash?(return!), "...is not a Hash."
6
+ end
13
7
 
14
- get
15
- def symbol_keys!
16
- all_syms = return!.keys.all? { |k| k.is_a?(Symbol) }
17
- demand all_syms, '...must have all symbol keys.'
18
- end
8
+ def symbol_keys!
9
+ all_syms = return!.keys.all? { |k| k.is_a?(Symbol) }
10
+ demand all_syms, '...must have all symbol keys.'
11
+ end
19
12
 
20
- end # === class Hashs
21
- end # === class Demand
13
+ end # === class Hashs
22
14
  end # === class Checked
23
15
 
24
16
 
@@ -1,72 +1,67 @@
1
1
 
2
2
  class Checked
3
- class Demand
4
- class Strings < Sinatra::Base
3
+ class Strings
5
4
 
6
- include Checked::Arch
7
- map '/string!'
8
-
9
- get
10
- def check!
11
- case return!
12
- when String
13
-
14
- return!
15
- when StringIO
16
- target.rewind
17
- return! target.readlines
18
- target.rewind
19
-
20
- return!
21
- else
22
- demand false, "...must be a String or StringIO."
23
- end
24
- end
5
+ public
25
6
 
26
- get
27
- def include!
28
- demand return![matcher], "...must contain: #{matcher.inspect}"
29
- end
7
+ def check!
8
+ case return!
9
+ when String
10
+ return!
11
+
12
+ when StringIO
13
+ target.rewind
14
+ return! target.readlines
15
+ target.rewind
30
16
 
31
- get
32
- def exclude! matcher
33
- demand !(return![matcher]), "...can't contain #{matcher.inspect}"
17
+ return!
18
+
19
+ else
20
+ demand false, "...must be a String or StringIO."
34
21
  end
22
+
23
+ return!( return!.strip )
24
+ end
35
25
 
36
- get
37
- def matches_only!
38
- invalid = return!.gsub(matcher, '')
39
- demand invalid.empty?, "...has invalid characters: #{str.inspect}"
40
- end
26
+ def include! matcher
27
+ demand return![matcher], "...must contain: #{matcher.inspect}"
28
+ end
29
+
30
+ def exclude! matcher
31
+ demand !(return![matcher]), "...can't contain #{matcher.inspect}"
32
+ end
33
+
34
+ def matches_only! matcher
35
+ invalid = return!.gsub(matcher, '')
36
+ demand invalid.empty?, "...has invalid characters: #{str.inspect}"
37
+ end
41
38
 
42
- get
43
- def not_empty!
39
+ def not_empty! *args
40
+ if args.empty?
44
41
  demand !(return!.strip.empty?), "...can't be empty."
42
+ else
43
+ super
45
44
  end
45
+ end
46
46
 
47
- get
48
- def file_read!
49
- return!.gsub("\r\n", "\n")
50
- end
51
-
52
- get
53
- def new_content!
54
- not_empty!
55
- file_read!
56
- end
47
+ def file_read!
48
+ return!.gsub("\r\n", "\n")
49
+ end
57
50
 
58
- get
59
- def file_content!
60
- new_content!
61
- end
51
+ def new_content!
52
+ not_empty!
53
+ file_read!
54
+ end
62
55
 
63
- get
64
- def hostname!
65
- invalid = return![ %r!([^\dA-Za-z_-]+)! ]
66
- demand !invalid, "...has invalid characters: #{$1.inspect}"
67
- end
56
+ def file_content!
57
+ new_content!
58
+ end
59
+
60
+ def hostname!
61
+ invalid = return![ %r!([^\dA-Za-z_-]+)! ]
62
+ demand !invalid, "...has invalid characters: #{$1.inspect}"
63
+ end
68
64
 
69
- end # === class String
70
- end # === class Demand
65
+ end # === class String
71
66
  end # === class Checked
72
67
 
@@ -1,22 +1,15 @@
1
1
 
2
2
  class Checked
3
- class Demand
4
- class Symbols < Sinatra::Base
3
+ class Symbols
5
4
 
6
- include Checked::Arch
7
- map '/symbol!'
8
-
9
- get
10
- def check!
11
- demand return!.is_a?(Symbol), '...must be a symbol.'
12
- end
13
-
14
- get
15
- def in!
16
- arr = args_hash['args'].first
17
- demand arr.include?(return!), "...must be in array: #{arr}"
18
- end # === def in!
19
-
20
- end # === class Symbols
21
- end # === class Demand
5
+ def check!
6
+ demand return!.is_a?(Symbol), '...must be a symbol.'
7
+ end
8
+
9
+ def in! *raw_arrs
10
+ arrs = raw_arrs.flatten
11
+ demand arrs.flatten.include?(return!), "...must be in array: #{arrs}"
12
+ end # === def in!
13
+
14
+ end # === class Symbols
22
15
  end # === class Checked
@@ -1,105 +1,57 @@
1
1
  class Checked
2
- class Demand
3
- class Vars < Sinatra::Base
4
-
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
46
-
47
- get
48
- def either!
49
- answer = args_hash['args'].flatten.detect { |m|
50
- begin
51
- return!.send m
52
- true
53
- rescue Failed
54
- false
55
- end
56
- }
57
- demand answer, "...is not any: #{args_hash['args'].inspect}"
58
- end
59
-
60
- get
61
- def be!
62
- rejected = args_hash['args'].flatten.select { |m|
63
- !(return!.send m)
64
- }
65
- demand rejected.empty?, "...must be all of these: #{rejected.map(&:to_s).join(', ')}"
66
- end
67
-
68
- get
69
- def not_be!
70
- rejected = args_hash['args'].flatten.select { |m|
71
- !!(return!.send m)
72
- }
73
- demand rejected.empty?, "...must not be: #{rejected.map(&:to_s).join(', ')}"
74
- end
75
-
76
- get
77
- def one_of!
78
- klasses = args_hash['args']
79
- demand \
80
- klasses.flatten.any? { |k| return!.is_a?(k) }, \
2
+ class Vars
3
+
4
+ def check!
5
+ return!
6
+ end
7
+
8
+ def either! *args
9
+ answer = args.flatten.detect { |m|
10
+ begin
11
+ return!.send m
12
+ true
13
+ rescue Failed
14
+ false
15
+ end
16
+ }
17
+ demand answer, "...is not any: #{args.inspect}"
18
+ end
19
+
20
+ def be! *args
21
+ rejected = args.flatten.select { |m|
22
+ !(return!.send m)
23
+ }
24
+ demand rejected.empty?, "...must be all of these: #{rejected.map(&:to_s).join(', ')}"
25
+ end
26
+
27
+ def not_be! *args
28
+ rejected = args.flatten.select { |m|
29
+ !!(return!.send m)
30
+ }
31
+ demand rejected.empty?, "...must not be: #{rejected.map(&:to_s).join(', ')}"
32
+ end
33
+
34
+ def one_of! *klasses
35
+ demand \
36
+ klasses.flatten.any? { |k| return!.is_a?(k) }, \
81
37
  "...can only be of class/module: #{klasses.map(&:to_s).join(', ')}"
82
- end
38
+ end
83
39
 
84
- get
85
- def nil!
86
- demand return!.nil?, "...must be nil."
87
- end
40
+ def nil!
41
+ demand return!.nil?, "...must be nil."
42
+ end
88
43
 
89
- get
90
- def not_nil!
91
- demand !return!.nil?, "...can't be nil."
92
- end
44
+ def not_nil!
45
+ demand !return!.nil?, "...can't be nil."
46
+ end
93
47
 
94
- get
95
- def respond_to!
96
- rejected = args_hash['args'].reject { |m|
97
- !return!.respond_to?(m)
98
- }
99
- demand rejected.empty?, "...must respond to #{rejected.inspect}"
100
- end
48
+ def respond_to! *args
49
+ rejected = args.reject { |m|
50
+ !return!.respond_to?(m)
51
+ }
52
+ demand rejected.empty?, "...must respond to #{rejected.inspect}"
53
+ end
101
54
 
102
- end # === class Vars
103
- end # === class Demand
55
+ end # === class Vars
104
56
  end # === module Checked
105
57
 
@@ -1,5 +1,3 @@
1
- require 'Sin_Arch'
2
-
3
- class Checked < Sinatra::Base
4
- VERSION = "2.0.2"
1
+ class Checked
2
+ VERSION = "3.0.0"
5
3
  end
data/lib/Checked.rb CHANGED
@@ -5,56 +5,33 @@ require "Checked/Arch"
5
5
 
6
6
  class Checked
7
7
 
8
- class Check_Args < Sinatra::Base
9
-
10
- include Checked::Arch
8
+ class App
11
9
 
12
- before
13
- def set_target
14
- keys! args_hash, 'name', 'value', 'args'
15
- a = args_hash['args']
16
- t = args_hash['value']
17
-
18
- demand a, array?(a), ':args must be an array.'
19
-
20
- request.env['original_target'] = t
21
-
22
- # === target_name
23
- request.env['target_name'] ||= begin
24
- if t.respond_to?(:english_name)
25
- t.english_name
26
- elsif !args_hash['name'].to_s.strip.empty?
27
- args_hash['name'].strip
28
- else
29
- t.class.name.gsub('_', ' ')
30
- end
31
- end
32
-
33
- return! t
10
+ def initialize
11
+ @target = nil
34
12
  end
35
-
36
- end # === class Check_Args < Sinatra::Base
37
-
38
- use Check_Args
39
-
40
- class App
41
- include Sin_Arch::App
42
13
  end # === class App
43
14
 
44
15
  end # === class Checked
45
16
 
46
17
 
47
18
 
19
+ require "Checked/All"
48
20
  %w{ Ask Clean Demand }.each { |klass|
49
21
 
50
22
  require "Checked/#{klass}/#{klass}"
23
+ included = []
51
24
 
52
25
  Dir.glob(File.join File.dirname(__FILE__), "Checked/#{klass}/*.rb").each { |path|
53
26
  next if path["/#{klass}/#{klass}.rb"]
54
27
 
55
28
  path =~ %r!lib/Checked/(.+)/(.+)\.rb!
56
29
  require( "Checked/#{$1}/#{$2}" )
57
- Checked.use eval("Checked::#{klass}::#{$2}")
30
+
31
+ unless included.include?($2)
32
+ Checked.const_get($2.to_sym).send :include, Checked::All
33
+ included << $2
34
+ end
58
35
  }
59
36
 
60
37
  }
data/spec/tests/Ask.rb CHANGED
@@ -20,17 +20,17 @@ describe "array! :include?" do
20
20
 
21
21
  end # === describe Ask :includes
22
22
 
23
- describe "array! :exclude?" do
23
+ describe "array! :exclude_all?" do
24
24
 
25
25
  behaves_like :racked_dsl
26
26
 
27
27
  it "returns true if array excludes element." do
28
- Array!([:a]).exclude?(:b)
28
+ Array!([:a]).exclude_all?(:b)
29
29
  .should.be == true
30
30
  end
31
31
 
32
32
  it "returns false if array does contains element" do
33
- Array!([:a]).exclude?(:a)
33
+ Array!([:a]).exclude_all?(:a)
34
34
  .should.be == false
35
35
  end
36
36
 
data/spec/tests/Clean.rb CHANGED
@@ -53,7 +53,7 @@ describe "Clean :chop_slash_r" do
53
53
  Hi\r\n
54
54
  Ok\r\n
55
55
  @
56
- String!(string).chop_slash_r.should.be == string.gsub("\r", '')
56
+ String!(string).chop_slash_r.should.be == string.strip.gsub("\r", '')
57
57
  end
58
58
 
59
59
 
@@ -69,7 +69,7 @@ describe "Clean :os_stardard" do
69
69
  Hi\r\n
70
70
  Ok\r\n
71
71
  @
72
- String!(string).os_stardard.should.be == string.strip.gsub("\r", '')
72
+ String!(string).os_stardard.should.be == string.gsub("\r", '').strip
73
73
  end
74
74
 
75
75
  end # === describe
data/spec/tests/DSL.rb CHANGED
@@ -102,7 +102,7 @@ describe "Stripped!" do
102
102
  end
103
103
 
104
104
  it 'returns string with original name' do
105
- Stripped!("Test String", 'test').Checked.name.should == 'Test String'
105
+ Stripped!("Test String", 'test').Checked.target_name.should == 'Test String'
106
106
  end
107
107
 
108
108
  end # === describe Stripped!
@@ -111,9 +111,9 @@ describe "String!" do
111
111
 
112
112
  behaves_like :racked_dsl
113
113
 
114
- it 'returns the string' do
114
+ it 'returns the string with same name' do
115
115
  s = 'str'
116
- String!( s ).object_id.should.be == s.object_id
116
+ String!( 'Tester', s ).Checked.target_name.should.be == 'Tester'
117
117
  end
118
118
 
119
119
  it 'raises Demand::Failed if not a string' do
data/spec/tests/Demand.rb CHANGED
@@ -156,7 +156,7 @@ describe "Demand string! :file_read!" do
156
156
  behaves_like :racked_dsl
157
157
 
158
158
  it 'returns a string with right carriage returns erased.' do
159
- String!("test\r\ntest\r\n").file_content!.should == "test\ntest\n"
159
+ String!("test\r\ntest\r\n").file_content!.should == "test\ntest"
160
160
  end
161
161
 
162
162
  end # === describe Demand string! :file_read!
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Checked
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 3.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-15 00:00:00.000000000 Z
12
+ date: 2012-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: Sin_Arch
16
- requirement: &7738600 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: *7738600
25
14
  - !ruby/object:Gem::Dependency
26
15
  name: rake
27
- requirement: &7738060 !ruby/object:Gem::Requirement
16
+ requirement: &23425480 !ruby/object:Gem::Requirement
28
17
  none: false
29
18
  requirements:
30
19
  - - ! '>='
@@ -32,10 +21,10 @@ dependencies:
32
21
  version: '0'
33
22
  type: :development
34
23
  prerelease: false
35
- version_requirements: *7738060
24
+ version_requirements: *23425480
36
25
  - !ruby/object:Gem::Dependency
37
26
  name: bacon
38
- requirement: &7737440 !ruby/object:Gem::Requirement
27
+ requirement: &23425020 !ruby/object:Gem::Requirement
39
28
  none: false
40
29
  requirements:
41
30
  - - ! '>='
@@ -43,10 +32,10 @@ dependencies:
43
32
  version: '0'
44
33
  type: :development
45
34
  prerelease: false
46
- version_requirements: *7737440
35
+ version_requirements: *23425020
47
36
  - !ruby/object:Gem::Dependency
48
37
  name: Bacon_Colored
49
- requirement: &7736560 !ruby/object:Gem::Requirement
38
+ requirement: &23424540 !ruby/object:Gem::Requirement
50
39
  none: false
51
40
  requirements:
52
41
  - - ! '>='
@@ -54,7 +43,7 @@ dependencies:
54
43
  version: '0'
55
44
  type: :development
56
45
  prerelease: false
57
- version_requirements: *7736560
46
+ version_requirements: *23424540
58
47
  description: ! "\n Various DSLs to clean, question (Ask), and validate (Demand)
59
48
  your objects,\n their classes (data types), and their properties.\n "
60
49
  email:
@@ -71,6 +60,7 @@ files:
71
60
  - README.rdoc
72
61
  - Rakefile
73
62
  - lib/Checked.rb
63
+ - lib/Checked/All.rb
74
64
  - lib/Checked/Arch.rb
75
65
  - lib/Checked/Ask/Arrays.rb
76
66
  - lib/Checked/Ask/Ask.rb