Checked 0.1.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.
@@ -0,0 +1,37 @@
1
+
2
+ module Checked
3
+ class Demand
4
+ module Mods
5
+ module Bools
6
+
7
+ def self.before_apply d
8
+ end
9
+
10
+ def self.apply? d
11
+ target = d.target
12
+ target.is_a?( FalseClass ) ||
13
+ target.is_a?(TrueClass)
14
+ end
15
+
16
+ def self.after_apply d
17
+ end
18
+
19
+ def be! meth, *args
20
+ answer = target.send meth, *args
21
+ demand answer, :bool!
22
+ return true if answer
23
+ fail!("...failed #{meth} with #{args.inspect}")
24
+ end
25
+
26
+ def not_be! meth, *args
27
+ bool!
28
+ pass = target.send(meth, *args)
29
+ demand pass, :bool!
30
+ return true unless pass
31
+ fail!("...#{meth} should not be true with #{args.inspect}")
32
+ end
33
+
34
+ end # === module Bools
35
+ end # === module Mods
36
+ end # === class Demand
37
+ end # === module Checked
@@ -0,0 +1,56 @@
1
+
2
+ module Checked
3
+ class Demand
4
+ module Mods
5
+ module File_Addresses
6
+
7
+ def self.apply? d
8
+ d.target.is_a?(String)
9
+ end
10
+
11
+ def hostname!
12
+ string!
13
+ not_empty!
14
+ contain_only! %r![\dA-Za-z_-]!
15
+ end
16
+
17
+ def not_dir!
18
+ string!
19
+ not_empty!
20
+ if File.directory?(target)
21
+ fail! "...can't be a directory."
22
+ end
23
+ end
24
+
25
+ def not_file!
26
+ string!
27
+ not_empty!
28
+ if File.file?(target)
29
+ fail! "...can't be a file."
30
+ end
31
+ end
32
+
33
+ def dir_address!
34
+ file_address!
35
+ end
36
+
37
+ def file_address!
38
+ self.target= target.strip
39
+ string!
40
+ not_empty!
41
+ end
42
+
43
+ def file_read!
44
+ self.target= target.gsub("\r\n", "\n")
45
+ end
46
+
47
+ def file_content!
48
+ string!
49
+ not_empty!
50
+ file_read!
51
+ end
52
+
53
+ end # === module File_Addresses
54
+ end # === module Mods
55
+ end # === class Demand
56
+ end # === module Checked
@@ -0,0 +1,51 @@
1
+
2
+ module Checked
3
+ class Demand
4
+ module Mods
5
+ module Strings
6
+
7
+ def self.before_apply d
8
+ end
9
+
10
+ def self.apply? d
11
+ d.target.is_a?( String ) ||
12
+ d.target.is_a?(StringIO)
13
+ end
14
+
15
+ def self.after_apply d
16
+ if d.target.is_a?(StringIO)
17
+ d.target.rewind
18
+ d.target= d.target.readlines
19
+ d.target.rewind
20
+ end
21
+ end
22
+
23
+ def include! matcher
24
+ included = target[matcher]
25
+ return true if included
26
+ fail!("...must contain: #{matcher.inspect}")
27
+ end
28
+
29
+ def exclude! matcher
30
+ raise_e = val[matcher]
31
+ return true unless raise_e
32
+ fail!("...can't contain #{matcher.inspect}")
33
+ end
34
+
35
+ def matches_only! matcher
36
+ str = target.gsub(matcher, '')
37
+ if !str.empty?
38
+ fail!( "...invalid characters: #{str.inspect}" )
39
+ end
40
+ end
41
+
42
+ def not_empty!
43
+ if target.strip.empty?
44
+ fail!("...can't be empty.")
45
+ end
46
+ end
47
+
48
+ end # === module Strings
49
+ end # === module DSL
50
+ end # === class Demand
51
+ end # === module Checked
@@ -0,0 +1,20 @@
1
+
2
+ module Checked
3
+ class Demand
4
+ module Mods
5
+ module Symbols
6
+
7
+ def self.before_apply d
8
+ end
9
+
10
+ def self.apply? d
11
+ d.target.is_a? Symbol
12
+ end
13
+
14
+ def self.after_apply d
15
+ end
16
+
17
+ end # === module Symbols
18
+ end # === class DEMANDS
19
+ end # === class Demand
20
+ end # === module Checked
@@ -0,0 +1,91 @@
1
+
2
+ module Checked
3
+ class Demand
4
+ module Mods
5
+ module Vars
6
+
7
+ def array!
8
+ a! Array
9
+ end
10
+
11
+ def bool!
12
+ return true if [true, false].include?(target)
13
+ fail!("...has to be a boolean.")
14
+ end
15
+
16
+ def string!
17
+ a! String
18
+ end
19
+
20
+ def symbol!
21
+ a! Symbol
22
+ end
23
+
24
+ def no_block_given!
25
+ one_of! NilClass, FalseClass
26
+ if target
27
+ fail! "No block allowed."
28
+ end
29
+ end
30
+
31
+ def no_block!
32
+ no_block_given!
33
+ end
34
+
35
+ def either! *meths
36
+ meths.flatten.detect { |m|
37
+ begin
38
+ send m
39
+ true
40
+ rescue Failed
41
+ false
42
+ end
43
+ }
44
+ end
45
+
46
+ def one_of! *klasses
47
+ return true if klasses.flatten.any? { |k| target.is_a?(k) }
48
+ fail! "...can only be of class/module: #{klasses.map(&:to_s).join(', ')}"
49
+ end
50
+
51
+ def a! klass
52
+ one_of! klass
53
+ end
54
+
55
+ def nil!
56
+ return true if target == nil
57
+ fail!("...must be nil.")
58
+ end
59
+
60
+ def not_nil!
61
+ fail!("...can't be nil.") if target.nil?
62
+ true
63
+ end
64
+
65
+ def respond_to! meth
66
+ return true if target.respond_to?(meth)
67
+ fail!("...must respond to #{meth.inspect}")
68
+ end
69
+
70
+ def not_empty!
71
+ respond_to! :empty?
72
+ is_empty = target.empty?
73
+ fail!("...can't be empty.") if is_empty
74
+ end
75
+
76
+ def exists!
77
+ respond_to! :exists?
78
+ return true if target.exists?
79
+ fail!("...must exist.")
80
+ end
81
+
82
+ def not_exists!
83
+ respond_to! :exists?
84
+ return true unless target.exists?
85
+ fail!("...must not exist.")
86
+ end
87
+
88
+ end # === module Var
89
+ end # === module Mods
90
+ end # === class Demand
91
+ end # === module Checked
@@ -0,0 +1,46 @@
1
+ require "Checked/Base"
2
+ require "Checked/Demand/DSL"
3
+
4
+
5
+ module Checked
6
+
7
+ class Demand
8
+
9
+ Failed = Class.new(RuntimeError)
10
+
11
+ module Base
12
+
13
+ include DSL
14
+ include ::Checked::Base
15
+
16
+
17
+ def err_msg msg = "...is invalid."
18
+ message = if msg.strip[ %r!^\.\.\.! ]
19
+ msg.sub('...', '').strip
20
+ else
21
+ msg
22
+ end
23
+
24
+ @err_msg || "#{target_name} #{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
+
40
+ include Base
41
+
42
+ end # === class Demand
43
+
44
+ end # === module Checked
45
+
46
+
@@ -0,0 +1,3 @@
1
+ module Checked
2
+ VERSION = "0.1.0"
3
+ end
data/lib/Checked.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "Checked/version"
2
+
3
+ %w{ Args Base Ask Clean Demand }.each { |klass|
4
+ require "Checked/#{klass}"
5
+ }
6
+
7
+
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
data/spec/helper.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'bacon'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+
15
+ Bacon.summary_on_exit
data/spec/main.rb ADDED
@@ -0,0 +1,32 @@
1
+
2
+ require File.expand_path('spec/helper')
3
+ require "Bacon_Colored"
4
+ require 'Checked'
5
+
6
+
7
+ FOLDER = ("/tmp/Checked_Test")
8
+ %x! mkdir -p #{FOLDER}!
9
+ at_exit {
10
+ %x! rm -rf #{FOLDER} !
11
+ }
12
+
13
+ def ruby_e cmd
14
+ file = "#{FOLDER}/delete_me_perf_#{rand(100000)}.rb"
15
+ begin
16
+ loader = File.expand_path( File.dirname(__FILE__) + '/../lib' )
17
+ File.open(file, 'w') { |io|
18
+ io.write %~
19
+ $LOAD_PATH.unshift('#{loader}')
20
+ #{cmd}
21
+ ~
22
+ }
23
+ %x[ bundle exec ruby #{file} 2>&1].strip
24
+ ensure
25
+ File.delete file
26
+ end
27
+ end
28
+
29
+
30
+ Dir.glob('spec/tests/*.rb').each { |file|
31
+ require File.expand_path(file.sub('.rb', '')) if File.file?(file)
32
+ }
data/spec/tests/Ask.rb ADDED
@@ -0,0 +1,13 @@
1
+
2
+
3
+ describe "require 'Checked/Ask'" do
4
+
5
+ it 'must include DSL' do
6
+ ruby_e(%!
7
+ require 'Checked/Ask'
8
+ puts Checked::Ask::DSL.to_s
9
+ !)
10
+ .should.be == 'Checked::Ask::DSL'
11
+ end
12
+
13
+ end # === describe require 'Checked/Ask'
@@ -0,0 +1,57 @@
1
+
2
+ shared 'Ask' do
3
+ before {
4
+ extend Checked::Ask::DSL
5
+ }
6
+ end
7
+
8
+ describe "ask empty?" do
9
+
10
+ behaves_like 'Ask'
11
+
12
+ it "returns true if string is :empty? after applying :strip" do
13
+ ask?(" \n ", :empty?).should.be === true
14
+ end
15
+
16
+ it "returns false if string is not :empty? after applying :strip" do
17
+ ask?(" n ", :empty?).should.be === false
18
+ end
19
+
20
+
21
+ end # === describe Ask Strings
22
+
23
+ describe "Ask :includes" do
24
+
25
+ behaves_like 'Ask'
26
+
27
+ it "returns true if string contains a Regexp matcher" do
28
+ Checked::Ask.new(" :a ") { |a|
29
+ a.< :includes?, / :a /
30
+ }.true?.should.be == true
31
+ end
32
+
33
+ it "returns false if string excludes a Regexp matcher" do
34
+ Checked::Ask.new(" :a ") { |a|
35
+ a.< :includes?, / :b /
36
+ }.true?.should.be == false
37
+ end
38
+
39
+ end # === describe Ask :includes
40
+
41
+ describe "Ask :excludes" do
42
+
43
+ behaves_like 'Ask'
44
+
45
+ it "returns true if string excludes a Regexp matcher" do
46
+ Checked::Ask.new(" :a ") { |a|
47
+ a.< :excludes?, / :b /
48
+ }.true?.should.be == true
49
+ end
50
+
51
+ it 'returns false if string includes a Regexp matcher' do
52
+ Checked::Ask.new(" :a ") { |a|
53
+ a.< :excludes?, / :a /
54
+ }.true?.should.be == false
55
+ end
56
+
57
+ end # === describe Ask :excludes
@@ -0,0 +1,43 @@
1
+
2
+ describe "Checked::DSL" do
3
+
4
+ %w{ Ask Demand Clean }.each { |name|
5
+ klass = Checked.const_get(name)
6
+
7
+ it "includes #{name}::DSL" do
8
+ Checked::DSL.included_modules.should.include klass::DSL
9
+ end
10
+
11
+ }
12
+
13
+ end # === describe
14
+
15
+
16
+ describe "Checked.demand!" do
17
+
18
+ before {
19
+ @perf = Class.new { include Checked::DSL }.new
20
+ }
21
+
22
+ it 'must be equivalent to: Demand.new(target)' do
23
+ should.raise(Checked::Demand::Failed) {
24
+ @perf.demand! [], :not_empty!
25
+ }.message.should == "Array, [], can't be empty."
26
+
27
+ end
28
+
29
+ end # === describe Checked.demand!
30
+
31
+ describe "Checked.named_demand!" do
32
+
33
+ before {
34
+ @perf = Class.new { include Checked::DSL }.new
35
+ }
36
+
37
+ it 'must be equivalent to: Demand.new(target) { |d| d.* name; d << args}' do
38
+ should.raise(Checked::Demand::Failed) {
39
+ @perf.named_demand! "Test Val", [:a, 'c'], :symbols!
40
+ }.message.should == "Test Val, [:a, \"c\"], contains a non-symbol."
41
+ end
42
+
43
+ end # === describe Checked.named_demand!
@@ -0,0 +1,13 @@
1
+
2
+
3
+ describe "require 'Checked/Clean'" do
4
+
5
+ it 'must include DSL' do
6
+ ruby_e(%!
7
+ require 'Checked/Clean'
8
+ puts Checked::Clean::DSL.to_s
9
+ !)
10
+ .should.be == 'Checked::Clean::DSL'
11
+ end
12
+
13
+ end # === describe require 'Checked/Clean'
@@ -0,0 +1,76 @@
1
+
2
+ shared "Clean" do
3
+
4
+ extend Checked::Clean::DSL
5
+
6
+ end # === shared
7
+
8
+ describe "Clean :chop_ext" do
9
+
10
+ behaves_like 'Clean'
11
+
12
+ it "should chop off the extension of a file string: /etc/something.txt" do
13
+ clean("/etc/something.txt", :chop_ext).should == '/etc/something'
14
+ end
15
+
16
+ it "should chop off the extension of a file string: /etc/something.rb" do
17
+ clean("/etc/something.rb", :chop_rb).should == '/etc/something'
18
+ end
19
+
20
+ it "should not chop off a non-.rb extension for :chop_rb" do
21
+ clean("/etc/something.rbs", :chop_rb).should == '/etc/something.rbs'
22
+ end
23
+
24
+ it "should not chop off an extension if it has not" do
25
+ clean("/etc/something", :chop_rb).should == '/etc/something'
26
+ end
27
+
28
+ it "should not chop off an extension if it includes '.' in a dir: /etc/rc.d/x-something" do
29
+ clean("/etc/rc.d/x-something", :chop_rb).should == '/etc/rc.d/x-something'
30
+ end
31
+
32
+ end # === describe
33
+
34
+ describe "Clean :ruby_name" do
35
+
36
+ behaves_like 'Clean'
37
+
38
+ it 'should return the basename without .rb' do
39
+ clean("/dir/some.path/String.rb", :ruby_name).should.be == 'String'
40
+ end
41
+
42
+ it 'should be the equivalent to :chop_rb if it is just a filename without a dir' do
43
+ clean("String.rb", :ruby_name).should.be == 'String'
44
+ end
45
+
46
+ end # === describe :ruby_name
47
+
48
+ describe "Clean :chop_slash_r" do
49
+
50
+ behaves_like 'Clean'
51
+
52
+ it "should remove all instances of \\r" do
53
+ string = %@
54
+ Hi\r\n
55
+ Ok\r\n
56
+ @
57
+ clean(string, :chop_slash_r).should.be == string.gsub("\r", '')
58
+ end
59
+
60
+
61
+ end # === describe :chop_slash_r
62
+
63
+
64
+ describe "Clean :os_stardard" do
65
+
66
+ behaves_like 'Clean'
67
+
68
+ it "should remove all \\r and strip" do
69
+ string = %@
70
+ Hi\r\n
71
+ Ok\r\n
72
+ @
73
+ clean(string, :os_stardard).should.be == string.strip.gsub("\r", '')
74
+ end
75
+
76
+ end # === describe
@@ -0,0 +1,39 @@
1
+
2
+ describe "require 'Checked/Demand'" do
3
+
4
+ it 'must include DSL' do
5
+ ruby_e(%!
6
+ require 'Checked/Demand'
7
+ puts Checked::Demand::DSL.to_s
8
+ !)
9
+ .should.be == 'Checked::Demand::DSL'
10
+ end
11
+
12
+ end # === describe require 'Checked/Demand'
13
+
14
+ describe "Demand errors" do
15
+
16
+ it 'must recommend method if not found in current modules' do
17
+ lambda {
18
+ d = Checked::Demand.new('')
19
+ d.<< :symbols!
20
+ }.should.raise(NoMethodError)
21
+ .message.should.be === "String, \"\", can not demand symbols!, which is found in: Arrays"
22
+ end
23
+
24
+ it 'must raise a NoMethodError when a missing method is used within a valid demand! method.' do
25
+ Missing_Meth = Module.new do
26
+ def file_content!
27
+ something()
28
+ end
29
+ end
30
+
31
+ d = Checked::Demand.new('')
32
+ d.extend Missing_Meth
33
+ lambda { d.<< :file_content! }
34
+ .should.raise(NoMethodError)
35
+ .message.should.match %r!undefined method `something' for!
36
+
37
+ end
38
+
39
+ end # === describe Demand errors
@@ -0,0 +1,31 @@
1
+
2
+ describe "demand :symbols!" do
3
+
4
+ before {
5
+ @fail = Checked::Demand::Failed
6
+ @d = lambda { |val|
7
+ d=Checked::Demand.new(val)
8
+ d << :symbols!
9
+ d.target
10
+ }
11
+ }
12
+
13
+ it 'must require Array' do
14
+ m = should.raise(NoMethodError) {
15
+ @d.call :sym
16
+ }.message
17
+ m.should.include "Symbol, :sym, can not demand symbols!, which is found in"
18
+ m.should.include "in: Arrays"
19
+ end
20
+
21
+ it 'must require Array be non-empty.' do
22
+ should.raise(@fail) {
23
+ @d.call( [] )
24
+ }.message.should.be == "Array, [], can't be empty."
25
+ end
26
+
27
+ it 'must pass for Array of single Symbol instance' do
28
+ @d.call([:sym]).should.be == [:sym]
29
+ end
30
+
31
+ end # === describe Demand for Arrays
@@ -0,0 +1,37 @@
1
+
2
+ describe "Demand not_dir!" do
3
+
4
+ it 'must fail for an existing dir' do
5
+ lambda {
6
+ d = Checked::Demand.new(File.expand_path "~/")
7
+ d.<< :not_dir!
8
+ }.should.raise(Checked::Demand::Failed)
9
+ end
10
+
11
+ end # === describe Demand not_dir!
12
+
13
+
14
+
15
+ describe "Demand not_file!" do
16
+
17
+ it 'must fail for an existing file' do
18
+ lambda {
19
+ d = Checked::Demand.new(File.expand_path "~/.bashrc")
20
+ d.<< :not_file!
21
+ }.should.raise(Checked::Demand::Failed)
22
+ end
23
+
24
+ end # === describe Demand not_file!
25
+
26
+
27
+ describe "Demand :file_content!" do
28
+
29
+ it 'must fail for an empty string' do
30
+ lambda {
31
+ d = Checked::Demand.new('')
32
+ d.<< :file_content!
33
+ }.should.raise(Checked::Demand::Failed)
34
+ .message.should.be == "String, \"\", can't be empty."
35
+ end
36
+
37
+ end # === describe Demand :file_content!