Checked 1.2.3 → 2.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/spec/tests/Demand.rb CHANGED
@@ -2,24 +2,18 @@
2
2
  # ============================ ARRAYS
3
3
  # ============================
4
4
 
5
- describe "array! check!" do
6
-
7
- it 'must return array' do
8
- BOX.array!([:a, :b]).check!.should == [:a, :b]
9
- end
10
-
11
- end # === describe array! check!
5
+ describe "Array! :symbols!" do
12
6
 
13
- describe "array! :symbols!" do
7
+ behaves_like :racked_dsl
14
8
 
15
9
  it 'must require Array be non-empty.' do
16
10
  should.raise(Checked::Demand::Failed) {
17
- BOX.array!( [] ).symbols!
11
+ Array!( [] ).symbols!
18
12
  }.message.should.be == "Array, [], can't be empty."
19
13
  end
20
14
 
21
15
  it 'must positively validate an Array of single Symbol instance' do
22
- lambda { BOX.array!([:sym]).symbols! }.should.not.raise
16
+ lambda { Array!([:sym]).symbols! }.should.not.raise
23
17
  end
24
18
 
25
19
  end # === describe Demand for Arrays
@@ -28,43 +22,49 @@ end # === describe Demand for Arrays
28
22
  # ============================ BOOLS
29
23
  # ============================
30
24
 
31
- describe "demand :bool!" do
25
+ describe "Bool!" do
26
+
27
+ behaves_like :racked_dsl
32
28
 
33
29
  it 'must raise Demand::Failed if not a boolean' do
34
30
  should.raise(Checked::Demand::Failed) {
35
- BOX.bool!( "Answer", :false ).check!
31
+ Bool!( "Answer", :false ).check!
36
32
  }.message.should.be == "Answer, :false, must be either of TrueClass or FalseClass."
37
33
  end
38
34
 
39
35
  end # === describe demand :bool!
40
36
 
41
- describe "demand :bool! :true!" do
37
+ describe "Bool! :true!" do
38
+
39
+ behaves_like :racked_dsl
42
40
 
43
41
  it 'must pass validation if true' do
44
42
  should.not.raise(Checked::Demand::Failed) {
45
- BOX.bool!( "Answer", 1 === 1 ).true!
43
+ Bool!( "Answer", 1 === 1 ).true!
46
44
  }
47
45
  end
48
46
 
49
47
  it 'must raise Demand::Failed if not true' do
50
48
  should.raise(Checked::Demand::Failed) {
51
- BOX.bool!( "ANSW", false ).true!
49
+ Bool!( "ANSW", false ).true!
52
50
  }.message.should.be == "ANSW, false, must be true (TrueClass)."
53
51
  end
54
52
 
55
53
  end # === describe demand :bool!
56
54
 
57
55
  describe "demand :bool! :false!" do
56
+
57
+ behaves_like :racked_dsl
58
58
 
59
59
  it 'must pass validation if false' do
60
60
  should.not.raise(Checked::Demand::Failed) {
61
- BOX.bool!( "Comparison", 1 === 2 ).false!
61
+ Bool!( "Comparison", 1 === 2 ).false!
62
62
  }
63
63
  end
64
64
 
65
65
  it 'must raise Demand::Failed if not false' do
66
66
  should.raise(Checked::Demand::Failed) {
67
- BOX.bool!( "ANSW", 1 == 1 ).false!
67
+ Bool!( "ANSW", 1 == 1 ).false!
68
68
  }.message.should.be == "ANSW, true, must be false (FalseClass)."
69
69
  end
70
70
 
@@ -75,11 +75,13 @@ end # === describe demand :bool!
75
75
  # ============================
76
76
 
77
77
 
78
- describe "Demand file_path!" do
78
+ describe "File_Path!" do
79
+
80
+ behaves_like :racked_dsl
79
81
 
80
82
  it 'must fail if string has control characters' do
81
83
  lambda {
82
- BOX.file_path!("~/\tbashee").check!
84
+ File_Path!("~/\tbashee").check!
83
85
  }.should.raise(Checked::Demand::Failed)
84
86
  .message.should.match %r!has invalid characters: !
85
87
  end
