striuct 0.1.7 → 0.2.3

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.
Files changed (36) hide show
  1. data/History.rdoc +43 -1
  2. data/LICENSE +22 -0
  3. data/Manifest.txt +21 -17
  4. data/{README.ja.rdoc → README.ja.old.rdoc} +11 -16
  5. data/README.rdoc +36 -250
  6. data/Rakefile +2 -2
  7. data/example/example.old.rdoc +188 -0
  8. data/example/example.rb +11 -219
  9. data/example/example1.rb +234 -0
  10. data/example/example2.rb +22 -0
  11. data/lib/striuct/abstract.rb +7 -7
  12. data/lib/striuct/conditions.rb +110 -69
  13. data/lib/striuct/{subclassable → containable}/basic.rb +9 -9
  14. data/lib/striuct/containable/classutil.rb +22 -0
  15. data/lib/striuct/{subclassable → containable}/eigen/basic.rb +9 -1
  16. data/lib/striuct/{subclassable → containable}/eigen/constructor.rb +1 -1
  17. data/lib/striuct/{subclassable → containable}/eigen/handy.rb +38 -5
  18. data/lib/striuct/{subclassable → containable}/eigen/inner.rb +22 -28
  19. data/lib/striuct/{subclassable → containable}/eigen/macro.rb +34 -25
  20. data/lib/striuct/{subclassable → containable}/eigen/safety.rb +15 -19
  21. data/lib/striuct/{subclassable/eigen/frame.rb → containable/eigen.rb} +10 -14
  22. data/lib/striuct/containable/handy.rb +91 -0
  23. data/lib/striuct/{subclassable → containable}/hashlike.rb +1 -1
  24. data/lib/striuct/{subclassable → containable}/inner.rb +23 -11
  25. data/lib/striuct/{subclassable → containable}/safety.rb +24 -5
  26. data/lib/striuct/{subclassable → containable}/yaml.rb +1 -1
  27. data/lib/striuct/containable.rb +39 -0
  28. data/lib/striuct/flavors.rb +83 -0
  29. data/lib/striuct/version.rb +2 -2
  30. data/lib/striuct.rb +13 -2
  31. data/test/test_striuct.rb +194 -17
  32. metadata +43 -35
  33. data/lib/striuct/frame.rb +0 -8
  34. data/lib/striuct/subclassable/classutil.rb +0 -26
  35. data/lib/striuct/subclassable/frame.rb +0 -33
  36. data/lib/striuct/subclassable/handy.rb +0 -57
