striuct 0.1.7 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
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/test/test_striuct.rb CHANGED
@@ -64,11 +64,6 @@ class TestStriuctSubclassEigen < Test::Unit::TestCase
64
64
  assert_equal false, user.strict?
65
65
  end
66
66
 
67
- def test_sufficient?
68
- assert_equal false, User.sufficient?(:age, 19)
69
- assert_equal true, User.sufficient?(:age, 20)
70
- end
71
-
72
67
  def test_restrict?
73
68
  klass = Striuct.new :foo do
74
69
  member :var, //
@@ -81,6 +76,14 @@ class TestStriuctSubclassEigen < Test::Unit::TestCase
81
76
  assert_equal false, klass.restrict?(:hoge)
82
77
  assert_equal true, klass.restrict?(:moge)
83
78
  end
79
+
80
+ def test_add_members
81
+ klass = Striuct.new :foo do
82
+ add_members :aaa, 'bbb', :ccc
83
+ end
84
+
85
+ assert_equal [:foo, :aaa, :bbb, :ccc], klass.members
86
+ end
84
87
  end
85
88
 
86
89
  class TestStriuctSubclassInstance1 < Test::Unit::TestCase
@@ -227,7 +230,19 @@ class TestStriuctSubclassInstance3 < Test::Unit::TestCase
227
230
 
228
231
  assert_equal [@user.each_value.to_a, @user.each_index.to_a].transpose, r
229
232
  end
230
-
233
+
234
+ def test_each_member_with_index
235
+ assert_same @user, @user.each_member_with_index{}
236
+ assert_kind_of Enumerator, enum = @user.each_member_with_index
237
+
238
+ r = []
239
+ @user.each_member_with_index do |name, index|
240
+ r << [name, index]
241
+ end
242
+
243
+ assert_equal [@user.each_key.to_a, @user.each_index.to_a].transpose, r
244
+ end
245
+
231
246
  def test_values
232
247
  assert_equal @user.values, [9999, 'taro', 'yamada', 'Tokyo Japan', 30]
233
248
  end
@@ -251,10 +266,10 @@ end
251
266
 
252
267
  class TestStriuctSubclassInstance4 < Test::Unit::TestCase
253
268
  class Sth < Striuct.new
254
- member :bool, true, false
269
+ member :bool, OR(true, false)
255
270
  member :sth
256
271
  protect_level :struct
257
- member :lambda, ->v{v % 3 == 2}, ->v{v.kind_of? Float}
272
+ member :lambda, OR(->v{v % 3 == 2}, ->v{v.kind_of? Float})
258
273
  end
259
274
 
260
275
  def setup
@@ -330,9 +345,7 @@ end
330
345
 
331
346
  class TestStriuctProcedure < Test::Unit::TestCase
332
347
  Sth = Striuct.new do
333
- member :age, /\A\d+\z/, Numeric do |arg|
334
- Integer arg
335
- end
348
+ member :age, Numeric, &->arg{Integer arg}
336
349
  end
337
350
 
338
351
  def setup
@@ -631,8 +644,8 @@ end
631
644
  class TestStriuctInference < Test::Unit::TestCase
632
645
  def test_inference
633
646
  klass = Striuct.define do
634
- member :n, Numeric, inference
635
- member :m, inference
647
+ member :n, Numeric, inference: true
648
+ member :m, anything, inference: true
636
649
  end
637
650
 
638
651
  sth, sth2 = klass.new, klass.new
@@ -903,13 +916,14 @@ end
903
916
 
904
917
  class TestStriuctSpecificConditions < Test::Unit::TestCase
905
918
  Sth = Striuct.define do
906
- member :list_only_int, generics(Integer)
919
+ member :list_only_int, GENERICS(Integer)
907
920
  member :true_or_false, boolean
908
921
  member :like_str, stringable
909
922
  member :has_x, CAN(:x)
910
923
  member :has_x_and_y, CAN(:x, :y)
911
- member :one_of_member, member_of([1, 3])
924
+ member :one_of_member, MEMBER_OF([1, 3])
912
925
  member :has_ignore, AND(1..5, 3..10)
926
+ member :nand, NAND(1..5, 3..10)
913
927
  member :all_pass, OR(1..5, 3..10)
914
928
  member :catch_name_error, CATCH(NameError){|v|v.no_name!}
915
929
  member :no_exception, STILL(->v{v.class})
@@ -989,7 +1003,7 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
989
1003
  end
990
1004
 
991
1005
  assert_raises Striuct::ConditionError do
992
- sth.has_ignore = 2
1006
+ sth.has_ignore= 2
993
1007
  end
994
1008
 
995
1009
  sth.has_ignore = 3
@@ -1000,7 +1014,27 @@ class TestStriuctSpecificConditions < Test::Unit::TestCase
1000
1014
  sth.has_ignore = []
