Checked 0.1.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/Checked.gemspec +2 -3
  2. data/lib/Checked.rb +21 -9
  3. data/lib/Checked/Ask/Arrays.rb +31 -0
  4. data/lib/Checked/Ask/Ask.rb +21 -0
  5. data/lib/Checked/Ask/Strings.rb +27 -0
  6. data/lib/Checked/Ask/Vars.rb +21 -0
  7. data/lib/Checked/Base/Arch.rb +20 -0
  8. data/lib/Checked/Base/Base.rb +110 -0
  9. data/lib/Checked/Base/DSL.rb +73 -0
  10. data/lib/Checked/Base/DSL_Obj.rb +35 -0
  11. data/lib/Checked/{Clean.rb → Clean/Clean.rb} +1 -4
  12. data/lib/Checked/Clean/Strings.rb +94 -0
  13. data/lib/Checked/Demand/Arrays.rb +78 -0
  14. data/lib/Checked/Demand/Bools.rb +28 -0
  15. data/lib/Checked/{Demand.rb → Demand/Demand.rb} +12 -11
  16. data/lib/Checked/Demand/File_Paths.rb +63 -0
  17. data/lib/Checked/Demand/Hashs.rb +29 -0
  18. data/lib/Checked/Demand/Strings.rb +66 -0
  19. data/lib/Checked/Demand/Symbols.rb +21 -0
  20. data/lib/Checked/Demand/Vars.rb +65 -0
  21. data/lib/Checked/version.rb +1 -1
  22. data/spec/main.rb +5 -1
  23. data/spec/tests/Ask.rb +126 -8
  24. data/spec/tests/Clean.rb +66 -8
  25. data/spec/tests/DSL.rb +31 -0
  26. data/spec/tests/Demand.rb +220 -29
  27. metadata +38 -32
  28. data/lib/Checked/Args.rb +0 -55
  29. data/lib/Checked/Ask.rb +0 -69
  30. data/lib/Checked/Ask/DSL.rb +0 -31
  31. data/lib/Checked/Ask/Mods/Arrays.rb +0 -25
  32. data/lib/Checked/Ask/Mods/Strings.rb +0 -26
  33. data/lib/Checked/Ask/Mods/Vars.rb +0 -12
  34. data/lib/Checked/Base.rb +0 -119
  35. data/lib/Checked/Clean/DSL.rb +0 -16
  36. data/lib/Checked/Clean/Mods/Strings.rb +0 -104
  37. data/lib/Checked/Demand/DSL.rb +0 -29
  38. data/lib/Checked/Demand/Mods/Arrays.rb +0 -72
  39. data/lib/Checked/Demand/Mods/Bools.rb +0 -37
  40. data/lib/Checked/Demand/Mods/File_Addresses.rb +0 -59
  41. data/lib/Checked/Demand/Mods/Strings.rb +0 -51
  42. data/lib/Checked/Demand/Mods/Symbols.rb +0 -20
  43. data/lib/Checked/Demand/Mods/Vars.rb +0 -91
  44. data/spec/tests/Ask_Strings.rb +0 -57
  45. data/spec/tests/Checked.rb +0 -43
  46. data/spec/tests/Clean_Strings.rb +0 -76
  47. data/spec/tests/Demand_Arrays.rb +0 -31
  48. data/spec/tests/Demand_File_Addresses.rb +0 -61
  49. data/spec/tests/Demand_Vars.rb +0 -19
data/Checked.gemspec CHANGED
@@ -21,9 +21,8 @@ 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
- # specify any dependencies here; for example:
25
- # s.add_development_dependency "rspec"
26
- # s.add_runtime_dependency "rest-client"
24
+ s.add_runtime_dependency "Uni_Arch"
25
+
27
26
  s.add_development_dependency 'rake'
28
27
  s.add_development_dependency 'bacon'
29
28
  s.add_development_dependency 'Bacon_Colored'
data/lib/Checked.rb CHANGED
@@ -1,14 +1,26 @@
1
1
  require "Checked/version"
