striuct 0.3.3 → 0.3.4
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.
- 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/example1.rb
CHANGED
@@ -1,234 +1,234 @@
|
|
1
|
-
#/usr/bin/ruby -w
|
2
|
-
|
3
|
-
require_relative '../lib/striuct'
|
4
|
-
|
5
|
-
def debug(message)
|
6
|
-
puts "line: #{caller.first.slice(/:(\w+)/, 1)}"
|
7
|
-
puts message.inspect, '-' * 80
|
8
|
-
end
|
9
|
-
|
10
|
-
|
11
|
-
# 1. Struct+ "Secure"
|
12
|
-
|
13
|
-
# macro "member" provides to use condtions
|
14
|
-
class User < Striuct.new
|
15
|
-
member :id, Integer
|
16
|
-
member :age, (20..140)
|
17
|
-
member :name, OR(/\A\w+\z/, /\A\w+ \w+\z/)
|
18
|
-
end
|
19
|
-
|
20
|
-
user = User.new 128381, 20
|
21
|
-
debug user
|
22
|
-
|
23
|
-
user.age = 30
|
24
|
-
user[2] = 'taro yamada'
|
25
|
-
debug user
|
26
|
-
|
27
|
-
# fail (Exception Striuct::ConditionError)
|
28
|
-
begin
|
29
|
-
user[:id] = 10.0
|
30
|
-
rescue
|
31
|
-
debug $!
|
32
|
-
end
|
33
|
-
|
34
|
-
begin
|
35
|
-
user.age = 19
|
36
|
-
rescue
|
37
|
-
debug $!
|
38
|
-
end
|
39
|
-
|
40
|
-
begin
|
41
|
-
user[2] = 'typo! name'
|
42
|
-
rescue
|
43
|
-
debug $!
|
44
|
-
end
|
45
|
-
|
46
|
-
# more detail checker do you need, use functional object
|
47
|
-
module Game
|
48
|
-
class Character
|
49
|
-
end
|
50
|
-
|
51
|
-
class DB < Striuct.new
|
52
|
-
member :monsters, ->list{(list - characters).empty?}
|
53
|
-
member :characters, GENERICS(Character)
|
54
|
-
end
|
55
|
-
|
56
|
-
monster = Character.new
|
57
|
-
db = DB.new
|
58
|
-
|
59
|
-
begin
|
60
|
-
db.characters = [1, 2]
|
61
|
-
rescue
|
62
|
-
debug $!
|
63
|
-
end
|
64
|
-
|
65
|
-
db.characters = [monster, Character.new]
|
66
|
-
debug db
|
67
|
-
|
68
|
-
begin
|
69
|
-
db.monsters = [Character.new]
|
70
|
-
rescue
|
71
|
-
debug $!
|
72
|
-
end
|
73
|
-
|
74
|
-
db.monsters = [monster]
|
75
|
-
debug db
|
76
|
-
end
|
77
|
-
|
78
|
-
# through "inference", and check under first passed object class
|
79
|
-
class FlexibleContainer < Striuct.new
|
80
|
-
member :anything, ANYTHING?, inference: true
|
81
|
-
member :number, Numeric, inference: true
|
82
|
-
end
|
83
|
-
|
84
|
-
fc1, fc2 = FlexibleContainer.new, FlexibleContainer.new
|
85
|
-
fc1.anything = 'str'
|
86
|
-
debug fc1
|
87
|
-
begin
|
88
|
-
fc1.anything = :sym
|
89
|
-
rescue
|
90
|
-
debug $!
|
91
|
-
end
|
92
|
-
|
93
|
-
begin
|
94
|
-
fc2.anything = :sym
|
95
|
-
rescue
|
96
|
-
debug $!
|
97
|
-
end
|
98
|
-
|
99
|
-
fc2.anything = 'string too'
|
100
|
-
|
101
|
-
debug fc2
|
102
|
-
|
103
|
-
begin
|
104
|
-
fc1.number = 'str'
|
105
|
-
rescue
|
106
|
-
debug $!
|
107
|
-
end
|
108
|
-
|
109
|
-
fc1.number = 1.0
|
110
|
-
debug fc1
|
111
|
-
|
112
|
-
begin
|
113
|
-
fc2.number = 1
|
114
|
-
rescue
|
115
|
-
debug $!
|
116
|
-
end
|
117
|
-
|
118
|
-
# Standard Struct not check member name.
|
119
|
-
NoGuard = Struct.new :__send__, :'? !'
|
120
|
-
noguard = NoGuard.new false
|
121
|
-
debug noguard.__send__
|
122
|
-
debug noguard.methods.include?(:'? !') # lost!!
|
123
|
-
|
124
|
-
# Striuct provides safety levels for naming.
|
125
|
-
class SafetyNaming < Striuct.new
|
126
|
-
begin
|
127
|
-
member :__send__
|
128
|
-
rescue
|
129
|
-
debug $!
|
130
|
-
end
|
131
|
-
|
132
|
-
begin
|
133
|
-
member :'? !'
|
134
|
-
rescue
|
135
|
-
debug $!
|
136
|
-
end
|
137
|
-
|
138
|
-
# set lower
|
139
|
-
protect_level :struct
|
140
|
-
|
141
|
-
member :__send__, :'? !'
|
142
|
-
end
|
143
|
-
|
144
|
-
# 2. Struct+ "Handy"
|
145
|
-
|
146
|
-
# to through block called "flavor"
|
147
|
-
# below case for type cast
|
148
|
-
class User2 < Striuct.new
|
149
|
-
member :age, OR(/\A\d+\z/, Numeric) do |arg|
|
150
|
-
Integer arg
|
151
|
-
end
|
152
|
-
|
153
|
-
member :name, ->v{v.respond_to? :to_s} do |v|
|
154
|
-
v.to_s.to_sym
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
user2 = User2.new
|
159
|
-
user2.age = 9
|
160
|
-
debug user2
|
161
|
-
|
162
|
-
user2.age = 10.1
|
163
|
-
debug user2
|
164
|
-
|
165
|
-
user2.age = '10'
|
166
|
-
debug user2
|
167
|
-
|
168
|
-
user2.name = 10
|
169
|
-
debug user2
|
170
|
-
user2.name = Class
|
171
|
-
debug user2
|
172
|
-
|
173
|
-
# Default value
|
174
|
-
|
175
|
-
class User3 < Striuct.new
|
176
|
-
member :lank, Fixnum
|
177
|
-
default :lank, 3
|
178
|
-
member :name
|
179
|
-
end
|
180
|
-
|
181
|
-
user3 = User3.new
|
182
|
-
user3
|
183
|
-
debug user3
|
184
|
-
|
185
|
-
# Standard Struct always define "nil is default". ...realy?
|
186
|
-
debug user3.assign?(:name)
|
187
|
-
user3.name = nil
|
188
|
-
debug user3.assign?(:name)
|
189
|
-
|
190
|
-
# Alias
|
191
|
-
|
192
|
-
class User3
|
193
|
-
alias_member :position, :lank
|
194
|
-
end
|
195
|
-
|
196
|
-
debug user3.lank.equal?(user3.position)
|
197
|
-
debug user3[:lank].equal?(user3[:position])
|
198
|
-
user3[:position] = 4
|
199
|
-
debug user3.lank
|
200
|
-
|
201
|
-
# New Constructors
|
202
|
-
|
203
|
-
# Subclass.define reject floating object
|
204
|
-
# * except if no finished assign each members
|
205
|
-
# * return object is frozen
|
206
|
-
user3 = User3.define do |r|
|
207
|
-
r.lank = 10
|
208
|
-
r.name = 'foo'
|
209
|
-
end
|
210
|
-
|
211
|
-
debug user3
|
212
|
-
|
213
|
-
# Subclass.load_pairs easy make from Hash and like Hash
|
214
|
-
user3 = User3[lank:10, name: 'foo']
|
215
|
-
|
216
|
-
debug user3
|
217
|
-
|
218
|
-
# 3. Keeping Struct's good interface
|
219
|
-
|
220
|
-
Sth1 = Striuct.new do
|
221
|
-
def my_special_method
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
225
|
-
debug Sth1.new.respond_to?(:my_special_method)
|
226
|
-
|
227
|
-
# to_struct
|
228
|
-
MyC = Striuct.new :a, :b
|
229
|
-
a = MyC.to_struct_class
|
230
|
-
p a
|
231
|
-
|
232
|
-
p MyC.new.to_struct
|
233
|
-
myc = Striuct.new :a, :b
|
1
|
+
#/usr/bin/ruby -w
|
2
|
+
|
3
|
+
require_relative '../lib/striuct'
|
4
|
+
|
5
|
+
def debug(message)
|
6
|
+
puts "line: #{caller.first.slice(/:(\w+)/, 1)}"
|
7
|
+
puts message.inspect, '-' * 80
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
# 1. Struct+ "Secure"
|
12
|
+
|
13
|
+
# macro "member" provides to use condtions
|
14
|
+
class User < Striuct.new
|
15
|
+
member :id, Integer
|
16
|
+
member :age, (20..140)
|
17
|
+
member :name, OR(/\A\w+\z/, /\A\w+ \w+\z/)
|
18
|
+
end
|
19
|
+
|
20
|
+
user = User.new 128381, 20
|
21
|
+
debug user
|
22
|
+
|
23
|
+
user.age = 30
|
24
|
+
user[2] = 'taro yamada'
|
25
|
+
debug user
|
26
|
+
|
27
|
+
# fail (Exception Striuct::ConditionError)
|
28
|
+
begin
|
29
|
+
user[:id] = 10.0
|
30
|
+
rescue
|
31
|
+
debug $!
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
user.age = 19
|
36
|
+
rescue
|
37
|
+
debug $!
|
38
|
+
end
|
39
|
+
|
40
|
+
begin
|
41
|
+
user[2] = 'typo! name'
|
42
|
+
rescue
|
43
|
+
debug $!
|
44
|
+
end
|
45
|
+
|
46
|
+
# more detail checker do you need, use functional object
|
47
|
+
module Game
|
48
|
+
class Character
|
49
|
+
end
|
50
|
+
|
51
|
+
class DB < Striuct.new
|
52
|
+
member :monsters, ->list{(list - characters).empty?}
|
53
|
+
member :characters, GENERICS(Character)
|
54
|
+
end
|
55
|
+
|
56
|
+
monster = Character.new
|
57
|
+
db = DB.new
|
58
|
+
|
59
|
+
begin
|
60
|
+
db.characters = [1, 2]
|
61
|
+
rescue
|
62
|
+
debug $!
|
63
|
+
end
|
64
|
+
|
65
|
+
db.characters = [monster, Character.new]
|
66
|
+
debug db
|
67
|
+
|
68
|
+
begin
|
69
|
+
db.monsters = [Character.new]
|
70
|
+
rescue
|
71
|
+
debug $!
|
72
|
+
end
|
73
|
+
|
74
|
+
db.monsters = [monster]
|
75
|
+
debug db
|
76
|
+
end
|
77
|
+
|
78
|
+
# through "inference", and check under first passed object class
|
79
|
+
class FlexibleContainer < Striuct.new
|
80
|
+
member :anything, ANYTHING?, inference: true
|
81
|
+
member :number, Numeric, inference: true
|
82
|
+
end
|
83
|
+
|
84
|
+
fc1, fc2 = FlexibleContainer.new, FlexibleContainer.new
|
85
|
+
fc1.anything = 'str'
|
86
|
+
debug fc1
|
87
|
+
begin
|
88
|
+
fc1.anything = :sym
|
89
|
+
rescue
|
90
|
+
debug $!
|
91
|
+
end
|
92
|
+
|
93
|
+
begin
|
94
|
+
fc2.anything = :sym
|
95
|
+
rescue
|
96
|
+
debug $!
|
97
|
+
end
|
98
|
+
|
99
|
+
fc2.anything = 'string too'
|
100
|
+
|
101
|
+
debug fc2
|
102
|
+
|
103
|
+
begin
|
104
|
+
fc1.number = 'str'
|
105
|
+
rescue
|
106
|
+
debug $!
|
107
|
+
end
|
108
|
+
|
109
|
+
fc1.number = 1.0
|
110
|
+
debug fc1
|
111
|
+
|
112
|
+
begin
|
113
|
+
fc2.number = 1
|
114
|
+
rescue
|
115
|
+
debug $!
|
116
|
+
end
|
117
|
+
|
118
|
+
# Standard Struct not check member name.
|
119
|
+
NoGuard = Struct.new :__send__, :'? !'
|
120
|
+
noguard = NoGuard.new false
|
121
|
+
debug noguard.__send__
|
122
|
+
debug noguard.methods.include?(:'? !') # lost!!
|
123
|
+
|
124
|
+
# Striuct provides safety levels for naming.
|
125
|
+
class SafetyNaming < Striuct.new
|
126
|
+
begin
|
127
|
+
member :__send__
|
128
|
+
rescue
|
129
|
+
debug $!
|
130
|
+
end
|
131
|
+
|
132
|
+
begin
|
133
|
+
member :'? !'
|
134
|
+
rescue
|
135
|
+
debug $!
|
136
|
+
end
|
137
|
+
|
138
|
+
# set lower
|
139
|
+
protect_level :struct
|
140
|
+
|
141
|
+
member :__send__, :'? !'
|
142
|
+
end
|
143
|
+
|
144
|
+
# 2. Struct+ "Handy"
|
145
|
+
|
146
|
+
# to through block called "flavor"
|
147
|
+
# below case for type cast
|
148
|
+
class User2 < Striuct.new
|
149
|
+
member :age, OR(/\A\d+\z/, Numeric) do |arg|
|
150
|
+
Integer arg
|
151
|
+
end
|
152
|
+
|
153
|
+
member :name, ->v{v.respond_to? :to_s} do |v|
|
154
|
+
v.to_s.to_sym
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
user2 = User2.new
|
159
|
+
user2.age = 9
|
160
|
+
debug user2
|
161
|
+
|
162
|
+
user2.age = 10.1
|
163
|
+
debug user2
|
164
|
+
|
165
|
+
user2.age = '10'
|
166
|
+
debug user2
|
167
|
+
|
168
|
+
user2.name = 10
|
169
|
+
debug user2
|
170
|
+
user2.name = Class
|
171
|
+
debug user2
|
172
|
+
|
173
|
+
# Default value
|
174
|
+
|
175
|
+
class User3 < Striuct.new
|
176
|
+
member :lank, Fixnum
|
177
|
+
default :lank, 3
|
178
|
+
member :name
|
179
|
+
end
|
180
|
+
|
181
|
+
user3 = User3.new
|
182
|
+
user3
|
183
|
+
debug user3
|
184
|
+
|
185
|
+
# Standard Struct always define "nil is default". ...realy?
|
186
|
+
debug user3.assign?(:name)
|
187
|
+
user3.name = nil
|
188
|
+
debug user3.assign?(:name)
|
189
|
+
|
190
|
+
# Alias
|
191
|
+
|
192
|
+
class User3
|
193
|
+
alias_member :position, :lank
|
194
|
+
end
|
195
|
+
|
196
|
+
debug user3.lank.equal?(user3.position)
|
197
|
+
debug user3[:lank].equal?(user3[:position])
|
198
|
+
user3[:position] = 4
|
199
|
+
debug user3.lank
|
200
|
+
|
201
|
+
# New Constructors
|
202
|
+
|
203
|
+
# Subclass.define reject floating object
|
204
|
+
# * except if no finished assign each members
|
205
|
+
# * return object is frozen
|
206
|
+
user3 = User3.define do |r|
|
207
|
+
r.lank = 10
|
208
|
+
r.name = 'foo'
|
209
|
+
end
|
210
|
+
|
211
|
+
debug user3
|
212
|
+
|
213
|
+
# Subclass.load_pairs easy make from Hash and like Hash
|
214
|
+
user3 = User3[lank:10, name: 'foo']
|
215
|
+
|
216
|
+
debug user3
|
217
|
+
|
218
|
+
# 3. Keeping Struct's good interface
|
219
|
+
|
220
|
+
Sth1 = Striuct.new do
|
221
|
+
def my_special_method
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
debug Sth1.new.respond_to?(:my_special_method)
|
226
|
+
|
227
|
+
# to_struct
|
228
|
+
MyC = Striuct.new :a, :b
|
229
|
+
a = MyC.to_struct_class
|
230
|
+
p a
|
231
|
+
|
232
|
+
p MyC.new.to_struct
|
233
|
+
myc = Striuct.new :a, :b
|
234
234
|
p myc.new.to_struct
|
data/example/example2.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
#!/usr/local/bin/ruby -w
|
2
|
-
|
3
|
-
$VERBOSE = true
|
4
|
-
|
5
|
-
require_relative '../lib/striuct'
|
6
|
-
|
7
|
-
User = Striuct.define do # other way "class User < Striuct.new"
|
8
|
-
member :firstname, /\w+/ # look like case syntax in Ruby
|
9
|
-
member :lank, AND(Fixnum, 1..10) # easy build multiple validation
|
10
|
-
default :lank, 3 # set a normal default value
|
11
|
-
member :registered, Time # look like type variable
|
12
|
-
default :registered, &->{Time.now} # evaluate with construction
|
13
|
-
p members #=> [firstname, lank, registered]
|
14
|
-
end
|
15
|
-
|
16
|
-
class MyUser < User
|
17
|
-
p members #=> [firstname, lank, registered]
|
18
|
-
member :foo # no validation, just an accessor
|
19
|
-
p members #=> [firstname, lank, registered, :foo]
|
20
|
-
end
|
21
|
-
|
22
|
-
user = MyUser.new
|
1
|
+
#!/usr/local/bin/ruby -w
|
2
|
+
|
3
|
+
$VERBOSE = true
|
4
|
+
|
5
|
+
require_relative '../lib/striuct'
|
6
|
+
|
7
|
+
User = Striuct.define do # other way "class User < Striuct.new"
|
8
|
+
member :firstname, /\w+/ # look like case syntax in Ruby
|
9
|
+
member :lank, AND(Fixnum, 1..10) # easy build multiple validation
|
10
|
+
default :lank, 3 # set a normal default value
|
11
|
+
member :registered, Time # look like type variable
|
12
|
+
default :registered, &->{Time.now} # evaluate with construction
|
13
|
+
p members #=> [firstname, lank, registered]
|
14
|
+
end
|
15
|
+
|
16
|
+
class MyUser < User
|
17
|
+
p members #=> [firstname, lank, registered]
|
18
|
+
member :foo # no validation, just an accessor
|
19
|
+
p members #=> [firstname, lank, registered, :foo]
|
20
|
+
end
|
21
|
+
|
22
|
+
user = MyUser.new
|
data/example/see_trace.rb
CHANGED
@@ -1,32 +1,32 @@
|
|
1
|
-
#/usr/bin/ruby -w
|
2
|
-
|
3
|
-
require_relative '../lib/striuct'
|
4
|
-
|
5
|
-
class User < Striuct.new
|
6
|
-
member :id, Integer
|
7
|
-
member :age, (20..140)
|
8
|
-
end
|
9
|
-
|
10
|
-
user = User.new
|
11
|
-
|
12
|
-
begin
|
13
|
-
user[:id] = 10.0
|
14
|
-
rescue
|
15
|
-
puts $!.backtrace
|
16
|
-
end
|
17
|
-
|
18
|
-
puts '-' * 80
|
19
|
-
|
20
|
-
$stdout.flush
|
21
|
-
|
22
|
-
begin
|
23
|
-
user.age = 19
|
24
|
-
rescue
|
25
|
-
puts $!.backtrace
|
26
|
-
end
|
27
|
-
|
28
|
-
puts '-' * 80
|
29
|
-
|
30
|
-
$stdout.flush
|
31
|
-
|
32
|
-
user.age = 19
|
1
|
+
#/usr/bin/ruby -w
|
2
|
+
|
3
|
+
require_relative '../lib/striuct'
|
4
|
+
|
5
|
+
class User < Striuct.new
|
6
|
+
member :id, Integer
|
7
|
+
member :age, (20..140)
|
8
|
+
end
|
9
|
+
|
10
|
+
user = User.new
|
11
|
+
|
12
|
+
begin
|
13
|
+
user[:id] = 10.0
|
14
|
+
rescue
|
15
|
+
puts $!.backtrace
|
16
|
+
end
|
17
|
+
|
18
|
+
puts '-' * 80
|
19
|
+
|
20
|
+
$stdout.flush
|
21
|
+
|
22
|
+
begin
|
23
|
+
user.age = 19
|
24
|
+
rescue
|
25
|
+
puts $!.backtrace
|
26
|
+
end
|
27
|
+
|
28
|
+
puts '-' * 80
|
29
|
+
|
30
|
+
$stdout.flush
|
31
|
+
|
32
|
+
user.age = 19
|
@@ -1,5 +1,5 @@
|
|
1
|
-
For developer
|
2
|
-
==============
|
3
|
-
|
4
|
-
* This dir's tree is diveded under roles/features.
|
5
|
-
Not under CONSTANT names.
|
1
|
+
For developer
|
2
|
+
==============
|
3
|
+
|
4
|
+
* This dir's tree is diveded under roles/features.
|
5
|
+
Not under CONSTANT names.
|
@@ -1,33 +1,33 @@
|
|
1
|
-
class Striuct; module ClassMethods
|
2
|
-
|
3
|
-
# @group Adjuster
|
4
|
-
|
5
|
-
# @param [Symbol, String] name
|
6
|
-
def has_adjuster?(name)
|
7
|
-
autonym = autonym_for name
|
8
|
-
|
9
|
-
! adjuster_for(autonym).nil?
|
10
|
-
end
|
11
|
-
|
12
|
-
alias_method :has_flavor?, :has_adjuster?
|
13
|
-
|
14
|
-
# @param [Symbol, String] name
|
15
|
-
def adjuster_for(name)
|
16
|
-
autonym = autonym_for name
|
17
|
-
|
18
|
-
_adjuster_for autonym
|
19
|
-
end
|
20
|
-
|
21
|
-
alias_method :flavor_for, :adjuster_for
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def _adjuster_for(autonym)
|
26
|
-
@adjusters[autonym]
|
27
|
-
end
|
28
|
-
|
29
|
-
alias_method :_flavor_for, :_adjuster_for
|
30
|
-
|
31
|
-
# @endgroup
|
32
|
-
|
1
|
+
class Striuct; module ClassMethods
|
2
|
+
|
3
|
+
# @group Adjuster
|
4
|
+
|
5
|
+
# @param [Symbol, String] name
|
6
|
+
def has_adjuster?(name)
|
7
|
+
autonym = autonym_for name
|
8
|
+
|
9
|
+
! adjuster_for(autonym).nil?
|
10
|
+
end
|
11
|
+
|
12
|
+
alias_method :has_flavor?, :has_adjuster?
|
13
|
+
|
14
|
+
# @param [Symbol, String] name
|
15
|
+
def adjuster_for(name)
|
16
|
+
autonym = autonym_for name
|
17
|
+
|
18
|
+
_adjuster_for autonym
|
19
|
+
end
|
20
|
+
|
21
|
+
alias_method :flavor_for, :adjuster_for
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def _adjuster_for(autonym)
|
26
|
+
@adjusters[autonym]
|
27
|
+
end
|
28
|
+
|
29
|
+
alias_method :_flavor_for, :_adjuster_for
|
30
|
+
|
31
|
+
# @endgroup
|
32
|
+
|
33
33
|
end; end
|