data/History.rdoc CHANGED
@@ -1,10 +1,52 @@
1
+ === 0.2.3 2012-4-2
2
+
3
+ * fix:
4
+ * Conditions.#NAND
5
+ * Conditions.#XOR
6
+ * Conditions.#XNOR
7
+
8
+ === 0.2.2 2012-3-30
9
+
10
+ * add:
11
+ * aliases Conditions#[FUNC()] -> Conditions#[FUNC?()]
12
+
13
+ * fix:
14
+ * GENERICS check when build condition
15
+ * arity checker when default with block parameter
16
+
17
+ === 0.2.1 2012-1-30
18
+
19
+ * delete:
20
+ * Conditions#ANY, ALL
21
+
22
+ * add:
23
+ * can choose timing of validation (member with option and safety_setter? and more)
24
+ * add Flavors (Useful flavor builders)
25
+ * .#to_struct_class, #to_struct
26
+ * #each_name_with_index (alias-> #each_member_with_index, #each_key_with_index)
27
+
28
+ === 0.2.0 2012-1-27
29
+
30
+ * delete:
31
+ * constant StrictStruct
32
+ * Eigen.#sufficient? (alias -> valid?, accept?)
33
+
34
+ * modify:
35
+ * conditions to a condition (use condition generator Conditions#OR)
36
+ * to expr a flavor, before check condition
37
+ * inference flag under option parameter
38
+ * long macro names (define_xxx -> add_xxx)
39
+
40
+ * add:
41
+ * #each_pair_with_index
42
+
1
43
  === 0.1.7 2012-1-26
2
44
 
3
45
  * add:
4
46
  * default with Proc
5
47
  * detail error message in Subclass.#[]=
6
48
 
7
- * fix
49
+ * fix:
8
50
  * class duplicating
9
51
 
10
52
  === 0.1.6 2012-1-25
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2011 Kenichi Kamiya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt CHANGED
@@ -1,30 +1,34 @@
1
1
  History.rdoc
2
2
  Manifest.txt
3
3
  README.rdoc
4
- README.ja.rdoc
4
+ README.ja.old.rdoc
5
5
  Rakefile
6
+ LICENSE
6
7
  lib/striuct.rb
7
- lib/striuct/frame.rb
8
8
  lib/striuct/version.rb
9
9
  lib/striuct/abstract.rb
10
10
  lib/striuct/conditions.rb
11
- lib/striuct/subclassable/frame.rb
12
- lib/striuct/subclassable/classutil.rb
13
- lib/striuct/subclassable/basic.rb
14
- lib/striuct/subclassable/safety.rb
15
- lib/striuct/subclassable/handy.rb
16
- lib/striuct/subclassable/hashlike.rb
17
- lib/striuct/subclassable/yaml.rb
18
- lib/striuct/subclassable/inner.rb
19
- lib/striuct/subclassable/eigen/frame.rb
20
- lib/striuct/subclassable/eigen/basic.rb
21
- lib/striuct/subclassable/eigen/constructor.rb
22
- lib/striuct/subclassable/eigen/safety.rb
23
- lib/striuct/subclassable/eigen/handy.rb
24
- lib/striuct/subclassable/eigen/macro.rb
25
- lib/striuct/subclassable/eigen/inner.rb
11
+ lib/striuct/flavors.rb
12
+ lib/striuct/containable.rb
13
+ lib/striuct/containable/classutil.rb
14
+ lib/striuct/containable/basic.rb
15
+ lib/striuct/containable/safety.rb
16
+ lib/striuct/containable/handy.rb
17
+ lib/striuct/containable/hashlike.rb
18
+ lib/striuct/containable/yaml.rb
19
+ lib/striuct/containable/inner.rb
20
+ lib/striuct/containable/eigen.rb
21
+ lib/striuct/containable/eigen/basic.rb
22
+ lib/striuct/containable/eigen/constructor.rb
23
+ lib/striuct/containable/eigen/safety.rb
24
+ lib/striuct/containable/eigen/handy.rb
25
+ lib/striuct/containable/eigen/macro.rb
26
+ lib/striuct/containable/eigen/inner.rb
26
27
  test/test_helper.rb
27
28
  test/test_striuct.rb
28
29
  example/example.rb
30
+ example/example1.rb
31
+ example/example2.rb
32
+ example/example.old.rdoc
29
33
  example/benchmarks.rb
30
34
  example/see_trace.rb
@@ -6,7 +6,7 @@
6
6
 
7
7
  == DESCRIPTION
8
8
 
9
- Struct以上の手軽さで扱える、Structより堅いコンテナを目指しています。
9
+ Structっぽい手触りで扱える、多機能なStructを目指しています。
10
10
 
11
11
  メンバ名にチェックがかかる&一つの新しいクラスとして扱えるStructは、場面によってはHashよりも好ましい事があるかと思います。
12
12
  でもそうやってStructを使うぐらいなわけだから、値の方にもある程度のチェックをかけたいと思う事が多くありませんか?
@@ -33,7 +33,7 @@ RubyのStructを愛しつつも、こういった部分をなんとかしたい
33
33
  * 構造体としてのメンバ追加時、Rubyの既存メソッドと干渉しないかを指定レベル別にチェック出来ます。
34
34
  * 違和感のない継承利用
35
35
  * なるべくStructの心地よい操作感を残したまま機能追加をしています。
36
- * 既存クラスやメソッドの上書き等を(勝手には)しない、控えめな名前空間
36
+ * 既存クラスやメソッドの上書き等をしない、控えめな名前空間
37
37
  * Pure Ruby
38
38
 
39
39
  == SYNOPSIS
@@ -41,9 +41,8 @@ RubyのStructを愛しつつも、こういった部分をなんとかしたい
41
41
  * setup
42
42
  require 'striuct'
43
43
 
44
- * 2つの名前空間をお借りしますが、どっちも同じクラスを指します
44
+ * この名前空間以外は汚しません。
45
45
  Striuct
46
- StrictStruct
47
46
 
48
47
  === Struct+ "Safety"
49
48
 
@@ -98,11 +97,11 @@ RubyのStructを愛しつつも、こういった部分をなんとかしたい
98
97
  db.monsters = [monster]
99
98
  end
100
99
 
101
- * 構造体クラスのコンテキスト内だけで使える "inference" というメソッドの返り値を渡すと、
102
- 最初に参照したオブジェクトのクラスが、以降全インスタンスでの参照条件となります。
100
+ * オプションパラメータにinferenceというキーワードをtrueで渡すと、
101
+ 最初に参照したオブジェクトのクラスが以降全インスタンスでの参照条件となります。
103
102
  class FlexibleContainer < Striuct.new
104
- member :anything, inference
105
- member :number, Numeric, inference
103
+ member :anything, anything, inference: true
104
+ member :number, Numeric, inference: true
106
105
  end
107
106
 
108
107
  fc1, fc2 = FlexibleContainer.new, FlexibleContainer.new
@@ -171,15 +170,11 @@ RubyのStructを愛しつつも、こういった部分をなんとかしたい
171
170
 
172
171
  * memberマクロへブロックを渡しておくと、チェック後一処理加えた値を参照可能です。
173
172
  class User2 < Striuct.new
174
- # 数字っぽければIntegerへ変換してから
175
- member :age, /\A\d+\z/, Numeric do |v|
176
- Integer v
177
- end
173
+ # 必ずInteger経由でキャスト
174
+ member :age, Fixnum, &->v{Integer v}
178
175
 
179
176
  # Symbolに変換した上で代入。
180
- member :name, ->v{v.respond_to? :to_s} do |v|
181
- v.to_s.to_sym
182
- end
177
+ member :name, Symbol, &->v{v.to_s.to_sym}
183
178
  end
184
179
 
185
180
  user2 = User2.new
@@ -193,6 +188,7 @@ RubyのStructを愛しつつも、こういった部分をなんとかしたい
193
188
  ==== Default
194
189
 
195
190
  * defaultマクロを用いることで、メンバ毎に標準値を定義することが出来ます。
191
+ ブロックを渡すと、初期化時にブロックの中身を評価して設定します。(Hash.newっぽく)
196
192
  class User3 < Striuct.new
197
193
  member :lank, Fixnum
198
194
  default :lank, 3
@@ -265,7 +261,6 @@ RubyのStructを愛しつつも、こういった部分をなんとかしたい
265
261
  == Note
266
262
 
267
263
  * 現状、このReadme含めてドキュメントは殆ど更新出来ていません。
268
- 0.2.0 までにはなんとかしたい
269
264
 
270
265
  == REQUIREMENTS
271
266
 
data/README.rdoc CHANGED
@@ -1,277 +1,63 @@
1
1
  = Striuct
2
2
 
3
3
  * http://github.com/kachick/striuct
4
- * https://rubygems.org/gems/striuct
4
+ * http://rubygems.org/gems/striuct
5
5
  * http://rubyforge.org/projects/striuct
6
6
 
7
- == DESCRIPTION
7
+ == Description
8
8
 
9
- Striuct = (YetAnotherStruct << safety << useful)
9
+ Struct++
10
10
 
11
- == FEATURES
12
- * When you link a object, Striuct checks "They are valid objects?".
13
- These checkers don't depend only types(class).
14
-
15
- The safety is made from your expressions.
16
- * When use "inference", member will be fixed under class of first passed object.
17
- * It is without undesirable naming of conflict, no-identifier and some cases.
18
- * You can add a step just before link a object. (named "flavor" at here)
19
- * You can use default-value, and we have different meanings between nil and default-value.
20
- * It is easy to alias member name.
21
- * You can use inherite of structual class.
22
- * Base API looks like Struct.
23
- * No taint your name-space.
24
- * Pure Ruby
11
+ == Features
12
+
13
+ * Base APIs look like Struct.
14
+ * Easy and Flexible Validations
15
+ * Hook just before running setters
16
+ * Default value
17
+ * Member aliasing
18
+ * Inheritable
19
+ * Pure Ruby :)
25
20
 
