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 CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
22
  s.require_paths = ["lib"]
23
23
 
24
- s.add_runtime_dependency "Uni_Arch"
24
+ s.add_runtime_dependency "Sin_Arch"
25
25
 
26
26
  s.add_development_dependency 'rake'
27
27
  s.add_development_dependency 'bacon'
@@ -0,0 +1,63 @@
1
+ class Checked
2
+
3
+ module Arch
4
+ include DSL::Ruby
5
+
6
+ def self.included klass
7
+ klass.send :include, Sin_Arch::Arch
8
+ end
9
+
10
+ def target_name
11
+ request.env['target_name']
12
+ end
13
+
14
+ def original_target
15
+ request.env['original_target']
16
+ end
17
+
18
+ def matcher
19
+ @matcher ||= begin
20
+ m = args_hash['args'].first
21
+ if !m
22
+ raise Sin_Arch::Missing_Argument, "Missing argument for matcher."
23
+ end
24
+ m
25
+ end
26
+ end
27
+
28
+ #
29
+ # ::Checked::Demand::Arrays => demand
30
+ # ::Checked::Clean::Arrays => clean
31
+ # ::Checked::Ask::Arrays => ask
32
+ #
33
+ def purpose
34
+ @purpose ||= begin
35
+ temp = self.class.name.split('::')[-2]
36
+ (temp && temp.downcase) || raise("Unknown purpose")
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def strip_target
43
+ return! return!.strip
44
+ end
45
+
46
+ def not_empty_args!
47
+ not_empty! args_hash['args']
48
+ end
49
+
50
+ def fail! raw_msg
51
+ msg = if raw_msg['...'] == 0
52
+ raw_msg.sub '...', target_name
53
+ else
54
+ raw_msg
55
+ end
56
+
57
+ super(msg)
58
+ end
59
+
60
+ end # === module Arch
61
+
62
+ end # === module Checked
63
+
@@ -1,28 +1,27 @@
1
1
 
2
2
 
3
- module Checked
3
+ class Checked
4
4
  class Ask
5
- class Arrays
5
+ class Arrays < Sinatra::Base
6
6
 
7
- include Uni_Arch::Base
8
- include Ask::Base
7
+ include Checked::Arch
9
8
 
10
- namespace '/array!'
9
+ map '/array!'
11
10
 
12
- route
11
+ get
13
12
  def symbols?
14
- return false if target.empty?
15
- target.all? { |val| val.is_a? Symbol }
13
+ return false if return!.empty?
14
+ return!.all? { |val| val.is_a? Symbol }
16
15
  end
17
16
 
18
- route
17
+ get
19
18
  def include?
20
- target.include?(*args)
19
+ return!.include?(*args_hash['args'])
21
20
  end
22
21
 
23
- route
22
+ get
24
23
  def exclude?
25
- !target.include?(*args)
24
+ !return!.include?(*args_hash['args'])
26
25
  end
27
26
 
28
27
  end # === class Arrays
@@ -1,21 +1,10 @@
1
1
 
2
- module Checked
2
+ class Checked
3
3
  class Ask
4
- module Base
5
-
6
- include ::Checked::Base
7
-
8
- def records
9
- @records ||= []
10
- end
11
-
12
- private # ==============================
13
-
14
- end # === module Base
15
4
 
16
5
  def initialize *args
17
6
  raise "Not allowed to use this Class directly."
18
7
  end
19
8
 
20
9
  end # === class Ask
21
- end # === module Checked
10
+ end # === class Checked
@@ -1,28 +1,12 @@
1
1
 
2
- module Checked
2
+ class Checked
3
3
  class Ask
4
- class Strings
4
+ class Strings < Sinatra::Base
5
5
 
6
- include Uni_Arch::Base
7
- include Ask::Base
6
+ include Checked::Arch
8
7
 
9
- namespace '/string!'
10
-
11
- route
12
- def empty?
13
- target.strip.empty?
14
- end
15
-
16
- route
17
- def include?
18
- !!target[*args]
19
- end
20
-
21
- route
22
- def exclude?
23
- !target[*args]
24
- end
8
+ map '/string!'
25
9
 
26
10
  end # === class Strings
27
11
  end # === class Ask