@@ -87,30 +89,33 @@ describe "Demand file_path!" do
87
89
  end # === describe Demand file_path!
88
90
 
89
91
 
90
- describe "Demand file_path! not_dir!" do
92
+ describe "File_Path! not_dir!" do
93
+
94
+ behaves_like :racked_dsl
91
95
 
92
96
  it 'must fail for an existing dir' do
93
97
  lambda {
94
- BOX.file_path!("~/").not_dir!
98
+ File_Path!("~/").not_dir!
95
99
  }.should.raise(Checked::Demand::Failed)
96
100
  .message.should.match %r!, can't be an existing directory.!
97
101
  end
98
102
 
99
103
  end # === describe Demand not_dir!
100
104
 
101
- describe "Demand file_path! not_file!" do
105
+ describe "File_Path! not_file!" do
106
+
107
+ behaves_like :racked_dsl
102
108
 
103
- it 'must fail for an existing file' do
109
+ it 'fails for an existing file' do
104
110
  lambda {
105
- BOX.file_path!("~/.bashrc").not_file!
111
+ File_Path!("~/.bashrc").not_file!
106
112
  }.should.raise(Checked::Demand::Failed)
107
113
  .message.should.match %r!bashrc., can't be a file!
108
114
  end
109
115
 
110
- it 'must return expanded path' do
116
+ it 'does not expand path if file does not exist' do
111
117
  path = "~/,RANDOM"
112
- target = File.expand_path(path)
113
- BOX.file_path!(path).not_file!.should.be == target
118
+ File_Path!(path).not_file!.should.be == path
114
119
  end
115
120
 
116
121
  end # === describe Demand not_file!
@@ -121,17 +126,19 @@ end # === describe Demand not_file!
121
126
  # ============================
122
127
 
123
128
  describe "Demand hash! :symbol_keys!" do
129
+
130
+ behaves_like :racked_dsl
124
131
 
125
132
  it 'must raise Fail if keys are not all symbols' do
126
133
  lambda {
127
- BOX.hash!( :hello=>'by', 'hi'=>'hiya' ).symbol_keys!
134
+ Hash!( :hello=>'by', 'hi'=>'hiya' ).symbol_keys!
128
135
  }.should.raise(Checked::Demand::Failed)
129
136
  .message.should.match %r!must have all symbol keys!
130
137
  end
131
138
 
132
139
  it 'must not raise Fail if keys are all symbols' do
133
140
  lambda {
134
- BOX.hash!( :helo=>'be', :hi=>'hi' ).symbol_keys!
141
+ Hash!( :helo=>'be', :hi=>'hi' ).symbol_keys!
135
142
  }.should.not.raise(Checked::Demand::Failed)
136
143
  end
137
144
 
@@ -145,18 +152,22 @@ end # === describe Demand hash! :symbol_keys!
145
152
 
146
153
 
147
154
  describe "Demand string! :file_read!" do
155
+
156
+ behaves_like :racked_dsl
148
157
 
149
158
  it 'returns a string with right carriage returns erased.' do
150
- BOX.string!("test\r\ntest\r\n").file_content!.should == "test\ntest\n"
159
+ String!("test\r\ntest\r\n").file_content!.should == "test\ntest\n"
151
160
  end
152
161
 
153
162
  end # === describe Demand string! :file_read!
154
163
 
155
164
  describe "Demand string! :file_content!" do
165
+
166
+ behaves_like :racked_dsl
156
167
 
157
168
  it 'must fail for an empty string' do
158
169
  lambda {
159
- BOX.string!('').file_content!
170
+ String!('').file_content!
160
171
  }.should.raise(Checked::Demand::Failed)
161
172
  .message.should.be == "String, \"\", can't be empty."
162
173
  end
@@ -164,17 +175,19 @@ describe "Demand string! :file_content!" do
164
175
  end # === describe Demand :file_content!
165
176
 
166
177
  describe "Demand file_path! :hostname!" do
178
+
179
+ behaves_like :racked_dsl
167
180
 
168
181
  it 'must not contain whitespace' do
169
182
  lambda {
170
- BOX.string!('some name').hostname!
183
+ String!('some name').hostname!
171
184
  }.should.raise(Checked::Demand::Failed)
172
185
  .message.should.be == 'String, "some name", has invalid characters: " "'