26
- == SYNOPSIS
21
+ == Usage
27
22
 
28
23
  * setup
29
24
  require 'striuct'
30
25
 
31
- * use two namespace (same meanings)
32
- Striuct
33
- StrictStruct
34
-
35
- === Struct+ "Safety"
36
-
37
- ==== "member" macro
38
-
39
- * Use "member" and you get a accessor, it looks Struct with tester(validator)s.
40
- class User < Striuct.new
41
- member :id, Integer
42
- member :age, (20..140)
43
- member :name, OR(/\A\w+\z/, /\A\w+ \w+\z/)
44
- end
45
-
46
- # pass
47
- user = User.new 128381, 20
26
+ === Example
48
27
 
49
- # pass
50
- user.age = 30
51
- user[2] = 'foo bar'
52
-
53
- # fail
54
- user[:id] = 10.0
55
- user.age = 19
56
- user[2] = nil
57
-
58
- * Use functional object and you get a tester on upstairs.
59
- module Game
60
- class Character
61
- end
62
-
63
- class DB < Striuct.new
64
- member :monsters, ->list{(list - characters).empty?}
65
- member :characters, GENERICS(Character)
66
- end
67
-
68
- monster = Character.new
69
- db = DB.new
70
-
71
- # fail
72
- db.characters = [1, 2]
73
-
74
- # pass
75
- db.characters = [monster, Character.new]
76
-
77
- # fail
78
- db.monsters = [Character.new]
79
-
80
- # pass
81
- db.monsters = [monster]
28
+ Person = Striuct.define do
29
+ member :name, AND(String, /\A\w+( \w+)?\z/)
82
30
  end