28
- end # === module Checked
12
+ end # === class Checked
@@ -1,22 +1,30 @@
1
1
 
2
- module Checked
2
+ class Checked
3
3
  class Ask
4
- class Vars
4
+ class Vars < Sinatra::Base
5
5
 
6
- include Uni_Arch::Base
7
- include Ask::Base
6
+ include Checked::Arch
8
7
 
9
- namespace '/var!'
8
+ map '/var!'
10
9
 
11
- route
12
- def respond_to?
13
- answ = args.map { |a|
14
- target.respond_to? a
15
- }.uniq == [true]
16
-
17
- answ
10
+ get
11
+ def respond_to_all?
12
+ a = not_empty_args!
13
+
14
+ a.all? { |a|
15
+ return!.respond_to? a
16
+ }
18
17
  end
19
18
 
19
+ get
20
+ def respond_to_any?
21
+ a = not_empty_args!
22
+
23
+ a.any? { |a|
24
+ return!.respond_to? a
25
+ }
26
+ end
27
+
20
28
  end # === class Vars
21
29
  end # === class Ask
22
- end # === module Checked
30
+ end # === class Checked
@@ -1,38 +1,10 @@
1
1
 
2
- module Checked
2
+ class Checked
3
3
  class Clean
4
- module Base
5
-
6
- private # =============================
7
- include ::Checked::DSL
8
-
9
- public # ==============================
10
- include ::Checked::Base
11
-
12
- def < meth, *args
13
- val = super
14
- named_demand!("Cleaned val", val, :not_nil!)
15
-
16
- self.target= super
17
- end
18
-
19
- def << *args
20
- args.flatten.each { |name|
21
- self.< name
22
- }
23
- end
24
-
25
- private # ===========================================
26
-
27
- def target= val
28
- named_demand! "Clean target", val, :not_nil!
29
-
30
- @target = val
31
- end
32
-
33
- end # === module Base
34
-
35
- include Base
36
4
 
5
+ def initialize *args
6
+ raise "Not allowed to use this Class directly."
7
+ end
8
+
37
9
  end # === class Clean
38
10
  end # === class Checked
@@ -1,33 +1,36 @@
1
1
 
2
- module Checked
2
+ class Checked
3
3
  class Clean
4
- class Strings
4
+ class Strings < Sinatra::Base
5
5
 
6
- include Uni_Arch::Base
7
- include Clean::Base
6
+ include Checked::Arch
8
7
 
9
- namespace '/string!'
8
+ map '/string!'
10
9
 
11
- before_these_methods
12
- def strip_string
13
- target.strip
10
+ before '/:name'
11
+ def strip_val
12
+ return!( return!.strip ) if strippable_route?
13
+ end
14
+
15
+ def strippable_route?
16
+ params['name'][%r![^\?\!]\Z!] && !%w{chop_slash_r}.include?(params['name'])
14
17
  end
15
18
 
16
- route
19
+ get
17
20
  def untar
18
- target
21
+ return!
19
22
  .sub(/\.tar\.gz$/, '')
20
23
  .sub(/\.tar/, '')
21
24
  end
22
25
 
23
- route
26
+ get
24
27
  def file_names
25
- ( target.strip.split.select { |word| word[*args] } )
28
+ ( return!.split.select { |word| word[*args] } )
26
29
  end
27
30
 
28
- route
31
+ get
29
32
  def file_names_by_ext