1001
1015
  end
1002
1016
  end
1017
+
1018
+ def test_nand
1019
+ sth = Sth.new
1020
+
1021
+ assert_raises Striuct::ConditionError do
1022
+ sth.nand = 4
1023
+ end
1024
+
1025
+ assert_raises Striuct::ConditionError do
1026
+ sth.nand = 4.5
1027
+ end
1003
1028
 
1029
+ sth.nand = 2
1030
+ assert_equal 2, sth.nand
1031
+ assert_equal true, sth.valid?(:nand)
1032
+ sth.nand = []
1033
+ assert_equal [], sth.nand
1034
+ end
1035
+
1036
+
1037
+
1004
1038
  def test_member_of
1005
1039
  sth = Sth.new
1006
1040
 
@@ -1206,4 +1240,147 @@ class TestStriuctInherit < Test::Unit::TestCase
1206
1240
  SubSubSth.__send__(:close)
1207
1241
  assert_equal true, SubSubSth.__send__(:closed?)
1208
1242
  end
1209
- end
1243
+ end
1244
+
1245
+ class TestStriuctConstants < Test::Unit::TestCase
1246
+ def test_const_version
1247
+ assert_equal '0.2.3', Striuct::VERSION
1248
+ assert_equal true, Striuct::VERSION.frozen?
1249
+ assert_same Striuct::VERSION, Striuct::Version
1250
+ end
1251
+ end
1252
+
1253
+ class TestStriuctConstants < Test::Unit::TestCase
1254
+ Sth = Striuct.define do
1255
+ member :name
1256
+ member :age
1257
+ end
1258
+
1259
+ def test_each_pair_with_index
1260
+ sth = Sth.new 'a', 10
1261
+ assert_same sth, sth.each_pair_with_index{}
1262
+
1263
+ enum = sth.each_pair_with_index
1264
+ assert_equal [:name, 'a', 0], enum.next
1265
+ assert_equal [:age, 10, 1], enum.next
1266
+ assert_raises StopIteration do
1267
+ enum.next
1268
+ end
1269
+ end
1270
+ end
1271
+
1272
+ class TestStriuct_to_Struct < Test::Unit::TestCase
1273
+ Sth = Striuct.define do
1274
+ member :name, String
1275
+ member :age, Integer
1276
+ end
1277
+
1278
+ def test_to_struct_class
1279
+ klass = Sth.to_struct_class
1280
+ assert_equal 'Striuct::Structs::Sth', klass.to_s
1281
+ assert_same klass, Sth.to_struct_class
1282
+ assert_kind_of Struct, Sth.new.to_struct
1283
+ assert_kind_of Striuct::Structs::Sth, Sth.new.to_struct
1284
+
1285
+ assert_raises RuntimeError do
1286
+ Striuct.new.new.to_struct
1287
+ end
1288
+
1289
+ Striuct.new(:a, :b, :c).new.to_struct
1290
+ assert_equal 1, Striuct::Structs.constants.length
1291
+ end
1292
+ end
1293
+
1294
+ class TestStriuctFlavors < Test::Unit::TestCase
1295
+ class MyClass
1296
+ def self.parse(v)
1297
+ raise unless /\A[a-z]+\z/ =~ v
1298
+ new
1299
+ end
1300
+ end
1301
+
1302
+ Sth = Striuct.define do
1303
+ member :chomped, AND(Symbol, /[^\n]\z/), &WHEN(String, ->v{v.chomp.to_sym})
1304
+ member :no_reduced, Symbol, &->v{v.to_sym}
1305
+ member :reduced, Symbol, &FLAVORS(->v{v.to_s}, ->v{v.to_sym})
1306
+ member :integer, Integer, &PARSE(Integer)
1307
+ member :myobj, ->v{v.instance_of? MyClass}, &PARSE(MyClass)
1308
+ end
1309
+
1310
+ def test_WHEN
1311
+ sth = Sth.new
1312
+
1313
+ assert_raises Striuct::ConditionError do
1314
+ sth.chomped = :"a\n"
1315
+ end
1316
+
1317
+ sth.chomped = "a\n"
1318
+
1319
+ assert_equal :a, sth.chomped
1320
+
1321
+ sth.chomped = :b
1322
+ assert_equal :b, sth.chomped
1323
+ end
1324
+
1325
+ def test_REDUCE
1326
+ sth = Sth.new
1327
+
1328
+ assert_raises Striuct::ConditionError do
1329
+ sth.no_reduced = 1
1330
+ end
1331
+
1332
+ sth.reduced = 1
1333
+
1334
+ assert_equal :'1', sth.reduced
1335
+ end
1336
+
1337
+ def test_PARSE
1338
+ sth = Sth.new
1339
+
1340
+ assert_raises Striuct::ConditionError do
1341
+ sth.integer = '1.0'
1342
+ end
1343
+
1344
+ sth.integer = '1'
1345
+
1346
+ assert_equal 1, sth.integer
1347
+
1348
+ assert_raises Striuct::ConditionError do
1349
+ sth.myobj = '1'
1350
+ end
1351
+
1352
+ sth.myobj = 'a'
1353
+
1354
+ assert_kind_of MyClass, sth.myobj
1355
+ end
1356
+ end
1357
+
1358
+ class TestGetterValidation < Test::Unit::TestCase
1359
+ Sth = Striuct.define do
1360
+ member :plus_getter, /./, getter_validation: true
1361
+ member :only_getter, /./, getter_validation: true,
1362
+ setter_validation: false
1363
+ end
1364
+
1365
+ def test_getter_validation
1366
+ sth = Sth.new
1367
+
1368
+ assert_raises Striuct::ConditionError do
1369
+ sth.plus_getter = ''
1370
+ end
1371
+
1372
+ sth.plus_getter = 'abc'
1373
+ assert_equal 'abc', sth.plus_getter
1374
+ sth.plus_getter.clear
1375
+
1376
+ assert_raises Striuct::ConditionError do
1377
+ sth.plus_getter
1378
+ end
1379
+
1380
+ sth.only_getter = ''
1381
+
1382
+ assert_raises Striuct::ConditionError do
1383
+ sth.only_getter
1384
+ end
1385
+ end
1386
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: striuct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,33 +9,33 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-26 00:00:00.000000000 Z
12
+ date: 2012-04-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: yard
16
- requirement: &25011612 !ruby/object:Gem::Requirement
15
+ name: rdoc
16
+ requirement: &23052000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.7.4
21
+ version: '3.10'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *25011612
24
+ version_requirements: *23052000
25
25
  - !ruby/object:Gem::Dependency
