striuct 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +35 -34
- data/.travis.yml +6 -6
- data/Gemfile +12 -12
- data/History.rdoc +279 -273
- data/LICENSE +21 -21
- data/Manifest.txt +91 -90
- data/README.ja.old.rdoc +297 -297
- data/README.md +128 -128
- data/Rakefile +11 -11
- data/benchmark/basics.rb +56 -56
- data/example/README.rb +44 -44
- data/example/example.old.rdoc +187 -187
- data/example/example1.rb +233 -233
- data/example/example2.rb +22 -22
- data/example/see_trace.rb +32 -32
- data/lib/striuct/classmethods/README.md +5 -5
- data/lib/striuct/classmethods/adjustment.rb +32 -32
- data/lib/striuct/classmethods/constructor.rb +63 -63
- data/lib/striuct/classmethods/default.rb +27 -27
- data/lib/striuct/classmethods/enum.rb +48 -48
- data/lib/striuct/classmethods/inner.rb +91 -91
- data/lib/striuct/classmethods/length.rb +14 -14
- data/lib/striuct/classmethods/macro.rb +144 -144
- data/lib/striuct/classmethods/named.rb +113 -113
- data/lib/striuct/classmethods/object.rb +54 -54
- data/lib/striuct/classmethods/prevent_conflicts.rb +89 -89
- data/lib/striuct/classmethods/requiremnets.rb +11 -11
- data/lib/striuct/classmethods/to_struct.rb +23 -25
- data/lib/striuct/classmethods/validation.rb +55 -55
- data/lib/striuct/classmethods.rb +1 -1
- data/lib/striuct/instancemethods/README.md +5 -5
- data/lib/striuct/instancemethods/assign.rb +30 -30
- data/lib/striuct/instancemethods/cast.rb +34 -34
- data/lib/striuct/instancemethods/compare.rb +30 -30
- data/lib/striuct/instancemethods/default.rb +13 -13
- data/lib/striuct/instancemethods/delegate_class_methods.rb +28 -28
- data/lib/striuct/instancemethods/enum.rb +103 -103
- data/lib/striuct/instancemethods/hashy.rb +121 -121
- data/lib/striuct/instancemethods/inner.rb +60 -60
- data/lib/striuct/instancemethods/keyvalidatable.rb +14 -14
- data/lib/striuct/instancemethods/lock.rb +61 -61
- data/lib/striuct/instancemethods/object.rb +52 -52
- data/lib/striuct/instancemethods/requirements.rb +14 -14
- data/lib/striuct/instancemethods/safety.rb +11 -11
- data/lib/striuct/instancemethods/subscript.rb +55 -55
- data/lib/striuct/instancemethods/validation.rb +29 -25
- data/lib/striuct/instancemethods/values.rb +57 -57
- data/lib/striuct/instancemethods.rb +1 -1
- data/lib/striuct/requirements.rb +5 -5
- data/lib/striuct/singleton_class.rb +66 -66
- data/lib/striuct/specificcontainer.rb +19 -19
- data/lib/striuct/structs.rb +7 -7
- data/lib/striuct/version.rb +5 -5
- data/lib/striuct.rb +13 -13
- data/striuct.gemspec +24 -24
- data/test/helper.rb +5 -5
- data/test/test_striuct-singleton_class-define.rb +19 -19
- data/test/test_striuct-subclass-class-cloning.rb +20 -20
- data/test/test_striuct-subclass-class-close.rb +36 -36
- data/test/test_striuct-subclass-class-constructor.rb +82 -82
- data/test/test_striuct-subclass-class-freeze.rb +36 -36
- data/test/test_striuct-subclass-class-inheritable.rb +57 -57
- data/test/test_striuct-subclass-class-macro.rb +13 -13
- data/test/test_striuct-subclass-class-safety_naming.rb +72 -72
- data/test/test_striuct-subclass-class-validation.rb +26 -26
- data/test/test_striuct-subclass-feature-alias_member.rb +53 -53
- data/test/test_striuct-subclass-feature-to_struct.rb +61 -25
- data/test/test_striuct-subclass-feature-validation_util.rb +57 -0
- data/test/test_striuct-subclass-instance-accessor.rb +137 -137
- data/test/test_striuct-subclass-instance-adjuster.rb +94 -94
- data/test/test_striuct-subclass-instance-assign.rb +30 -30
- data/test/test_striuct-subclass-instance-basic.rb +32 -32
- data/test/test_striuct-subclass-instance-cloning.rb +22 -22
- data/test/test_striuct-subclass-instance-compare.rb +51 -51
- data/test/test_striuct-subclass-instance-default_value.rb +128 -128
- data/test/test_striuct-subclass-instance-enum.rb +92 -92
- data/test/test_striuct-subclass-instance-freeze.rb +19 -19
- data/test/test_striuct-subclass-instance-hashlike.rb +153 -153
- data/test/test_striuct-subclass-instance-keyvalidatable.rb +24 -24
- data/test/test_striuct-subclass-instance-lock.rb +39 -39
- data/test/test_striuct-subclass-instance-to_s_family.rb +25 -25
- data/test/test_striuct-subclass-instance-validation_functional_condition.rb +50 -50
- data/test/test_striuct-subclass-instance-validation_inference.rb +50 -50
- data/test/test_striuct-subclass-instance-validation_specific_conditions.rb +247 -247
- data/test/test_striuct-subclass-instance-validation_with_getter.rb +33 -33
- data/test/test_striuct-subclass-instance_names.rb +18 -18
- data/test/test_striuct-version.rb +10 -10
- metadata +4 -2
data/example/example.old.rdoc
CHANGED
@@ -1,188 +1,188 @@
|
|
1
|
-
=== Struct+ "Safety"
|
2
|
-
|
3
|
-
==== "member" macro
|
4
|
-
|
5
|
-
* Use "member" and you get a accessor, it looks Struct with tester(validator)s.
|
6
|
-
class User < Striuct.new
|
7
|
-
member :id, Integer
|
8
|
-
member :age, (20..140)
|
9
|
-
member :name, OR(/\A\w+\z/, /\A\w+ \w+\z/)
|
10
|
-
end
|
11
|
-
|
12
|
-
# pass
|
13
|
-
user = User.new 128381, 20
|
14
|
-
user.age = 30
|
15
|
-
user[2] = 'foo bar'
|
16
|
-
|
17
|
-
# fail
|
18
|
-
user[:id] = 10.0
|
19
|
-
user.age = 19
|
20
|
-
user[2] = nil
|
21
|
-
|
22
|
-
* Use functional object and you get a tester on upstairs.
|
23
|
-
module Game
|
24
|
-
class Character
|
25
|
-
end
|
26
|
-
|
27
|
-
class DB < Striuct.new
|
28
|
-
member :monsters, ->list{(list - characters).empty?}
|
29
|
-
member :characters, GENERICS(Character)
|
30
|
-
end
|
31
|
-
|
32
|
-
monster = Character.new
|
33
|
-
db = DB.new
|
34
|
-
|
35
|
-
# fail
|
36
|
-
db.characters = [1, 2]
|
37
|
-
|
38
|
-
# pass
|
39
|
-
db.characters = [monster, Character.new]
|
40
|
-
|
41
|
-
# fail
|
42
|
-
db.monsters = [Character.new]
|
43
|
-
|
44
|
-
# pass
|
45
|
-
db.monsters = [monster]
|
46
|
-
end
|
47
|
-
|
48
|
-
* Use "inference" and all instance test under class of first passed object.
|
49
|
-
class FlexibleContainer < Striuct.new
|
50
|
-
member :anything, anything, inference: true
|
51
|
-
member :number, Numeric, inference: true
|
52
|
-
end
|
53
|
-
|
54
|
-
fc1, fc2 = FlexibleContainer.new, FlexibleContainer.new
|
55
|
-
# pass
|
56
|
-
fc1.anything = 'str'
|
57
|
-
|
58
|
-
# fail
|
59
|
-
fc1.anything = :sym
|
60
|
-
fc2.anything = :sym
|
61
|
-
|
62
|
-
# pass
|
63
|
-
fc2.anything = 'string too'
|
64
|
-
|
65
|
-
# fail
|
66
|
-
fc1.number = 'str'
|
67
|
-
|
68
|
-
# pass
|
69
|
-
fc1.number = 1.0
|
70
|
-
|
71
|
-
# fail
|
72
|
-
fc2.number = 1
|
73
|
-
|
74
|
-
==== Protect from risks in naming members
|
75
|
-
|
76
|
-
* Standard Struct dosen't check member-name.
|
77
|
-
NoGuard = Struct.new :object_id, :'? !'
|
78
|
-
noguard = NoGuard.new false
|
79
|
-
noguard.object_id #=> false
|
80
|
-
noguard.methods.include?(:'? !') #=> false(lost!!)
|
81
|
-
|
82
|
-
* Striuct provides safety levels for this. (default: :prevent)
|
83
|
-
class SafetyNaming < Striuct.new
|
84
|
-
begin
|
85
|
-
member :object_id
|
86
|
-
rescue
|
87
|
-
p $!
|
88
|
-
end
|
89
|
-
|
90
|
-
begin
|
91
|
-
member :'? !'
|
92
|
-
rescue
|
93
|
-
p $!
|
94
|
-
end
|
95
|
-
|
96
|
-
# set lower
|
97
|
-
protect_level :struct
|
98
|
-
|
99
|
-
member :object_id, :'? !'
|
100
|
-
end
|
101
|
-
|
102
|
-
=== Struct+ "Handy"
|
103
|
-
|
104
|
-
==== Flavor
|
105
|
-
|
106
|
-
* Block with member macro, it is called "flavor" at here.
|
107
|
-
Below cases for type cast.
|
108
|
-
class User2 < Striuct.new
|
109
|
-
member :age, Fixnum, &->v{Integer v}
|
110
|
-
member :name, Symbol, &->v{v.to_s.to_sym}
|
111
|
-
end
|
112
|
-
|
113
|
-
user2 = User2.new
|
114
|
-
user2.age = 9 #=> 9(Fixnum)
|
115
|
-
user2.age = 10.1 #=> 10(Fixnum)
|
116
|
-
user2.age = '10' #=> 10(Fixnum)
|
117
|
-
|
118
|
-
user2.name = 10 #=> :10(Symbol)
|
119
|
-
user2.name = Class #=> :Class(Symbol)
|
120
|
-
|
121
|
-
==== Default
|
122
|
-
|
123
|
-
* provides default value and can use block parameter(like Hash.new)
|
124
|
-
class User3 < Striuct.new
|
125
|
-
member :lank, Fixnum
|
126
|
-
default :lank, 3
|
127
|
-
member :name
|
128
|
-
end
|
129
|
-
|
130
|
-
user3 = User3.new
|
131
|
-
user3.lank #=> 3
|
132
|
-
|
133
|
-
* Standard Struct always define "nil is default". Realy?
|
134
|
-
user3.name #=> nil
|
135
|
-
user3.assign? :name #=> false
|
136
|
-
user3.name = nil
|
137
|
-
user3.assign? :name #=> true
|
138
|
-
|
139
|
-
==== Alias
|
140
|
-
|
141
|
-
* alias member name
|
142
|
-
class User3
|
143
|
-
alias_member :position, :lank
|
144
|
-
end
|
145
|
-
|
146
|
-
user3.lank.equal? user3.position #=> true
|
147
|
-
user3[:lank].equal? user3[:position] #=> true
|
148
|
-
user3[:position] = 4
|
149
|
-
user3.lank #=> 4
|
150
|
-
|
151
|
-
==== Inherit
|
152
|
-
|
153
|
-
* (writing...)
|
154
|
-
|
155
|
-
==== Lock
|
156
|
-
|
157
|
-
* (writing...)
|
158
|
-
|
159
|
-
==== New Constructors
|
160
|
-
|
161
|
-
* Subclass.define reject floating object.
|
162
|
-
* block parameter is new instance
|
163
|
-
* except if no finished to assign each member
|
164
|
-
* returning object is tested strict(optional)
|
165
|
-
* returning object is locked(optional)
|
166
|
-
user3 = User3.define do |r|
|
167
|
-
r.lank = 10
|
168
|
-
r.name = 'foo'
|
169
|
-
end
|
170
|
-
|
171
|
-
* Subclass.[](load_pairs) make from Hash and like Hash
|
172
|
-
user3 = User3[lank: 10, name: 'foo']
|
173
|
-
|
174
|
-
=== Almost interfaces are keeping Struct has.
|
175
|
-
|
176
|
-
Sth1 = Striuct.new do
|
177
|
-
def my_special_method
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
Sth1.new.respond_to?(:my_special_method) #=> true
|
182
|
-
|
183
|
-
=== HashLike
|
184
|
-
|
185
|
-
* some interfaces import from Hash
|
186
|
-
|
187
|
-
* easy cast to Hash
|
1
|
+
=== Struct+ "Safety"
|
2
|
+
|
3
|
+
==== "member" macro
|
4
|
+
|
5
|
+
* Use "member" and you get a accessor, it looks Struct with tester(validator)s.
|
6
|
+
class User < Striuct.new
|
7
|
+
member :id, Integer
|
8
|
+
member :age, (20..140)
|
9
|
+
member :name, OR(/\A\w+\z/, /\A\w+ \w+\z/)
|
10
|
+
end
|
11
|
+
|
12
|
+
# pass
|
13
|
+
user = User.new 128381, 20
|
14
|
+
user.age = 30
|
15
|
+
user[2] = 'foo bar'
|
16
|
+
|
17
|
+
# fail
|
18
|
+
user[:id] = 10.0
|
19
|
+
user.age = 19
|
20
|
+
user[2] = nil
|
21
|
+
|
22
|
+
* Use functional object and you get a tester on upstairs.
|
23
|
+
module Game
|
24
|
+
class Character
|
25
|
+
end
|
26
|
+
|
27
|
+
class DB < Striuct.new
|
28
|
+
member :monsters, ->list{(list - characters).empty?}
|
29
|
+
member :characters, GENERICS(Character)
|
30
|
+
end
|
31
|
+
|
32
|
+
monster = Character.new
|
33
|
+
db = DB.new
|
34
|
+
|
35
|
+
# fail
|
36
|
+
db.characters = [1, 2]
|
37
|
+
|
38
|
+
# pass
|
39
|
+
db.characters = [monster, Character.new]
|
40
|
+
|
41
|
+
# fail
|
42
|
+
db.monsters = [Character.new]
|
43
|
+
|
44
|
+
# pass
|
45
|
+
db.monsters = [monster]
|
46
|
+
end
|
47
|
+
|
48
|
+
* Use "inference" and all instance test under class of first passed object.
|
49
|
+
class FlexibleContainer < Striuct.new
|
50
|
+
member :anything, anything, inference: true
|
51
|
+
member :number, Numeric, inference: true
|
52
|
+
end
|
53
|
+
|
54
|
+
fc1, fc2 = FlexibleContainer.new, FlexibleContainer.new
|
55
|
+
# pass
|
56
|
+
fc1.anything = 'str'
|
57
|
+
|
58
|
+
# fail
|
59
|
+
fc1.anything = :sym
|
60
|
+
fc2.anything = :sym
|
61
|
+
|
62
|
+
# pass
|
63
|
+
fc2.anything = 'string too'
|
64
|
+
|
65
|
+
# fail
|
66
|
+
fc1.number = 'str'
|
67
|
+
|
68
|
+
# pass
|
69
|
+
fc1.number = 1.0
|
70
|
+
|
71
|
+
# fail
|
72
|
+
fc2.number = 1
|
73
|
+
|
74
|
+
==== Protect from risks in naming members
|
75
|
+
|
76
|
+
* Standard Struct dosen't check member-name.
|
77
|
+
NoGuard = Struct.new :object_id, :'? !'
|
78
|
+
noguard = NoGuard.new false
|
79
|
+
noguard.object_id #=> false
|
80
|
+
noguard.methods.include?(:'? !') #=> false(lost!!)
|
81
|
+
|
82
|
+
* Striuct provides safety levels for this. (default: :prevent)
|
83
|
+
class SafetyNaming < Striuct.new
|
84
|
+
begin
|
85
|
+
member :object_id
|
86
|
+
rescue
|
87
|
+
p $!
|
88
|
+
end
|
89
|
+
|
90
|
+
begin
|
91
|
+
member :'? !'
|
92
|
+
rescue
|
93
|
+
p $!
|
94
|
+
end
|
95
|
+
|
96
|
+
# set lower
|
97
|
+
protect_level :struct
|
98
|
+
|
99
|
+
member :object_id, :'? !'
|
100
|
+
end
|
101
|
+
|
102
|
+
=== Struct+ "Handy"
|
103
|
+
|
104
|
+
==== Flavor
|
105
|
+
|
106
|
+
* Block with member macro, it is called "flavor" at here.
|
107
|
+
Below cases for type cast.
|
108
|
+
class User2 < Striuct.new
|
109
|
+
member :age, Fixnum, &->v{Integer v}
|
110
|
+
member :name, Symbol, &->v{v.to_s.to_sym}
|
111
|
+
end
|
112
|
+
|
113
|
+
user2 = User2.new
|
114
|
+
user2.age = 9 #=> 9(Fixnum)
|
115
|
+
user2.age = 10.1 #=> 10(Fixnum)
|
116
|
+
user2.age = '10' #=> 10(Fixnum)
|
117
|
+
|
118
|
+
user2.name = 10 #=> :10(Symbol)
|
119
|
+
user2.name = Class #=> :Class(Symbol)
|
120
|
+
|
121
|
+
==== Default
|
122
|
+
|
123
|
+
* provides default value and can use block parameter(like Hash.new)
|
124
|
+
class User3 < Striuct.new
|
125
|
+
member :lank, Fixnum
|
126
|
+
default :lank, 3
|
127
|
+
member :name
|
128
|
+
end
|
129
|
+
|
130
|
+
user3 = User3.new
|
131
|
+
user3.lank #=> 3
|
132
|
+
|
133
|
+
* Standard Struct always define "nil is default". Realy?
|
134
|
+
user3.name #=> nil
|
135
|
+
user3.assign? :name #=> false
|
136
|
+
user3.name = nil
|
137
|
+
user3.assign? :name #=> true
|
138
|
+
|
139
|
+
==== Alias
|
140
|
+
|
141
|
+
* alias member name
|
142
|
+
class User3
|
143
|
+
alias_member :position, :lank
|
144
|
+
end
|
145
|
+
|
146
|
+
user3.lank.equal? user3.position #=> true
|
147
|
+
user3[:lank].equal? user3[:position] #=> true
|
148
|
+
user3[:position] = 4
|
149
|
+
user3.lank #=> 4
|
150
|
+
|
151
|
+
==== Inherit
|
152
|
+
|
153
|
+
* (writing...)
|
154
|
+
|
155
|
+
==== Lock
|
156
|
+
|
157
|
+
* (writing...)
|
158
|
+
|
159
|
+
==== New Constructors
|
160
|
+
|
161
|
+
* Subclass.define reject floating object.
|
162
|
+
* block parameter is new instance
|
163
|
+
* except if no finished to assign each member
|
164
|
+
* returning object is tested strict(optional)
|
165
|
+
* returning object is locked(optional)
|
166
|
+
user3 = User3.define do |r|
|
167
|
+
r.lank = 10
|
168
|
+
r.name = 'foo'
|
169
|
+
end
|
170
|
+
|
171
|
+
* Subclass.[](load_pairs) make from Hash and like Hash
|
172
|
+
user3 = User3[lank: 10, name: 'foo']
|
173
|
+
|
174
|
+
=== Almost interfaces are keeping Struct has.
|
175
|
+
|
176
|
+
Sth1 = Striuct.new do
|
177
|
+
def my_special_method
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
Sth1.new.respond_to?(:my_special_method) #=> true
|
182
|
+
|
183
|
+
=== HashLike
|
184
|
+
|
185
|
+
* some interfaces import from Hash
|
186
|
+
|
187
|
+
* easy cast to Hash
|
188
188
|
user3.to_h #=> {:lank=>3, :name=>nil}
|