83
-
84
- * Use "inference" and all instance test under class of first passed object.
85
- class FlexibleContainer < Striuct.new
86
- member :anything, inference
87
- member :number, Numeric, inference
88
- end
89
-
90
- fc1, fc2 = FlexibleContainer.new, FlexibleContainer.new
91
- # pass
92
- fc1.anything = 'str'
93
-
94
- # fail
95
- fc1.anything = :sym
96
- fc2.anything = :sym
97
-
98
- # pass
99
- fc2.anything = 'string too'
100
31
 
101
- # fail
102
- fc1.number = 'str'
103
-
104
- # pass
105
- fc1.number = 1.0
106
-
107
- # fail
108
- fc2.number = 1
109
-
110
- ==== Protect from risks in naming members
111
-
112
- * Standard Struct dosen't check member-name.
113
- NoGuard = Struct.new :object_id, :'? !'
114
- noguard = NoGuard.new false
115
- noguard.object_id #=> false
116
- noguard.methods.include?(:'? !') #=> false(lost!!)
117
-
118
- * Striuct provides safety levels for this. (default: :prevent)
119
- class SafetyNaming < Striuct.new
120
- begin
121
- member :object_id
122
- rescue
123
- p $!
124
- end
125
-
126
- begin
127
- member :'? !'
128
- rescue
129
- p $!
130
- end
131
-
132
- # set lower
133
- protect_level :struct
134
-
135
- member :object_id, :'? !'
136
- end
137
-
138
- ==== Support utils
139
-
140
- * valid? / acccept? / sufficient? # can set a argument to a member
141
- * conditionable? # can set a condition for any members
142
- * inference? # inference member and no passed any object
143
- * restrict? # has any conditions for a member
144
- * strict? # now, all members are passed any conditions
145
- * secure? # instance and class are fixed and strict
146
- * cname? # can use member name under protect level
147
-
148
- === Struct+ "Handy"
149
-
150
- ==== Flavor
151
-
152
- * Block with member macro, it is called "flavor" at here.
153
- Below cases for type cast.
154
- class User2 < Striuct.new
155
- member :age, /\A\d+\z/, Numeric do |arg|
156
- Integer arg
157
- end
158
-
159
- member :name, ->v{v.respond_to? :to_s} do |v|
160
- v.to_s.to_sym
161
- end
32
+ class User < Person
33
+ member :id, AND(Integer, 1..99999)
162
34
  end
163
35
 
164
- user2 = User2.new
165
- user2.age = 9 #=> 9(Fixnum)
166
- user2.age = 10.1 #=> 10(Fixnum)
167
- user2.age = '10' #=> 10(Fixnum)
36
+ user = User.new
37
+ user.members #=> [:name, :id]
38
+ user.name = :Ken #=> error
39
+ user.name = '' #=> error
40
+ user.name = 'Ken' #=> pass
168
41
 