26
- name: rdoc
27
- requirement: &25011336 !ruby/object:Gem::Requirement
26
+ name: yard
27
+ requirement: &23051652 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: '3.10'
32
+ version: 0.7.5
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *25011336
35
+ version_requirements: *23051652
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: newgem
38
- requirement: &25011072 !ruby/object:Gem::Requirement
38
+ requirement: &23051292 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,54 +43,62 @@ dependencies:
43
43
  version: 1.5.3
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *25011072
46
+ version_requirements: *23051292
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: hoe
49
- requirement: &25010808 !ruby/object:Gem::Requirement
49
+ requirement: &23050932 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: '2.13'
54
+ version: '3.0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *25010808
58
- description: Striuct = (YetAnotherStruct << safety << useful)
57
+ version_requirements: *23050932
58
+ description: Struct++
59
59
  email:
60
60
  - kachick1+ruby@gmail.com
61
61
  executables: []
62
62
  extensions: []
63
63
  extra_rdoc_files:
64
+ - History.rdoc
64
65
  - Manifest.txt
66
+ - README.rdoc
67
+ - README.ja.old.rdoc
68
+ - example/example.old.rdoc
65
69
  files:
66
70
  - History.rdoc
67
71
  - Manifest.txt
68
72
  - README.rdoc
69
- - README.ja.rdoc
73
+ - README.ja.old.rdoc
70
74
  - Rakefile
75
+ - LICENSE
71
76
  - lib/striuct.rb
72
- - lib/striuct/frame.rb
73
77
  - lib/striuct/version.rb
74
78
  - lib/striuct/abstract.rb
75
79
  - lib/striuct/conditions.rb
76
- - lib/striuct/subclassable/frame.rb
77
- - lib/striuct/subclassable/classutil.rb
78
- - lib/striuct/subclassable/basic.rb
79
- - lib/striuct/subclassable/safety.rb
80
- - lib/striuct/subclassable/handy.rb
81
- - lib/striuct/subclassable/hashlike.rb
82
- - lib/striuct/subclassable/yaml.rb
83
- - lib/striuct/subclassable/inner.rb
84
- - lib/striuct/subclassable/eigen/frame.rb
85
- - lib/striuct/subclassable/eigen/basic.rb
86
- - lib/striuct/subclassable/eigen/constructor.rb
87
- - lib/striuct/subclassable/eigen/safety.rb
88
- - lib/striuct/subclassable/eigen/handy.rb
89
- - lib/striuct/subclassable/eigen/macro.rb
90
- - lib/striuct/subclassable/eigen/inner.rb
80
+ - lib/striuct/flavors.rb
81
+ - lib/striuct/containable.rb
82
+ - lib/striuct/containable/classutil.rb
83
+ - lib/striuct/containable/basic.rb
84
+ - lib/striuct/containable/safety.rb
85
+ - lib/striuct/containable/handy.rb
86
+ - lib/striuct/containable/hashlike.rb
87
+ - lib/striuct/containable/yaml.rb
88
+ - lib/striuct/containable/inner.rb
89
+ - lib/striuct/containable/eigen.rb
90
+ - lib/striuct/containable/eigen/basic.rb
91
+ - lib/striuct/containable/eigen/constructor.rb
92
+ - lib/striuct/containable/eigen/safety.rb
93
+ - lib/striuct/containable/eigen/handy.rb
94
+ - lib/striuct/containable/eigen/macro.rb
95
+ - lib/striuct/containable/eigen/inner.rb
91
96
  - test/test_helper.rb