173
186
  end
174
187
 
175
188
  it 'validates for a valid hostname' do
176
189
  lambda {
177
- BOX.string!('bdrm').hostname!
190
+ String!('bdrm').hostname!
178
191
  }.should.not.raise(Checked::Demand::Failed)
179
192
  end
180
193
 
@@ -185,26 +198,30 @@ end # === describe Demand :hostname!
185
198
  # ============================
186
199
 
187
200
  describe "Demand symbol! check!" do
201
+
202
+ behaves_like :racked_dsl
188
203
 
189
204
  it 'raises Demand::Failed if not Symbol' do
190
205
  lambda {
191
- BOX.symbol!('Name', []).check!
206
+ Symbol!('Name', []).check!
192
207
  }.should.raise(Checked::Demand::Failed)
193
- .message.should.be == "Name, [], must be a Symbol."
208
+ .message.should.be == "Name, [], must be a symbol."
194
209
  end
195
210
 
196
211
  end # === describe Demand symbol! check!
197
212
 
198
213
  describe "symbol! :in! array" do
214
+
215
+ behaves_like :racked_dsl
199
216
 
200
217
  it 'raises Demand::Failed if not in array' do
201
- lambda { BOX.symbol!(:a).in!([:b, :c]) }
218
+ lambda { Symbol!(:a).in!([:b, :c]) }
202
219
  .should.raise(Checked::Demand::Failed)
203
220
  .message.should.be == "Symbol, :a, must be in array: [:b, :c]"
204
221
  end
205
222
 
206
223
  it 'validates if symbols is in array.' do
207
- lambda { BOX.symbol!(:a).in!([:a, :b, :c]) }
224
+ lambda { Symbol!(:a).in!([:a, :b, :c]) }
208
225
  .should.not.raise(Checked::Demand::Failed)
209
226
  end
210
227
 
@@ -215,20 +232,24 @@ end # === describe symbol! :in! array
215
232
  # ============================
216
233
 
217
234
  describe "Named :demand!" do
235
+
236
+ behaves_like :racked_dsl
218
237
 
219
238
  it 'must use name in errors' do
220
239
  should.raise(Checked::Demand::Failed) {
221
- BOX.demand!("Test Val", :a ).nil!
240
+ Var!("Test Val", :a ).nil!
222
241
  }.message.should == "Test Val, :a, must be nil."
223
242
  end
224
243
 
225
244
  end # === describe Named :demand
226
245
 
227
246
  describe "DSL :demand!" do
247
+
248
+ behaves_like :racked_dsl
228
249
 
229
250
  it 'must raise error when demand fails.' do
230
251
  should.raise(Checked::Demand::Failed) {
231
- BOX.demand!( [] ).nil!
252
+ Var!( [] ).nil!
232
253
  }.message.should == "Array, [], must be nil."
233
254
 
234
255
  end
@@ -236,6 +257,8 @@ describe "DSL :demand!" do
236
257
  end # === describe :demand
237
258
 
238
259
  describe "demand be!" do
260
+
261
+ behaves_like :racked_dsl
239
262
 
240
263
  before do
241
264
  @fail = Checked::Demand::Failed
@@ -243,8 +266,8 @@ describe "demand be!" do
243
266
 
244
267
  it 'must fail if invalid class' do
245
268
  should.raise(@fail) {
246
- BOX.demand!([]).one_of!(Symbol)
247
- }.message.should == "Array, [], can only be of class/module: Symbol"
269
+ Var!([]).one_of!(Symbol)
270
+ }.message.should.match %r!Array, \[\], can only be of class/module: Symbol!i
248
271
  end
249
272
 
250
273
  end # === describe demand a! (certain class)
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: 1.2.3
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-10 00:00:00.000000000Z
12
+ date: 2012-01-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: Uni_Arch
16
- requirement: &17281880 !ruby/object:Gem::Requirement
15
+ name: Sin_Arch
16
+ requirement: &12698040 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *17281880
24
+ version_requirements: *12698040
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &17276960 !ruby/object:Gem::Requirement
27
+ requirement: &12696760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *17276960
35
+ version_requirements: *12696760
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bacon
38
- requirement: &17276380 !ruby/object:Gem::Requirement
38
+ requirement: &12695580 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *17276380
46
+ version_requirements: *12695580
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: Bacon_Colored
49
- requirement: &17275640 !ruby/object:Gem::Requirement
49
+ requirement: &12709280 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *17275640
57
+ version_requirements: *12709280
58
58
  description: ! "\n Various DSLs to clean, question (Ask), and validate (Demand)
