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/README.md
CHANGED
@@ -1,128 +1,128 @@
|
|
1
|
-
striuct
|
2
|
-
=======
|
3
|
-
|
4
|
-
Description
|
5
|
-
-----------
|
6
|
-
|
7
|
-
Struct++
|
8
|
-
|
9
|
-
Features
|
10
|
-
--------
|
11
|
-
|
12
|
-
### Strict
|
13
|
-
|
14
|
-
* Easy and Flexible Validations
|
15
|
-
* Prevent to conflict member names
|
16
|
-
* Lock setters for each member
|
17
|
-
|
18
|
-
### Useful
|
19
|
-
|
20
|
-
* Hook just before setters
|
21
|
-
* Default value
|
22
|
-
* Member aliasing
|
23
|
-
* Inheritable
|
24
|
-
* Handling between nil <-> unassigned
|
25
|
-
* More flendly API for Hash
|
26
|
-
|
27
|
-
### Onepoint
|
28
|
-
|
29
|
-
* Base API looks like Struct
|
30
|
-
* Pure Ruby :)
|
31
|
-
|
32
|
-
Usage
|
33
|
-
-----
|
34
|
-
|
35
|
-
### Overview - Case 1
|
36
|
-
|
37
|
-
```ruby
|
38
|
-
require 'striuct'
|
39
|
-
|
40
|
-
class Person < Striuct
|
41
|
-
member :fullname, AND(String, /\A.+\z/) # Flexible Validation
|
42
|
-
alias_member :name, :fullname # Use other name
|
43
|
-
end
|
44
|
-
|
45
|
-
class User < Person # Inheritable
|
46
|
-
member :id, Integer, # Looks typed validation
|
47
|
-
default_proc: ->{User.next_id} # With default value
|
48
|
-
|
49
|
-
def self.next_id
|
50
|
-
@id ||= 0
|
51
|
-
@id += 1
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
john = User.new 'john'
|
56
|
-
john[:name] #=> 'john'
|
57
|
-
john.name = '' #=> Exception # Validate with setter
|
58
|
-
john.id #=> 1
|
59
|
-
ken = User[name: 'ken'] # Construct from hash
|
60
|
-
ken.id #=> 2
|
61
|
-
```
|
62
|
-
### Overview - Case 2
|
63
|
-
|
64
|
-
```ruby
|
65
|
-
class Foo < Striuct
|
66
|
-
member :foo
|
67
|
-
member :bar, Numeric, inference: true
|
68
|
-
member :with_adjuster, Integer, &->v{Integer v}
|
69
|
-
end
|
70
|
-
|
71
|
-
foo = Foo.new
|
72
|
-
|
73
|
-
# nil <-> unaasigned
|
74
|
-
foo.foo #=> nil
|
75
|
-
foo.assign?(:foo) #=> false
|
76
|
-
foo.foo = nil
|
77
|
-
foo.assign?(:foo) #=> true
|
78
|
-
|
79
|
-
# Lock to a member
|
80
|
-
foo.lock(:foo)
|
81
|
-
foo.foo = nil #=> error
|
82
|
-
|
83
|
-
# Inference Validation
|
84
|
-
foo.bar = 1.2 #=> pass # memorize 1.2's class is Float
|
85
|
-
foo.bar = 1 #=> error # 1 is not Float
|
86
|
-
|
87
|
-
# With adjuster
|
88
|
-
foo.with_adjuster = '5'
|
89
|
-
foo.with_adjuster #=> 5 # Casted via adjuster
|
90
|
-
```
|
91
|
-
|
92
|
-
|
93
|
-
### More Examples
|
94
|
-
|
95
|
-
#### Basics
|
96
|
-
|
97
|
-
example/*
|
98
|
-
|
99
|
-
Requirements
|
100
|
-
-------------
|
101
|
-
|
102
|
-
* Ruby 1.9.2 or later [MRI/YARV, Rubinius](http://travis-ci.org/#!/kachick/striuct)
|
103
|
-
* validation - 0.0.3
|
104
|
-
* keyvalidatable - 0.0.3
|
105
|
-
|
106
|
-
Install
|
107
|
-
-------
|
108
|
-
|
109
|
-
```shell
|
110
|
-
$ gem install striuct
|
111
|
-
```
|
112
|
-
|
113
|
-
Link
|
114
|
-
----
|
115
|
-
|
116
|
-
* [code](https://github.com/kachick/striuct)
|
117
|
-
* [API](http://kachick.github.com/docs/striuct/api/frames.html)
|
118
|
-
* [issues](https://github.com/kachick/striuct/issues)
|
119
|
-
* [CI](http://travis-ci.org/#!/kachick/striuct)
|
120
|
-
* [gem](https://rubygems.org/gems/striuct)
|
121
|
-
|
122
|
-
License
|
123
|
-
--------
|
124
|
-
|
125
|
-
The MIT X11 License
|
126
|
-
Copyright (c) 2011 Kenichi Kamiya
|
127
|
-
See the file LICENSE for further details.
|
128
|
-
|
1
|
+
striuct
|
2
|
+
=======
|
3
|
+
|
4
|
+
Description
|
5
|
+
-----------
|
6
|
+
|
7
|
+
Struct++
|
8
|
+
|
9
|
+
Features
|
10
|
+
--------
|
11
|
+
|
12
|
+
### Strict
|
13
|
+
|
14
|
+
* Easy and Flexible Validations
|
15
|
+
* Prevent to conflict member names
|
16
|
+
* Lock setters for each member
|
17
|
+
|
18
|
+
### Useful
|
19
|
+
|
20
|
+
* Hook just before setters
|
21
|
+
* Default value
|
22
|
+
* Member aliasing
|
23
|
+
* Inheritable
|
24
|
+
* Handling between nil <-> unassigned
|
25
|
+
* More flendly API for Hash
|
26
|
+
|
27
|
+
### Onepoint
|
28
|
+
|
29
|
+
* Base API looks like Struct
|
30
|
+
* Pure Ruby :)
|
31
|
+
|
32
|
+
Usage
|
33
|
+
-----
|
34
|
+
|
35
|
+
### Overview - Case 1
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'striuct'
|
39
|
+
|
40
|
+
class Person < Striuct
|
41
|
+
member :fullname, AND(String, /\A.+\z/) # Flexible Validation
|
42
|
+
alias_member :name, :fullname # Use other name
|
43
|
+
end
|
44
|
+
|
45
|
+
class User < Person # Inheritable
|
46
|
+
member :id, Integer, # Looks typed validation
|
47
|
+
default_proc: ->{User.next_id} # With default value
|
48
|
+
|
49
|
+
def self.next_id
|
50
|
+
@id ||= 0
|
51
|
+
@id += 1
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
john = User.new 'john'
|
56
|
+
john[:name] #=> 'john'
|
57
|
+
john.name = '' #=> Exception # Validate with setter
|
58
|
+
john.id #=> 1
|
59
|
+
ken = User[name: 'ken'] # Construct from hash
|
60
|
+
ken.id #=> 2
|
61
|
+
```
|
62
|
+
### Overview - Case 2
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class Foo < Striuct
|
66
|
+
member :foo
|
67
|
+
member :bar, Numeric, inference: true
|
68
|
+
member :with_adjuster, Integer, &->v{Integer v}
|
69
|
+
end
|
70
|
+
|
71
|
+
foo = Foo.new
|
72
|
+
|
73
|
+
# nil <-> unaasigned
|
74
|
+
foo.foo #=> nil
|
75
|
+
foo.assign?(:foo) #=> false
|
76
|
+
foo.foo = nil
|
77
|
+
foo.assign?(:foo) #=> true
|
78
|
+
|
79
|
+
# Lock to a member
|
80
|
+
foo.lock(:foo)
|
81
|
+
foo.foo = nil #=> error
|
82
|
+
|
83
|
+
# Inference Validation
|
84
|
+
foo.bar = 1.2 #=> pass # memorize 1.2's class is Float
|
85
|
+
foo.bar = 1 #=> error # 1 is not Float
|
86
|
+
|
87
|
+
# With adjuster
|
88
|
+
foo.with_adjuster = '5'
|
89
|
+
foo.with_adjuster #=> 5 # Casted via adjuster
|
90
|
+
```
|
91
|
+
|
92
|
+
|
93
|
+
### More Examples
|
94
|
+
|
95
|
+
#### Basics
|
96
|
+
|
97
|
+
example/*
|
98
|
+
|
99
|
+
Requirements
|
100
|
+
-------------
|
101
|
+
|
102
|
+
* Ruby 1.9.2 or later [MRI/YARV, Rubinius](http://travis-ci.org/#!/kachick/striuct)
|
103
|
+
* validation - 0.0.3
|
104
|
+
* keyvalidatable - 0.0.3
|
105
|
+
|
106
|
+
Install
|
107
|
+
-------
|
108
|
+
|
109
|
+
```shell
|
110
|
+
$ gem install striuct
|
111
|
+
```
|
112
|
+
|
113
|
+
Link
|
114
|
+
----
|
115
|
+
|
116
|
+
* [code](https://github.com/kachick/striuct)
|
117
|
+
* [API](http://kachick.github.com/docs/striuct/api/frames.html)
|
118
|
+
* [issues](https://github.com/kachick/striuct/issues)
|
119
|
+
* [CI](http://travis-ci.org/#!/kachick/striuct)
|
120
|
+
* [gem](https://rubygems.org/gems/striuct)
|
121
|
+
|
122
|
+
License
|
123
|
+
--------
|
124
|
+
|
125
|
+
The MIT X11 License
|
126
|
+
Copyright (c) 2011 Kenichi Kamiya
|
127
|
+
See the file LICENSE for further details.
|
128
|
+
|
data/Rakefile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
#!/usr/bin/env rake
|
2
|
-
require 'bundler/gem_tasks'
|
3
|
-
|
4
|
-
require 'rake/testtask'
|
5
|
-
|
6
|
-
task default: [:test]
|
7
|
-
|
8
|
-
Rake::TestTask.new do |tt|
|
9
|
-
tt.verbose = true
|
10
|
-
end
|
11
|
-
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
task default: [:test]
|
7
|
+
|
8
|
+
Rake::TestTask.new do |tt|
|
9
|
+
tt.verbose = true
|
10
|
+
end
|
11
|
+
|
data/benchmark/basics.rb
CHANGED
@@ -1,56 +1,56 @@
|
|
1
|
-
#/usr/bin/ruby -w
|
2
|
-
|
3
|
-
require 'benchmark'
|
4
|
-
require_relative '../lib/striuct'
|
5
|
-
|
6
|
-
XStruct = Struct.new :any, :no_use1, :no_use2
|
7
|
-
|
8
|
-
XStriuct = Striuct.define do
|
9
|
-
member :any
|
10
|
-
member :int, Integer
|
11
|
-
member :truthy, ->v{v}
|
12
|
-
end
|
13
|
-
|
14
|
-
xstruct = XStruct.new
|
15
|
-
xstriuct = XStriuct.new
|
16
|
-
|
17
|
-
TIMES = 100000
|
18
|
-
OBJ = 123
|
19
|
-
|
20
|
-
Benchmark.bm do |bm|
|
21
|
-
bm.report 'Struct(equal Noguard): Setter' do
|
22
|
-
TIMES.times do
|
23
|
-
xstruct.any = OBJ
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
bm.report 'Struct: Reader' do
|
28
|
-
TIMES.times do
|
29
|
-
xstruct.any
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
bm.report 'Striuct(when Noguard): Setter' do
|
34
|
-
TIMES.times do
|
35
|
-
xstriuct.any = OBJ
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
bm.report 'Striuct(guard under class): Setter' do
|
40
|
-
TIMES.times do
|
41
|
-
xstriuct.int = OBJ
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
bm.report 'Striuct(guard under function)' do
|
46
|
-
TIMES.times do
|
47
|
-
xstriuct.truthy = OBJ
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
bm.report 'Striuct: Reader' do
|
52
|
-
TIMES.times do
|
53
|
-
xstriuct.any
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
1
|
+
#/usr/bin/ruby -w
|
2
|
+
|
3
|
+
require 'benchmark'
|
4
|
+
require_relative '../lib/striuct'
|
5
|
+
|
6
|
+
XStruct = Struct.new :any, :no_use1, :no_use2
|
7
|
+
|
8
|
+
XStriuct = Striuct.define do
|
9
|
+
member :any
|
10
|
+
member :int, Integer
|
11
|
+
member :truthy, ->v{v}
|
12
|
+
end
|
13
|
+
|
14
|
+
xstruct = XStruct.new
|
15
|
+
xstriuct = XStriuct.new
|
16
|
+
|
17
|
+
TIMES = 100000
|
18
|
+
OBJ = 123
|
19
|
+
|
20
|
+
Benchmark.bm do |bm|
|
21
|
+
bm.report 'Struct(equal Noguard): Setter' do
|
22
|
+
TIMES.times do
|
23
|
+
xstruct.any = OBJ
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
bm.report 'Struct: Reader' do
|
28
|
+
TIMES.times do
|
29
|
+
xstruct.any
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
bm.report 'Striuct(when Noguard): Setter' do
|
34
|
+
TIMES.times do
|
35
|
+
xstriuct.any = OBJ
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
bm.report 'Striuct(guard under class): Setter' do
|
40
|
+
TIMES.times do
|
41
|
+
xstriuct.int = OBJ
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
bm.report 'Striuct(guard under function)' do
|
46
|
+
TIMES.times do
|
47
|
+
xstriuct.truthy = OBJ
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
bm.report 'Striuct: Reader' do
|
52
|
+
TIMES.times do
|
53
|
+
xstriuct.any
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/example/README.rb
CHANGED
@@ -1,44 +1,44 @@
|
|
1
|
-
$VERBOSE = true
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
class Person < Striuct
|
6
|
-
member :fullname, AND(String, /\A.+\z/) # Flexible Validation
|
7
|
-
alias_member :name, :fullname # Use other name
|
8
|
-
end
|
9
|
-
|
10
|
-
class User < Person # Inheritable
|
11
|
-
member :id, Integer, # Looks typed validation
|
12
|
-
default_proc: ->{User.next_id} # With default value
|
13
|
-
|
14
|
-
def self.next_id
|
15
|
-
@id ||= 0
|
16
|
-
@id += 1
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
john = User.new 'john'
|
21
|
-
p john[:name] #=> 'john'
|
22
|
-
#~ p john.name = '' #=> error
|
23
|
-
p john.id #=> 1
|
24
|
-
ken = User[name: 'ken'] # Construct from hash
|
25
|
-
p ken.id #=> 2
|
26
|
-
|
27
|
-
class Foo < Striuct
|
28
|
-
member :foo
|
29
|
-
member :bar, Numeric, inference: true
|
30
|
-
member :with_adjuster, Integer, &->v{Integer v}
|
31
|
-
end
|
32
|
-
|
33
|
-
foo = Foo.new
|
34
|
-
p foo.foo #=> nil
|
35
|
-
p foo.assign?(:foo) #=> false
|
36
|
-
foo.foo = nil
|
37
|
-
p foo.assign?(:foo) #=> true
|
38
|
-
foo.lock(:foo)
|
39
|
-
#~ foo.foo = nil #=> error
|
40
|
-
foo.bar = 1.2
|
41
|
-
#~ foo.bar = 1 #=> error
|
42
|
-
|
43
|
-
foo.with_adjuster = '5'
|
44
|
-
p foo.with_adjuster
|
1
|
+
$VERBOSE = true
|
2
|
+
|
3
|
+
require_relative '../lib/striuct'
|
4
|
+
|
5
|
+
class Person < Striuct
|
6
|
+
member :fullname, AND(String, /\A.+\z/) # Flexible Validation
|
7
|
+
alias_member :name, :fullname # Use other name
|
8
|
+
end
|
9
|
+
|
10
|
+
class User < Person # Inheritable
|
11
|
+
member :id, Integer, # Looks typed validation
|
12
|
+
default_proc: ->{User.next_id} # With default value
|
13
|
+
|
14
|
+
def self.next_id
|
15
|
+
@id ||= 0
|
16
|
+
@id += 1
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
john = User.new 'john'
|
21
|
+
p john[:name] #=> 'john'
|
22
|
+
#~ p john.name = '' #=> error
|
23
|
+
p john.id #=> 1
|
24
|
+
ken = User[name: 'ken'] # Construct from hash
|
25
|
+
p ken.id #=> 2
|
26
|
+
|
27
|
+
class Foo < Striuct
|
28
|
+
member :foo
|
29
|
+
member :bar, Numeric, inference: true
|
30
|
+
member :with_adjuster, Integer, &->v{Integer v}
|
31
|
+
end
|
32
|
+
|
33
|
+
foo = Foo.new
|
34
|
+
p foo.foo #=> nil
|
35
|
+
p foo.assign?(:foo) #=> false
|
36
|
+
foo.foo = nil
|
37
|
+
p foo.assign?(:foo) #=> true
|
38
|
+
foo.lock(:foo)
|
39
|
+
#~ foo.foo = nil #=> error
|
40
|
+
foo.bar = 1.2
|
41
|
+
#~ foo.bar = 1 #=> error
|
42
|
+
|
43
|
+
foo.with_adjuster = '5'
|
44
|
+
p foo.with_adjuster
|