2
+ require 'Uni_Arch'
2
3
 
3
- %w{ Args Base Ask Clean Demand }.each { |klass|
4
- require "Checked/#{klass}"
4
+ require "Checked/Base/DSL"
5
+ require "Checked/Base/DSL_Obj"
6
+ require "Checked/Base/Base"
7
+ require "Checked/Base/Arch"
8
+
9
+
10
+ %w{ Demand Ask Clean }.each { |klass|
11
+ require "Checked/#{klass}/#{klass}"
12
+
13
+ Dir.glob(File.join File.dirname(__FILE__), "Checked/#{klass}/*.rb").each { |path|
14
+
15
+ # Require the file.
16
+ path =~ %r!lib/Checked/(.+)/(.+)\.rb!
17
+ require( "Checked/#{$1}/#{$2}" ) if $1 && $2
18
+
19
+ # Set up routes.
20
+ if klass != $2
21
+ Checked::Arch.use Checked.const_get(:"#{klass}").const_get(:"#{$2}")
22
+ end
23
+ }
5
24
  }
6
25
 
7
26
 
8
- module Checked
9
- module DSL
10
- include ::Checked::Ask::DSL
11
- include ::Checked::Clean::DSL
12
- include ::Checked::Demand::DSL
13
- end # === module DSL
14
- end
@@ -0,0 +1,31 @@
1
+
2
+
3
+ module Checked
4
+ class Ask
5
+ class Arrays
6
+
7
+ include Ask::Base
8
+
9
+ namespace '/array!'
10
+
11
+ route
12
+ def symbols?
13
+ return false if target.empty?
14
+ target.all? { |val| val.is_a? Symbol }
15
+ end
16
+
17
+ route
18
+ def include?
19
+ target.include?(*args)
20
+ end
21
+
22
+ route
23
+ def exclude?
24
+ !target.include?(*args)
25
+ end
26
+
27
+ end # === class Arrays
28
+
29
+
30
+ end # === class Ask
31
+ end # === class Checked
@@ -0,0 +1,21 @@
1
+
2
+ module Checked
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
+
16
+ def initialize *args
17
+ raise "Not allowed to use this Class directly."
18
+ end
19
+
20
+ end # === class Ask
21
+ end # === module Checked
@@ -0,0 +1,27 @@
1
+
2
+ module Checked
3
+ class Ask
4
+ class Strings
5
+
6
+ include Ask::Base
7
+
8
+ namespace '/string!'
9
+
10
+ route
11
+ def empty?
12
+ target.strip.empty?
13
+ end
14
+
15
+ route
16
+ def include?
17
+ !!target[*args]
18
+ end
19
+
20
+ route
21
+ def exclude?
22
+ !target[*args]
23
+ end
24
+
25
+ end # === class Strings
26
+ end # === class Ask
27
+ end # === module Checked
@@ -0,0 +1,21 @@
1
+
2
+ module Checked
3
+ class Ask
4
+ class Vars
5
+
6
+ include Ask::Base
7
+
8
+ namespace '/var!'
9
+
10
+ route
11
+ def respond_to?
12
+ answ = args.map { |a|
13
+ target.respond_to? a
14
+ }.uniq == [true]
15
+
16
+ answ
17
+ end
18
+
19
+ end # === class Vars
20
+ end # === class Ask
21
+ end # === module Checked
@@ -0,0 +1,20 @@
1
+
2
+ module Checked
3
+ class Arch
4
+ include ::Checked::Base
5
+
6
+ before
7
+ def save_key
8
+ request.response.body= request.headers.check_target
9
+ end
10
+
11
+ after_method
12
+ def save_last_response
13
+ unless request.path[%r@!/\Z@]
14
+ request.response.body= request.response.last
15
+ end
16
+ end
17
+
18
+ end # === class Arch
19
+
20
+ end # === module Checked
@@ -0,0 +1,110 @@
1
+ module Checked
2
+
3
+ module Base
4
+
5
+ include Uni_Arch::Base
6
+
7
+ CHECK = begin
8
+ o = Object.new
9
+ o.extend Checked::DSL
10
+ o
11
+ end
12
+
13
+ def target_klass
14
+ klass = self.class.name.split('::').last.sub(%r!s/Z!, '')
15
+ if klass == 'Var'
16
+ klass
17
+ else
18
+ eval klass
19
+ end
20
+ end
21
+
22
+ def target
23
+ request.response.body
24
+ end
25
+
26
+ def original_target
27
+ request.headers.check_target
28
+ end
29
+
30
+ def target_name
31
+ if target.respond_to?(:english_name)
32
+ return target.english_name
33
+ end
34
+
35
+ if request.headers.has_key?(:check_name) && request.headers.check_name
36
+ return request.headers.check_name
37
+ end
38
+
39
+ target.class.name.gsub('_', ' ')
40
+ end
41
+
42
+ def args
43
+ request.headers.args
44
+ end
45
+
46
+ #
47
+ # ::Checked::Demand::Arrays => demand
48
+ # ::Checked::Clean::Arrays => clean
49
+ # ::Checked::Ask::Arrays => ask
50
+ #
51
+ def purpose
52
+ @purpose ||= begin
53
+ temp = self.class.name.split('::')[-2]
54
+ if temp
55
+ temp.downcase.sub(/er$/, '')
56
+ else
57
+ raise "Unknown purpose"
58
+ end
59
+ end
60
+ end
61
+
62
+ def fail! msg
63
+ raise Checked::Demand::Failed, "#{request.env.target.inspect} #{msg}"
64
+ end
65
+
66
+ def not_empty!
67
+ fail!('...can\'t be empty.') if target.empty?
68
+ end
69
+
70
+ def be! meth, *args
71
+ answer = target.send meth, *args
72
+ demand answer, :bool!
73
+ return true if answer
74
+ fail!("...failed #{meth} with #{args.inspect}")
75
+ end
76
+
77
+ def not_be! meth, *args
78
+ bool!
79
+ pass = target.send(meth, *args)
80
+ demand pass, :bool!
81
+ return true unless pass
82
+ fail!("...#{meth} should not be true with #{args.inspect}")
83
+ end
84
+
85
+ def array? val
86
+ respond_to_all?( val, :[], :pop )
87
+ end
88
+
89
+ def hash? val
90
+ respond_to_all?( val, :[], :keys, :values )
91
+ end
92
+
93
+ def respond_to_all? val, *meths
94
+ meths.map { |m|
95
+ val.respond_to? m
96
+ }.uniq == [true]
97
+ end
98
+
99
+ def matcher
100
+ request.headers.matcher
101
+ end
102
+
103
+ def strip_target
104
+ request.response.body= target.strip
105
+ end
106
+
107
+ end # === module Base
108
+
109
+ end # === module Checked
110
+
@@ -0,0 +1,73 @@
1
+
2
+ module Checked
3
+ module DSL
4
+
5
+ # ============ Demand ==============
6
+
7
+ %w{ String Array Hash }.each { |name|
8
+ eval %~
9
+ def #{name}!( *args )
10
+ #{name.downcase}!(*args).check!
11
+ end
12
+ ~
13
+ }
14
+
15
+ %w{ var array bool file_path string symbol hash }.each { |klass|
16
+ eval %~
17
+ def #{klass}! *args
18
+ raise "No block allowed here." if block_given?
19
+ check_it( '#{klass}!', *args )
20
+ end
21
+
22
+ def #{klass}? *args
23
+ raise "No block allowed here." if block_given?
24
+ check_it( 'ask', *args )
25
+ end
26
+
27
+ def #{klass} *args
28
+ raise "No block allowed here." if block_given?
29
+ check_it( 'clean', *args )
30
+ end
31
+ ~
32
+ }
33
+
34
+ alias_method :demand!, :var!
35
+ alias_method :ask?, :var?
36
+
37
+ # ============= Ask ================
38
+
39
+ def check_it namespace, *args
40
+ args.unshift(nil) if args.size == 1
41
+ ::Checked::DSL::Obj.new( namespace, *args )
42
+ end
43
+
44
+ def _main_class_ unk
45
+ case unk
46
+ when String
47
+ 'string'
48
+ when Hash
49
+ 'hash'
50
+ when Array
51
+ 'array'
52
+ when Symbol
53
+ 'symbol'
54
+ when TrueClass, FalseClass
55
+ 'bool'
56
+ else
57
+ raise ArgumentError, "Unknown class: #{unk.inspect}"
58
+ end
59
+ end
60
+
61
+ def any? target, *args
62
+ raise "No block allowed." if block_given?
63
+
64
+ args.map { |a|
65
+ send "#{klass}?", target, a
66
+ check_it( 'ask', _main_class_(target), nil, target).send( a ).request.response.body
67
+ }.compact == [true]
68
+ end
69
+
70
+ # ============ Clean ===============
71
+
72
+ end # === module DSL
73
+ end
@@ -0,0 +1,35 @@
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
@@ -1,13 +1,10 @@
1
- require "Checked/Base"
2
- require "Checked/Clean/DSL"
3
- require "Checked/Demand/DSL"
4
1
 
5
2
  module Checked
6
3
  class Clean
7
4
  module Base
8
5
 
9
6
  private # =============================
10
- include ::Checked::Demand::DSL
7
+ include ::Checked::DSL
11
8
 
12
9
  public # ==============================
13
10
  include ::Checked::Base
@@ -0,0 +1,94 @@
1
+
2
+ module Checked
3
+ class Clean
4
+ class Strings
5
+
6
+ include Clean::Base
7
+
8
+ namespace '/string!'
9
+
10
+ before_these_methods
11
+ def strip_string
12
+ target.strip
13
+ end
14
+
15
+ route
16
+ def untar
17
+ target
18
+ .sub(/\.tar\.gz$/, '')
19
+ .sub(/\.tar/, '')
20
+ end
21
+
22
+ route
23
+ def file_names
24
+ ( target.strip.split.select { |word| word[*args] } )
25
+ end
26
+
27
+ route
28
+ def file_names_by_ext
29
+ names = CHECK.string!(target).file_names(*args)
30
+ bases = names.map { |s|
31
+ s.sub(%r!#{ext}$!, '')
32
+ }
33
+
34
+ names.zip bases
35
+ end
36
+
37
+ route
38
+ def shell
39
+ target
40
+ .strip
41
+ .split("\n")
42
+ .map(&:strip)
43
+ .reject { |line| line.empty? }
44
+ .join(' && ')
45
+ end
46
+
47
+ route
48
+ def chop_ext
49
+ target.sub /\.[^\.]+$/, ''
50
+ end
51
+
52
+ route
53
+ def ruby_name
54
+ CHECK.string!( File.basename( target ) ).chop_rb
55
+ end
56
+
57
+ route
58
+ def chop_rb
59
+ target.sub %r!\.rb$!, ''
60
+ end
61
+
62
+ route
63
+ def chop_slash_r
64
+ target.gsub "\r", ''
65
+ end
66
+
67
+ route
68
+ def os_stardard
69
+ CHECK.string!(target).chop_slash_r.strip
70
+ end
71
+
72
+ route
73
+ def to_single
74
+ target.gsub( /s\Z/, '' )
75
+ end
76
+
77
+ route
78
+ def to_plural
79
+ target.to_single + 's'
80
+ end
81
+
82
+ route
83
+ def to_class_name
84
+ target.split('_').map(&:capitalize).join('_')
85
+ end
86
+
87
+ route
88
+ def to_camel_case
89
+ target.split('_').map(&:capitalize).join
90
+ end
91
+
92
+ end # === class Strings
93
+ end # === class Clean
94
+ end # === module Checked