Checked 3.2.0 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/Checked/DSL.rb +29 -0
- data/lib/Checked/version.rb +1 -1
- data/spec/tests/DSL.rb +70 -0
- metadata +9 -9
data/lib/Checked/DSL.rb
CHANGED
@@ -114,6 +114,18 @@ class Checked
|
|
114
114
|
end
|
115
115
|
true
|
116
116
|
end
|
117
|
+
|
118
|
+
def get_or_set *args
|
119
|
+
Size! args, 1..2
|
120
|
+
name = args.shift
|
121
|
+
raise ArgumentError, "Instance variable #{name.inspect} has not been defined." unless instance_variable_defined?(name)
|
122
|
+
|
123
|
+
if args.empty?
|
124
|
+
instance_variable_get name
|
125
|
+
else
|
126
|
+
instance_variable_set name, args.first
|
127
|
+
end
|
128
|
+
end
|
117
129
|
|
118
130
|
end # === module Ruby
|
119
131
|
|
@@ -162,6 +174,23 @@ class Checked
|
|
162
174
|
n.Checked= v.Checked
|
163
175
|
n
|
164
176
|
end
|
177
|
+
|
178
|
+
def Size! arg, num_or_range
|
179
|
+
sizes = case num_or_range
|
180
|
+
when Fixnum
|
181
|
+
[num_or_range]
|
182
|
+
when Range
|
183
|
+
num_or_range.to_a
|
184
|
+
else
|
185
|
+
raise ArgumentError, "Wrong class for size: #{num_or_range.inspect}"
|
186
|
+
end
|
187
|
+
|
188
|
+
if not sizes.include?(arg.size)
|
189
|
+
raise ArgumentError, "Array, #{arg.inspect}, can only contain #{sizes.join ' or '} items."
|
190
|
+
end
|
191
|
+
|
192
|
+
arg
|
193
|
+
end
|
165
194
|
|
166
195
|
end # === module Rack_Arch
|
167
196
|
|
data/lib/Checked/version.rb
CHANGED
data/spec/tests/DSL.rb
CHANGED
@@ -35,6 +35,76 @@ describe "hash! " do
|
|
35
35
|
|
36
36
|
end # === describe array! check!
|
37
37
|
|
38
|
+
describe "Size!" do
|
39
|
+
|
40
|
+
behaves_like :racked_dsl
|
41
|
+
|
42
|
+
it 'raises ArgumentError if size of array is not within range' do
|
43
|
+
target = [:a, :b, :c]
|
44
|
+
r = 1..2
|
45
|
+
lambda { Size!(target, r) }
|
46
|
+
.should.raise(ArgumentError)
|
47
|
+
.message.should == "Array, #{target.inspect}, can only contain #{r.to_a.join ' or '} items."
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'raises ArgumentError if size is not a Fixnum or Range' do
|
51
|
+
lambda { Size!([], [0]) }
|
52
|
+
.should.raise(ArgumentError)
|
53
|
+
.message.should == "Wrong class for size: #{[0].inspect}"
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'raises ArgumentError if missing size' do
|
57
|
+
lambda { Size!([:a, :b, :c]) }
|
58
|
+
.should.raise(ArgumentError)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'returns array' do
|
62
|
+
t = [:a, :b]
|
63
|
+
Size!( t, 1..2).object_id.should == t.object_id
|
64
|
+
end
|
65
|
+
|
66
|
+
end # === describe Sizes_0_1!
|
67
|
+
|
68
|
+
describe "get_or_set :@var" do
|
69
|
+
|
70
|
+
behaves_like :racked_dsl
|
71
|
+
|
72
|
+
it 'raises ArgumentError if instance variable has not been initialized.' do
|
73
|
+
lambda { get_or_set :@something }
|
74
|
+
.should.raise(ArgumentError)
|
75
|
+
.message.should.match %r!@something has not been defined!
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'gets instance variable if array is empty.' do
|
79
|
+
@server = :hi
|
80
|
+
get_or_set( :@server ).should == @server
|
81
|
+
end
|
82
|
+
|
83
|
+
end # === describe get_or_set
|
84
|
+
|
85
|
+
describe "get_or_set :@var, var" do
|
86
|
+
|
87
|
+
behaves_like :racked_dsl
|
88
|
+
|
89
|
+
it 'raises ArgumentError if instance variable has not been initialized.' do
|
90
|
+
lambda { get_or_set :@someg, :hi}
|
91
|
+
.should.raise(ArgumentError)
|
92
|
+
.message.should.match %r!@someg has not been defined!
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'sets instance variable.' do
|
96
|
+
@server = :hi
|
97
|
+
get_or_set( :@server, :bye)
|
98
|
+
@server.should == :bye
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'returns last value' do
|
102
|
+
@server = :hi
|
103
|
+
get_or_set( :@server, :bye ).should == :bye
|
104
|
+
end
|
105
|
+
|
106
|
+
end # === describe get_or_set
|
107
|
+
|
38
108
|
describe "string! " do
|
39
109
|
|
40
110
|
behaves_like :ruby_dsl
|
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: 3.
|
4
|
+
version: 3.3.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-
|
12
|
+
date: 2012-01-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &15476740 !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: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *15476740
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bacon
|
27
|
-
requirement: &
|
27
|
+
requirement: &15412400 !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: *
|
35
|
+
version_requirements: *15412400
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: Bacon_Colored
|
38
|
-
requirement: &
|
38
|
+
requirement: &15410880 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *15410880
|
47
47
|
description: ! "\n Various DSLs to clean, question (Ask), and validate (Demand)
|
48
48
|
your objects,\n their classes (data types), and their properties.\n "
|
49
49
|
email:
|
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
104
|
version: '0'
|
105
105
|
requirements: []
|
106
106
|
rubyforge_project: Checked
|
107
|
-
rubygems_version: 1.8.
|
107
|
+
rubygems_version: 1.8.15
|
108
108
|
signing_key:
|
109
109
|
specification_version: 3
|
110
110
|
summary: Check your types and more.
|