59
59
  your objects,\n their classes (data types), and their properties.\n "
60
60
  email:
@@ -71,16 +71,14 @@ files:
71
71
  - README.rdoc
72
72
  - Rakefile
73
73
  - lib/Checked.rb
74
+ - lib/Checked/Arch.rb
74
75
  - lib/Checked/Ask/Arrays.rb
75
76
  - lib/Checked/Ask/Ask.rb
76
77
  - lib/Checked/Ask/Strings.rb
77
78
  - lib/Checked/Ask/Vars.rb
78
- - lib/Checked/Base/Arch.rb
79
- - lib/Checked/Base/Base.rb
80
- - lib/Checked/Base/DSL.rb
81
- - lib/Checked/Base/DSL_Obj.rb
82
79
  - lib/Checked/Clean/Clean.rb
83
80
  - lib/Checked/Clean/Strings.rb
81
+ - lib/Checked/DSL.rb
84
82
  - lib/Checked/Demand/Arrays.rb
85
83
  - lib/Checked/Demand/Bools.rb
86
84
  - lib/Checked/Demand/Demand.rb
@@ -1,33 +0,0 @@
1
-
2
- module Checked
3
- class Arch
4
-
5
- include Uni_Arch::Base
6
-
7
- before
8
- def save_key
9
- request.response.body= request.headers.check_target
10
- end
11
-
12
- before
13
- def run_check
14
- return if request.path['/var!/']
15
- return if request.path['/check!/']
16
- request.response.body= begin
17
- path = request.path.split('/')[0,2].join('/') + "/check!/"
18
- app = Checked::Arch.new( path, :check_name => request.headers.check_name, :check_target => request.response.body )
19
- app.fulfill_request
20
- app.request.response.body
21
- end
22
- end
23
-
24
- after_method
25
- def save_last_response
26
- unless request.path[%r@!/\Z@]
27
- request.response.body= request.response.last
28
- end
29
- end
30
-
31
- end # === class Arch
32
-
33
- end # === module Checked
@@ -1,108 +0,0 @@
1
- module Checked
2
-
3
- module Base
4
-
5
- CHECK = begin
6
- o = Object.new
7
- o.extend Checked::DSL
8
- o
9
- end
10
-
11
- def target_klass
12
- klass = self.class.name.split('::').last.sub(%r!s/Z!, '')
13
- if klass == 'Var'
14
- klass
15
- else
16
- eval klass
17
- end
18
- end
19
-
20
- def target
21
- request.response.body
22
- end
23
-
24
- def original_target
25
- request.headers.check_target
26
- end
27
-
28
- def target_name
29
- if target.respond_to?(:english_name)
30
- return target.english_name
31
- end
32
-
33
- if request.headers.has_key?(:check_name) && request.headers.check_name
34
- return request.headers.check_name
35
- end
36
-
37
- target.class.name.gsub('_', ' ')
38
- end
39
-
40
- def args
41
- request.headers.args
42
- end
43
-
44
- #
45
- # ::Checked::Demand::Arrays => demand
46
- # ::Checked::Clean::Arrays => clean
47
- # ::Checked::Ask::Arrays => ask
48
- #
49
- def purpose
50
- @purpose ||= begin
51
- temp = self.class.name.split('::')[-2]
52
- if temp
53
- temp.downcase.sub(/er$/, '')
54
- else
55
- raise "Unknown purpose"
56
- end
57
- end
58
- end
59
-
60
- def fail! msg
61
- raise Checked::Demand::Failed, "#{request.env.target.inspect} #{msg}"
62
- end
63
-
64
- def not_empty!
65
- fail!('...can\'t be empty.') if target.empty?
66
- end
67
-
68
- def be! meth, *args
69
- answer = target.send meth, *args
70
- demand answer, :bool!
71
- return true if answer
72
- fail!("...failed #{meth} with #{args.inspect}")
73
- end
74
-
75
- def not_be! meth, *args
76
- bool!
77
- pass = target.send(meth, *args)
78
- demand pass, :bool!
79
- return true unless pass
80
- fail!("...#{meth} should not be true with #{args.inspect}")
81
- end
82
-
83
- def array? val
84
- respond_to_all?( val, :[], :pop )
85
- end
86
-
87
- def hash? val
88
- respond_to_all?( val, :[], :keys, :values )
89
- end
90
-
91
- def respond_to_all? val, *meths
92
- meths.map { |m|
93
- val.respond_to? m
94
- }.uniq == [true]
95
- end
96
-
97
- def matcher
98
- request.headers.matcher
99
- end
100
-
101
- def strip_target
102
- request.response.body= target.strip
103
- end
104
-
105
- end # === module Base
106
-
107
- end # === module Checked
108
-
@@ -1,81 +0,0 @@
1
-
2
- module Checked
3
- module DSL
4
-
5
- # ============ Demand ==============
6
-
7
- %w{ String Array File_Path Hash Bool }.each { |name|
8
- eval %~
9
- def #{name}!( *args )
10
- #{name.downcase}!(*args).check!
11
- end
12
- ~
13
- }
14
-
15
- %w{ True False }.each { |bool|
16
- eval %~
17
- def #{bool}! *args
18
- bool!(*args).#{bool.downcase}!
19
- end
20
- ~
21
- }
22
-
23
- %w{ var array bool file_path string symbol hash }.each { |klass|
24
- eval %~
25
- def #{klass}! *args
26
- raise "No block allowed here." if block_given?
27
- check_it( '#{klass}!', *args )
28
- end
29
-
30
- def #{klass}? *args
31
- raise "No block allowed here." if block_given?
32
- check_it( 'ask', *args )
33
- end
34
-
35
- def #{klass} *args
36
- raise "No block allowed here." if block_given?
37
- check_it( 'clean', *args )
38
- end
39
- ~
40
- }
41
-
42
- alias_method :demand!, :var!
43
- alias_method :ask?, :var?
44
-
45
- # ============= Ask ================
46
-
47
- def check_it namespace, *args
48
- args.unshift(nil) if args.size == 1
49
- ::Checked::DSL::Obj.new( namespace, *args )
50
- end
51
-
52
- def _main_class_ unk
53
- case unk
54
- when String
55
- 'string'
56
- when Hash
57
- 'hash'
58
- when Array
59
- 'array'
60
- when Symbol
61
- 'symbol'
62
- when TrueClass, FalseClass
63
- 'bool'
64
- else
65
- raise ArgumentError, "Unknown class: #{unk.inspect}"
66
- end
67
- end
68
-
69
- def any? target, *args
70
- raise "No block allowed." if block_given?
71
-
72
- args.map { |a|
73
- send "#{klass}?", target, a
74
- check_it( 'ask', _main_class_(target), nil, target).send( a ).request.response.body
75
- }.compact == [true]
76
- end
77
-
78
- # ============ Clean ===============
79
-
80
- end # === module DSL
81
- end
@@ -1,35 +0,0 @@
1
-
2
- module Checked
3
-
4
- module DSL
5
-
6
- class Obj < BasicObject
7
-
8
- attr_reader :prefix, :headers
9
- def initialize prefix, name, val
10
- @prefix = prefix
11
- @headers = { 'check_name' => name, 'check_target' => val }
12
- @max_missing = 4
13
- @miss_count = 0
14
- end
15
-
16
- def method_missing name, *args, &blok
17
- if @miss_count >= @max_missing
18
- ::Kernel.raise "Infinite loop: #{name}, #{args}"
19
- end
20
-
21
- @miss_count = @miss_count + 1
22
- ::Kernel.raise "No block allowed." if blok
23
- headers['args'] = args
24
-
25
- path = '/' + [ @prefix, name ].map(&:to_s).join('/') + '/'
26
- app = ::Checked::Arch.new( path, headers )
27
- app.fulfill_request
28
- app.request.response.body
29
- end # === def method_missing
30
-
31
- end # === class Obj < BasicObject
32
-
33
- end # === module DSL
34
-
35
- end # === module Checked