169
- user2.name = 10 #=> :10(Symbol)
170
- user2.name = Class #=> :Class(Symbol)
42
+ === More Example
171
43
 
172
- ==== Default
44
+ example/*
173
45
 
174
- * provides default value
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.lank #=> 3
183
-
184
- * Standard Struct always define "nil is default". Realy?
185
- user3.name #=> nil
186
- user3.assign? :name #=> false
187
- user3.name = nil
188
- user3.assign? :name #=> true
189
-
190
- ==== Alias
191
-
192
- * alias member name
193
- class User3
194
- alias_member :position, :lank
195
- end
196
-
197
- user3.lank.equal? user3.position #=> true
198
- user3[:lank].equal? user3[:position] #=> true
199
- user3[:position] = 4
200
- user3.lank #=> 4
201
-
202
- ==== Inherit
203
-
204
- * (writing...)
205
-
206
- ==== Lock
207
-
208
- * (writing...)
209
-
210
- ==== New Constructors
211
-
212
- * Subclass.define reject floating object.
213
- * block parameter is new instance
214
- * except if no finished to assign each member
215
- * returning object is tested strict(optional)
216
- * returning object is locked(optional)
217
- user3 = User3.define do |r|
218
- r.lank = 10
219
- r.name = 'foo'
220
- end
221
-
222
- * Subclass.[](load_pairs) make from Hash and like Hash
223
- user3 = User3[lank: 10, name: 'foo']
224
-
225
- === Almost interfaces are keeping Struct has.
46
+ == Requirements
226
47
 
227
- Sth1 = Striuct.new do
228
- def my_special_method
229
- end
230
- end
231
-
232
- Sth1.new.respond_to?(:my_special_method) #=> true
233
-
234
- === HashLike
235
-
236
- * some interfaces import from Hash
237
-
238
- * easy cast to Hash
239
- user3.to_h #=> {:lank=>3, :name=>nil}
240
-
241
-
242
- == Note
243
-
244
- * Documents are poor
245
-
246
- == REQUIREMENTS
247
-
248
- * Ruby 1.9.2 and later (tested 1.9.2, 1.9.3)
249
-
250
- == INSTALL
251
-
252
- * sudo gem install striuct
48
+ * Ruby 1.9.2 or later
49
+
50
+ tested release versions
253
51
 
254
- == LICENSE
52
+ * 1.9.3-p125
53
+ * 1.9.2-p290
255
54
 
256
- (The MIT License)
55
+ == Installation
257
56
 
258
- Copyright (c) 2011 Kenichi Kamiya
57
+ * gem install striuct
259
58
 
260
- Permission is hereby granted, free of charge, to any person obtaining
261
- a copy of this software and associated documentation files (the
262
- 'Software'), to deal in the Software without restriction, including
263
- without limitation the rights to use, copy, modify, merge, publish,
264
- distribute, sublicense, and/or sell copies of the Software, and to
265
- permit persons to whom the Software is furnished to do so, subject to
266
- the following conditions:
59
+ == License
267
60
 
268
- The above copyright notice and this permission notice shall be
269
- included in all copies or substantial portions of the Software.
61
+ Copyright (C) 2011 Kenichi Kamiya
270
62
 
271
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
272
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
273
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
274
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
275
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
276
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
277
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
63
+ The MIT License (See the file LICENSE.)
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'rubygems'
2
- gem 'hoe', '~> 2.13.0'
2
+ gem 'hoe', '~> 3.0.1'
3
3
  require 'hoe'
4
4
  require 'fileutils'
5
5
 
@@ -13,7 +13,7 @@ $hoe = Hoe.spec 'striuct' do
13
13
  developer 'Kenichi Kamiya', 'kachick1+ruby@gmail.com'
14
14
  self.rubyforge_name = self.name
15
15
  require_ruby_version '>= 1.9.2'
16
- dependency 'yard', '~> 0.7.4', :development
16
+ dependency 'yard', '~> 0.7.5', :development
17
17
  end
18
18
 
19
19
  require 'newgem/tasks'