92
97
  - test/test_striuct.rb
93
98
  - example/example.rb
99
+ - example/example1.rb
100
+ - example/example2.rb
101
+ - example/example.old.rdoc
94
102
  - example/benchmarks.rb
95
103
  - example/see_trace.rb
96
104
  - .gemtest
@@ -116,10 +124,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
124
  version: '0'
117
125
  requirements: []
118
126
  rubyforge_project: striuct
119
- rubygems_version: 1.8.15
127
+ rubygems_version: 1.8.16
120
128
  signing_key:
121
129
  specification_version: 3
122
- summary: Striuct = (YetAnotherStruct << safety << useful)
130
+ summary: Struct++
123
131
  test_files:
124
132
  - test/test_helper.rb
125
133
  - test/test_striuct.rb
data/lib/striuct/frame.rb DELETED
@@ -1,8 +0,0 @@
1
- # @author Kenichi Kamiya
2
- # @abstract
3
- class Striuct
4
- class ConditionError < ArgumentError; end
5
- end
6
-
7
- require_relative 'version'
8
- require_relative 'abstract'
@@ -1,26 +0,0 @@
1
- class Striuct; module Subclassable
2
-
3
-
4
- # @author Kenichi Kamiya
5
- module ClassUtil
6
-
7
- private
8
-
9
- def delegate_class_method(name)
10
- define_method name do |*args, &block|
11
- self.class.__send__ name, *args, &block
12
- end
13
- end
14
-
15
- def delegate_class_methods(*names)
16
- unless names.length >= 1
17
- raise ArgumentError, 'wrong number of argument 0 for 1+'
18
- end
19
-
20
- names.each{|name|delegate_class_method name}
21
- end
22
-
23
- end
24
-
25
-
26
- end; end
@@ -1,33 +0,0 @@
1
- require_relative 'classutil'
2
- require_relative 'eigen/frame'
3
-
4
- class Striuct
5
-
6
- # @author Kenichi Kamiya
7
- module Subclassable
8
- extend ClassUtil
9
- include Enumerable
10
-
11
- SpecificContainer = Struct.new :value
12
-
13
- if respond_to? :private_constant
14
- private_constant :SpecificContainer
15
- end
16
-
17
- class << self
18
- private
19
-
20
- def included(klass)
21
- klass.extend Eigen
22
- end
23
- end
24
- end
25
-
26
- end
27
-
28
- require_relative 'basic'
29
- require_relative 'safety'
30
- require_relative 'handy'
31
- require_relative 'hashlike'
32
- require_relative 'yaml'
33
- require_relative 'inner'
@@ -1,57 +0,0 @@
1
- class Striuct; module Subclassable
2
- # @group Struct + Handy
3
-
4
- # see self.class.*args
5
- delegate_class_methods :has_default?, :default_for, :has_flavor?
6
-
7
- # @yield [index]
8
- # @yieldparam [Integer] index
9
- # @yieldreturn [self]
10
- # @return [Enumerator]
11
- def each_index(&block)
12
- return to_enum(__method__) unless block_given?
13
- self.class.each_index(&block)
14
- self
15
- end
16
-
17
- # @yield [value, index]
18
- # @yieldparam [Integer] index
19
- # @yieldreturn [self]
20
- # @return [Enumerator]
21
- def each_value_with_index(&block)
22
- return to_enum(__method__) unless block_given?
23
- each_value.with_index(&block)
24
- self
25
- end
26
-
27
- alias_method :each_with_index, :each_value_with_index
28
-
29
- # @param [Symbol, String] name
30
- def assign?(name)
31
- name = originalkey_for(keyable_for name)
32
-
33
- @db.has_key? name
34
- end
35
-
36
- # @param [Symbol, String, Fixnum] key
37
- def clear_at(key)
38
- __subscript__(key){|name|__clear__ name}
39
- end
40
-
41
- alias_method :unassign, :clear_at
42
- alias_method :reset_at, :clear_at
43
-
44
- # @param [Symbol, String] name
45
- def default?(name)
46
- name = originalkey_for(keyable_for name)
47
-
48
- default_for(name) == self[name]
49
- end
50
-
51
- # all members aren't assigned
52
- def empty?
53
- each_name.none?{|name|assign? name}
54
- end
55
-
56
- # @endgroup
57
- end; end