30
- names = CHECK.string!(target).file_names(*args)
33
+ names = CHECK.string!(return!).file_names(*args)
31
34
  bases = names.map { |s|
32
35
  s.sub(%r!#{ext}$!, '')
33
36
  }
@@ -35,61 +38,60 @@ module Checked
35
38
  names.zip bases
36
39
  end
37
40
 
38
- route
41
+ get
39
42
  def shell
40
- target
41
- .strip
43
+ return!
42
44
  .split("\n")
43
45
  .map(&:strip)
44
46
  .reject { |line| line.empty? }
45
47
  .join(' && ')
46
48
  end
47
49
 
48
- route
50
+ get
49
51
  def chop_ext
50
- target.sub /\.[^\.]+$/, ''
52
+ return!.sub /\.[^\.]+$/, ''
51
53
  end
52
54
 
53
- route
55
+ get
54
56
  def ruby_name
55
- CHECK.string!( File.basename( target ) ).chop_rb
57
+ File.basename chop_rb
56
58
  end
57
59
 
58
- route
60
+ get
59
61
  def chop_rb
60
- target.sub %r!\.rb$!, ''
62
+ return!.sub %r!\.rb$!, ''
61
63
  end
62
64
 
63
- route
65
+ get
64
66
  def chop_slash_r
65
- target.gsub "\r", ''
67
+ return!.gsub "\r", ''
66
68
  end
67
69
 
68
- route
70
+ get
69
71
  def os_stardard
70
- CHECK.string!(target).chop_slash_r.strip
72
+ chop_slash_r
71
73
  end
72
74
 
73
- route
75
+ get
74
76
  def to_single
75
- target.gsub( /s\Z/, '' )
77
+ return!.gsub( /s\Z/, '' )
76
78
  end
77
79
 
78
- route
80
+ get
79
81
  def to_plural
80
- target.to_single + 's'
82
+ return!.to_single + 's'
81
83
  end
82
84
 
83
- route
85
+ get
84
86
  def to_class_name
85
- target.split('_').map(&:capitalize).join('_')
87
+ return!.split('_').map(&:capitalize).join('_')
86
88
  end
87
89
 
88
- route
90
+ get
89
91
  def to_camel_case
90
- target.split('_').map(&:capitalize).join
92
+ return!.split('_').map(&:capitalize).join
91
93
  end
92
94
 
93
95
  end # === class Strings
94
96
  end # === class Clean
95
- end # === module Checked
97
+ end # === class Checked
@@ -0,0 +1,238 @@
1
+ class Object
2
+
3
+ def Checked
4
+ @sin_arch ||= begin
5
+ o = ::Checked::Obj.new
6
+ o.value = self
7
+ o
8
+ end
9
+ end
10
+
11
+ def Checked_applys?
12
+ (instance_variable_defined?(:@sin_arch) && Checked().on?)
13
+ end
14
+
15
+ def method_missing meth_name, *args
16
+ return(super) if !Checked_applys?
17
+
18
+ # Keep count after :Checked_applys? call
19
+ # because sometimes value is frozen.
20
+ @count ||= 1
21
+ @count += 1
22
+ return(super) if @count > 6
23
+
24
+ raise "Unknown block." if block_given?
25
+ begin
26
+ result = Checked().get!(meth_name, *args)
27
+ rescue Sin_Arch::Not_Found
28
+ return super
29
+ end
30
+ @count = 1
31
+
32
+ result.Checked.<< Checked()
33
+ result
34
+ end
35
+
36
+ end # === class Object
37
+
38
+ class Checked
39
+
40
+ class Obj
41
+
42
+ module Base
43
+
44
+ attr_accessor :map, :name, :value, :app
45
+
46
+ def initialize
47
+ off!
48
+ self.map = nil
49
+ self.name = nil
50
+ end
51
+
52
+ def << checked
53
+ on! checked.map
54
+ self.name = checked.name
55
+ end
56
+
57
+ def on?
58
+ @on
59
+ end
60
+
61
+ def on! new_map
62
+ raise ArgumentError, "Map value unacceptable: #{new_map.inspect}" unless new_map
63
+ self.map = new_map
64
+ @on = true
65
+ end
66
+
67
+ def off?
68
+ !@on
69
+ end
70
+
71
+ def off!
72
+ @on = false
73
+ end
74
+
75
+ def get! meth_name, *args
76
+ self.app = Checked::App.new
77
+ app.get!("/#{map}/#{meth_name}", 'name'=>name, 'value'=>value, 'args'=>args)
78
+ end
79
+
80
+ end # === module Base
81
+
82
+ include Base
83
+
84
+ end # === class Obj
85
+
86
+ end # === class Checked
87
+
88
+ class Checked
89
+ module DSL
90
+
91
+ module Ruby
92
+
93
+ def demand *args
94
+ case args.size
95
+ when 2
96
+ bool, raw_msg = args
97
+ msg = raw_msg.sub(%r!\A\.\.\.!, "#{target_name || return!.class}, #{return!.inspect}, ")
98
+ fail!(msg) unless bool
99
+ return!
100
+ when 3
101
+ val, bool, raw_msg = args
102
+ if respond_to?(:return!) && return! == val
103
+ return demand( bool, raw_msg )
104
+ end
105
+ msg = raw_msg.sub(%r!\A\.\.\.!, "#{val.class}, #{val.inspect}, ")
106
+ fail!(msg) unless bool
107
+ val
108
+ else
109
+ raise ArgumentError, "Too many arguments: #{args.inspect}"
110
+ end
111
+ end
112
+
113
+ def fail! msg
114
+ raise Checked::Demand::Failed, msg
115
+ end
116
+
117
+ def respond_to_all? val, *meths
118
+ meths.map { |m|
119
+ val.respond_to? m
120
+ }.uniq == [true]
121
+ end
122
+
123
+ def array? val
124
+ respond_to_all?( val, :[], :pop )
125
+ end
126
+
127
+ def hash? val
128
+ respond_to_all?( val, :[], :keys, :values )
129
+ end
130
+
131
+ def array! val
132
+ demand val, array?(val), '...is not an array.'
133
+ val
134
+ end
135
+
136
+ def hash! val
137
+ demand val, hash?(val), '...must be a hash.'
138
+ val
139
+ end
140
+
141
+ def string? val
142
+ val.is_a?(String)
143
+ end
144
+
145
+ def string! val
146
+ demand val, val.is_a?(String), '...must be a string.'
147
+ val
148
+ end
149
+
150
+ def bool! val
151
+ demand val, val.is_a?(TrueClass) || val.is_a?(FalseClass), '...must be either true (TrueClass) or false (FalseClass).'
152
+ val
153
+ end
154
+
155
+ def true! val
156
+ demand val, val === true, '...must be true (TrueClass).'
157
+ val
158
+ end
159
+
160
+ def false! val
161
+ demand val, val === false, '...must be false (FalseClass).'
162
+ val
163
+ end
164
+
165
+ def not_empty! val
166
+ demand val, !val.empty?, "...can't be empty."
167
+ val
168
+ end
169
+
170
+ def keys! h, *args
171
+ missing = args.select { |k| !h.has_key?(k) }
172
+ unless missing.empty?
173
+ raise Checked::Demand::Fail, "Missing keys: #{missing.inspect} in #{h}"
174
+ end
175
+ true
176
+ end
177
+
178
+ end # === module Ruby
179
+
180
+ module Racked
181
+
182
+ include Ruby
183
+
184
+ def self.eval! m
185
+ caller(1).first =~ %r!([^\:]+):(\d+):in `.!
186
+ if $1 && $2
187
+ eval m, nil, $1, $2.to_i - m.split("\n").size + 1
188
+ else
189
+ eval m, nil
190
+ end
191
+ end
192
+
193
+ %w{ Array Bool File_Path Hash String Symbol Var }.each { |name|
194
+ eval! %~
195
+ def #{name}!( *args )
196
+ Check!( '#{name.downcase}!', *args ).check!
197
+ end
198
+ ~
199
+ }
200
+
201
+ def Stripped! *args
202
+ v = String!(*args)
203
+ String!(v.strip)
204
+ end
205
+
206
+ def Check! ns, *name_and_or_val
207
+ name, val = case name_and_or_val.size
208
+ when 1
209
+ [ nil, name_and_or_val.first ]
210
+ when 2
211
+ name_and_or_val
212
+ else
213
+ raise ArgumentError, "Unknown values for name/value: #{name_and_or_val.inspect}"
214
+ end
215
+
216
+ val.Checked.on! ns
217
+ val.Checked.name = name
218
+ val.Checked.get! 'check!'
219
+ val
220
+ end
221
+
222
+ # ============= Ask ================
223
+
224
+ def any? target, *args
225
+ raise "No block allowed." if block_given?
226
+
227
+ args.map { |a|
228
+ send "#{klass}?", target, a
229
+ check_it( 'ask', _main_class_(target), nil, target).send( a ).request.response.body
230
+ }.compact == [true]
231
+ end
232
+
233
+
234
+ end # === module Rack_Arch
235
+
236
+ end # === module DSL
237
+
238
+ end # === class Checked