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.
Files changed (88) hide show
  1. data/.gitignore +35 -34
  2. data/.travis.yml +6 -6
  3. data/Gemfile +12 -12
  4. data/History.rdoc +279 -273
  5. data/LICENSE +21 -21
  6. data/Manifest.txt +91 -90
  7. data/README.ja.old.rdoc +297 -297
  8. data/README.md +128 -128
  9. data/Rakefile +11 -11
  10. data/benchmark/basics.rb +56 -56
  11. data/example/README.rb +44 -44
  12. data/example/example.old.rdoc +187 -187
  13. data/example/example1.rb +233 -233
  14. data/example/example2.rb +22 -22
  15. data/example/see_trace.rb +32 -32
  16. data/lib/striuct/classmethods/README.md +5 -5
  17. data/lib/striuct/classmethods/adjustment.rb +32 -32
  18. data/lib/striuct/classmethods/constructor.rb +63 -63
  19. data/lib/striuct/classmethods/default.rb +27 -27
  20. data/lib/striuct/classmethods/enum.rb +48 -48
  21. data/lib/striuct/classmethods/inner.rb +91 -91
  22. data/lib/striuct/classmethods/length.rb +14 -14
  23. data/lib/striuct/classmethods/macro.rb +144 -144
  24. data/lib/striuct/classmethods/named.rb +113 -113
  25. data/lib/striuct/classmethods/object.rb +54 -54
  26. data/lib/striuct/classmethods/prevent_conflicts.rb +89 -89
  27. data/lib/striuct/classmethods/requiremnets.rb +11 -11
  28. data/lib/striuct/classmethods/to_struct.rb +23 -25
  29. data/lib/striuct/classmethods/validation.rb +55 -55
  30. data/lib/striuct/classmethods.rb +1 -1
  31. data/lib/striuct/instancemethods/README.md +5 -5
  32. data/lib/striuct/instancemethods/assign.rb +30 -30
  33. data/lib/striuct/instancemethods/cast.rb +34 -34
  34. data/lib/striuct/instancemethods/compare.rb +30 -30
  35. data/lib/striuct/instancemethods/default.rb +13 -13
  36. data/lib/striuct/instancemethods/delegate_class_methods.rb +28 -28
  37. data/lib/striuct/instancemethods/enum.rb +103 -103
  38. data/lib/striuct/instancemethods/hashy.rb +121 -121
  39. data/lib/striuct/instancemethods/inner.rb +60 -60
  40. data/lib/striuct/instancemethods/keyvalidatable.rb +14 -14
  41. data/lib/striuct/instancemethods/lock.rb +61 -61
  42. data/lib/striuct/instancemethods/object.rb +52 -52
  43. data/lib/striuct/instancemethods/requirements.rb +14 -14
  44. data/lib/striuct/instancemethods/safety.rb +11 -11
  45. data/lib/striuct/instancemethods/subscript.rb +55 -55
  46. data/lib/striuct/instancemethods/validation.rb +29 -25
  47. data/lib/striuct/instancemethods/values.rb +57 -57
  48. data/lib/striuct/instancemethods.rb +1 -1
  49. data/lib/striuct/requirements.rb +5 -5
  50. data/lib/striuct/singleton_class.rb +66 -66
  51. data/lib/striuct/specificcontainer.rb +19 -19
  52. data/lib/striuct/structs.rb +7 -7
  53. data/lib/striuct/version.rb +5 -5
  54. data/lib/striuct.rb +13 -13
  55. data/striuct.gemspec +24 -24
  56. data/test/helper.rb +5 -5
  57. data/test/test_striuct-singleton_class-define.rb +19 -19
  58. data/test/test_striuct-subclass-class-cloning.rb +20 -20
  59. data/test/test_striuct-subclass-class-close.rb +36 -36
  60. data/test/test_striuct-subclass-class-constructor.rb +82 -82
  61. data/test/test_striuct-subclass-class-freeze.rb +36 -36
  62. data/test/test_striuct-subclass-class-inheritable.rb +57 -57
  63. data/test/test_striuct-subclass-class-macro.rb +13 -13
  64. data/test/test_striuct-subclass-class-safety_naming.rb +72 -72
  65. data/test/test_striuct-subclass-class-validation.rb +26 -26
  66. data/test/test_striuct-subclass-feature-alias_member.rb +53 -53
  67. data/test/test_striuct-subclass-feature-to_struct.rb +61 -25
  68. data/test/test_striuct-subclass-feature-validation_util.rb +57 -0
  69. data/test/test_striuct-subclass-instance-accessor.rb +137 -137
  70. data/test/test_striuct-subclass-instance-adjuster.rb +94 -94
  71. data/test/test_striuct-subclass-instance-assign.rb +30 -30
  72. data/test/test_striuct-subclass-instance-basic.rb +32 -32
  73. data/test/test_striuct-subclass-instance-cloning.rb +22 -22
  74. data/test/test_striuct-subclass-instance-compare.rb +51 -51
  75. data/test/test_striuct-subclass-instance-default_value.rb +128 -128
  76. data/test/test_striuct-subclass-instance-enum.rb +92 -92
  77. data/test/test_striuct-subclass-instance-freeze.rb +19 -19
  78. data/test/test_striuct-subclass-instance-hashlike.rb +153 -153
  79. data/test/test_striuct-subclass-instance-keyvalidatable.rb +24 -24
  80. data/test/test_striuct-subclass-instance-lock.rb +39 -39
  81. data/test/test_striuct-subclass-instance-to_s_family.rb +25 -25
  82. data/test/test_striuct-subclass-instance-validation_functional_condition.rb +50 -50
  83. data/test/test_striuct-subclass-instance-validation_inference.rb +50 -50
  84. data/test/test_striuct-subclass-instance-validation_specific_conditions.rb +247 -247
  85. data/test/test_striuct-subclass-instance-validation_with_getter.rb +33 -33
  86. data/test/test_striuct-subclass-instance_names.rb +18 -18
  87. data/test/test_striuct-version.rb +10 -10
  88. metadata +4 -2
@@ -1,104 +1,104 @@
1
- class Striuct; module InstanceMethods
2
-
3
- # @group Enumerative
4
-
5
- # @yield [autonym]
6
- # @yieldparam [Symbol] autonym - sequential under defined
7
- # @yieldreturn [self]
8
- # @return [Enumerator]
9
- def each_autonym(&block)
10
- return to_enum(__callee__) unless block_given?
11
-
12
- self.class.each_autonym(&block)
13
- self
14
- end
15
-
16
- alias_method :each_name, :each_autonym
17
- alias_method :each_member, :each_autonym
18
- alias_method :each_key, :each_autonym
19
-
20
- # @yield [value]
21
- # @yieldparam [Object] value - sequential under defined
22
- # @see #each_autonym
23
- # @yieldreturn [self]
24
- # @return [Enumerator]
25
- def each_value
26
- return to_enum(__callee__) unless block_given?
27
-
28
- each_autonym{|autonym|yield self[autonym]}
29
- end
30
-
31
- alias_method :each, :each_value
32
-
33
- # @yield [autonym, value]
34
- # @yieldparam [Symbol] autonym
35
- # @yieldparam [Object] value
36
- # @yieldreturn [self]
37
- # @return [Enumerator]
38
- # @see #each_autonym
39
- # @see #each_value
40
- def each_pair
41
- return to_enum(__callee__) unless block_given?
42
-
43
- each_autonym{|autonym|yield autonym, self[autonym]}
44
- end
45
-
46
- # @yield [index]
47
- # @yieldparam [Integer] index
48
- # @yieldreturn [self]
49
- # @return [Enumerator]
50
- def each_index
51
- return to_enum(__callee__) unless block_given?
52
-
53
- self.class.each_index{|index|yield index}
54
- self
55
- end
56
-
57
- # @yield [autonym, index]
58
- # @yieldparam [Symbol] autonym
59
- # @yieldparam [Integer] index
60
- # @yieldreturn [self]
61
- # @return [Enumerator]
62
- def each_autonym_with_index
63
- return to_enum(__callee__) unless block_given?
64
-
65
- self.class.each_autonym_with_index{|autonym, index|yield autonym, index}
66
- self
67
- end
68
-
69
- alias_method :each_name_with_index, :each_autonym_with_index
70
- alias_method :each_member_with_index, :each_autonym_with_index
71
- alias_method :each_key_with_index, :each_autonym_with_index
72
-
73
- # @yield [value, index]
74
- # @yieldparam [Integer] index
75
- # @yieldreturn [self]
76
- # @return [Enumerator]
77
- def each_value_with_index
78
- return to_enum(__callee__) unless block_given?
79
-
80
- each_value.with_index{|value, index|yield value, index}
81
- self
82
- end
83
-
84
- alias_method :each_with_index, :each_value_with_index
85
-
86
- # @yield [autonym, value, index]
87
- # @yieldparam [Symbol] autonym
88
- # @yieldparam [Integer] index
89
- # @yieldreturn [self]
90
- # @return [Enumerator]
91
- def each_pair_with_index
92
- return to_enum(__callee__) unless block_given?
93
-
94
- index = 0
95
- each_pair do |autonym, value|
96
- yield autonym, value, index
97
- index += 1
98
- end
99
- self
100
- end
101
-
102
- # @endgroup
103
-
1
+ class Striuct; module InstanceMethods
2
+
3
+ # @group Enumerative
4
+
5
+ # @yield [autonym]
6
+ # @yieldparam [Symbol] autonym - sequential under defined
7
+ # @yieldreturn [self]
8
+ # @return [Enumerator]
9
+ def each_autonym(&block)
10
+ return to_enum(__callee__) unless block_given?
11
+
12
+ self.class.each_autonym(&block)
13
+ self
14
+ end
15
+
16
+ alias_method :each_name, :each_autonym
17
+ alias_method :each_member, :each_autonym
18
+ alias_method :each_key, :each_autonym
19
+
20
+ # @yield [value]
21
+ # @yieldparam [Object] value - sequential under defined
22
+ # @see #each_autonym
23
+ # @yieldreturn [self]
24
+ # @return [Enumerator]
25
+ def each_value
26
+ return to_enum(__callee__) unless block_given?
27
+
28
+ each_autonym{|autonym|yield self[autonym]}
29
+ end
30
+
31
+ alias_method :each, :each_value
32
+
33
+ # @yield [autonym, value]
34
+ # @yieldparam [Symbol] autonym
35
+ # @yieldparam [Object] value
36
+ # @yieldreturn [self]
37
+ # @return [Enumerator]
38
+ # @see #each_autonym
39
+ # @see #each_value
40
+ def each_pair
41
+ return to_enum(__callee__) unless block_given?
42
+
43
+ each_autonym{|autonym|yield autonym, self[autonym]}
44
+ end
45
+
46
+ # @yield [index]
47
+ # @yieldparam [Integer] index
48
+ # @yieldreturn [self]
49
+ # @return [Enumerator]
50
+ def each_index
51
+ return to_enum(__callee__) unless block_given?
52
+
53
+ self.class.each_index{|index|yield index}
54
+ self
55
+ end
56
+
57
+ # @yield [autonym, index]
58
+ # @yieldparam [Symbol] autonym
59
+ # @yieldparam [Integer] index
60
+ # @yieldreturn [self]
61
+ # @return [Enumerator]
62
+ def each_autonym_with_index
63
+ return to_enum(__callee__) unless block_given?
64
+
65
+ self.class.each_autonym_with_index{|autonym, index|yield autonym, index}
66
+ self
67
+ end
68
+
69
+ alias_method :each_name_with_index, :each_autonym_with_index
70
+ alias_method :each_member_with_index, :each_autonym_with_index
71
+ alias_method :each_key_with_index, :each_autonym_with_index
72
+
73
+ # @yield [value, index]
74
+ # @yieldparam [Integer] index
75
+ # @yieldreturn [self]
76
+ # @return [Enumerator]
77
+ def each_value_with_index
78
+ return to_enum(__callee__) unless block_given?
79
+
80
+ each_value.with_index{|value, index|yield value, index}
81
+ self
82
+ end
83
+
84
+ alias_method :each_with_index, :each_value_with_index
85
+
86
+ # @yield [autonym, value, index]
87
+ # @yieldparam [Symbol] autonym
88
+ # @yieldparam [Integer] index
89
+ # @yieldreturn [self]
90
+ # @return [Enumerator]
91
+ def each_pair_with_index
92
+ return to_enum(__callee__) unless block_given?
93
+
94
+ index = 0
95
+ each_pair do |autonym, value|
96
+ yield autonym, value, index
97
+ index += 1
98
+ end
99
+ self
100
+ end
101
+
102
+ # @endgroup
103
+
104
104
  end; end
@@ -1,122 +1,122 @@
1
- require_relative 'subscript'
2
-
3
- class Striuct; module InstanceMethods
4
-
5
- # @group Like Ruby's Hash
6
-
7
- alias_method :fetch, :[]
8
-
9
- def has_value?(value)
10
- @db.has_value? value
11
- end
12
-
13
- alias_method :value?, :has_value?
14
-
15
- # keep truthy only (unassign falsy member)
16
- # @yield [autonym, value]
17
- # @yieldparam [Symbol] autonym
18
- # @see #each_pair
19
- # @return [Enumerator]
20
- # @yieldreturn [self]
21
- # @yieldreturn [nil]
22
- def select!
23
- raise "can't modify frozen #{self.class}" if frozen?
24
- return to_enum(__callee__) unless block_given?
25
-
26
- modified = false
27
- each_pair do |autonym, value|
28
- unless yield autonym, value
29
- unassign autonym
30
- modified = true
31
- end
32
- end
33
-
34
- modified ? self : nil
35
- end
36
-
37
- # @see #select!
38
- # @yield [autonym, value]
39
- # @yieldparam [Symbol] autonym
40
- # @return [Enumerator]
41
- def keep_if(&block)
42
- raise "can't modify frozen #{self.class}" if frozen?
43
- return to_enum(__callee__) unless block_given?
44
-
45
- select!(&block)
46
- self
47
- end
48
-
49
- # @see #select!
50
- # keep falsy only (unassign truthy member)
51
- # @yield [autonym, value]
52
- # @yieldparam [Symbol] autonym
53
- # @return [Enumerator]
54
- def reject!
55
- raise "can't modify frozen #{self.class}" if frozen?
56
- return to_enum(__callee__) unless block_given?
57
-
58
- modified = false
59
- each_pair do |autonym, value|
60
- if yield autonym, value
61
- unassign autonym
62
- modified = true
63
- end
64
- end
65
-
66
- modified ? self : nil
67
- end
68
-
69
- # @see #reject!
70
- # @yield [autonym, value]
71
- # @yieldparam [Symbol] autonym
72
- # @return [Enumerator]
73
- def delete_if(&block)
74
- raise "can't modify frozen #{self.class}" if frozen?
75
- return to_enum(__callee__) unless block_given?
76
-
77
- reject!(&block)
78
- self
79
- end
80
-
81
- # @param [Symbol, String] name
82
- # @return [Array] e.g [autonym, value]
83
- def assoc(name)
84
- autonym = autonym_for name
85
-
86
- [autonym, self[name]]
87
- end
88
-
89
- # @return [Array] [autonym, value]
90
- def rassoc(value)
91
- each_pair.find{|_, val|val == value}
92
- end
93
-
94
- # @see Hash#flatten
95
- # @return [Array]
96
- def flatten(level=1)
97
- each_pair.to_a.flatten level
98
- end
99
-
100
- # @see #select!
101
- # @yield [autonym, value]
102
- # @yieldparam [Symbol] autonym
103
- # @return [Striuct]
104
- def select(&block)
105
- return to_enum(__callee__) unless block_given?
106
-
107
- dup.tap {|r|r.select!(&block)}
108
- end
109
-
110
- # @see #reject!
111
- # @yield [autonym, value]
112
- # @yieldparam [Symbol] autonym
113
- # @return [Striuct]
114
- def reject(&block)
115
- return to_enum(__callee__) unless block_given?
116
-
117
- dup.tap {|r|r.reject!(&block)}
118
- end
119
-
120
- # @endgroup
121
-
1
+ require_relative 'subscript'
2
+
3
+ class Striuct; module InstanceMethods
4
+
5
+ # @group Like Ruby's Hash
6
+
7
+ alias_method :fetch, :[]
8
+
9
+ def has_value?(value)
10
+ @db.has_value? value
11
+ end
12
+
13
+ alias_method :value?, :has_value?
14
+
15
+ # keep truthy only (unassign falsy member)
16
+ # @yield [autonym, value]
17
+ # @yieldparam [Symbol] autonym
18
+ # @see #each_pair
19
+ # @return [Enumerator]
20
+ # @yieldreturn [self]
21
+ # @yieldreturn [nil]
22
+ def select!
23
+ raise "can't modify frozen #{self.class}" if frozen?
24
+ return to_enum(__callee__) unless block_given?
25
+
26
+ modified = false
27
+ each_pair do |autonym, value|
28
+ unless yield autonym, value
29
+ unassign autonym
30
+ modified = true
31
+ end
32
+ end
33
+
34
+ modified ? self : nil
35
+ end
36
+
37
+ # @see #select!
38
+ # @yield [autonym, value]
39
+ # @yieldparam [Symbol] autonym
40
+ # @return [Enumerator]
41
+ def keep_if(&block)
42
+ raise "can't modify frozen #{self.class}" if frozen?
43
+ return to_enum(__callee__) unless block_given?
44
+
45
+ select!(&block)
46
+ self
47
+ end
48
+
49
+ # @see #select!
50
+ # keep falsy only (unassign truthy member)
51
+ # @yield [autonym, value]
52
+ # @yieldparam [Symbol] autonym
53
+ # @return [Enumerator]
54
+ def reject!
55
+ raise "can't modify frozen #{self.class}" if frozen?
56
+ return to_enum(__callee__) unless block_given?
57
+
58
+ modified = false
59
+ each_pair do |autonym, value|
60
+ if yield autonym, value
61
+ unassign autonym
62
+ modified = true
63
+ end
64
+ end
65
+
66
+ modified ? self : nil
67
+ end
68
+
69
+ # @see #reject!
70
+ # @yield [autonym, value]
71
+ # @yieldparam [Symbol] autonym
72
+ # @return [Enumerator]
73
+ def delete_if(&block)
74
+ raise "can't modify frozen #{self.class}" if frozen?
75
+ return to_enum(__callee__) unless block_given?
76
+
77
+ reject!(&block)
78
+ self
79
+ end
80
+
81
+ # @param [Symbol, String] name
82
+ # @return [Array] e.g [autonym, value]
83
+ def assoc(name)
84
+ autonym = autonym_for name
85
+
86
+ [autonym, self[name]]
87
+ end
88
+
89
+ # @return [Array] [autonym, value]
90
+ def rassoc(value)
91
+ each_pair.find{|_, val|val == value}
92
+ end
93
+
94
+ # @see Hash#flatten
95
+ # @return [Array]
96
+ def flatten(level=1)
97
+ each_pair.to_a.flatten level
98
+ end
99
+
100
+ # @see #select!
101
+ # @yield [autonym, value]
102
+ # @yieldparam [Symbol] autonym
103
+ # @return [Striuct]
104
+ def select(&block)
105
+ return to_enum(__callee__) unless block_given?
106
+
107
+ dup.tap {|r|r.select!(&block)}
108
+ end
109
+
110
+ # @see #reject!
111
+ # @yield [autonym, value]
112
+ # @yieldparam [Symbol] autonym
113
+ # @return [Striuct]
114
+ def reject(&block)
115
+ return to_enum(__callee__) unless block_given?
116
+
117
+ dup.tap {|r|r.reject!(&block)}
118
+ end
119
+
120
+ # @endgroup
121
+
122
122
  end; end
@@ -1,60 +1,60 @@
1
- class Striuct; module InstanceMethods
2
-
3
- # @group Use Only Inner
4
-
5
- private
6
-
7
- def __get__(name)
8
- autonym = autonym_for name
9
- value = @db[autonym]
10
-
11
- if safety_getter?(autonym) and !accept?(autonym, value)
12
- raise ::Validation::InvalidReadingError,
13
- "#{value.inspect} is deficient for #{name} in #{self.class}"
14
- end
15
-
16
- value
17
- end
18
-
19
- def __set__(name, value)
20
- raise "can't modify frozen #{self.class}" if frozen?
21
- autonym = autonym_for name
22
- raise "can't modify locked member #{name}" if lock? autonym
23
-
24
- if has_adjuster? autonym
25
- begin
26
- value = instance_exec value, &adjuster_for(autonym)
27
- rescue Exception
28
- raise ::Validation::UnmanagebleError
29
- end
30
- end
31
-
32
- if safety_setter?(autonym) and !accept?(autonym, value)
33
- raise ::Validation::InvalidWritingError,
34
- "#{value.inspect} is deficient for #{name} in #{self.class}"
35
- end
36
-
37
- if inference? autonym
38
- self.class.__send__ :__found_family__!, self, autonym, value
39
- end
40
-
41
- @db[autonym] = value
42
- rescue ::Validation::InvalidError
43
- unless /in \[\]=/ =~ caller[1].slice(/([^:]+)\z/)
44
- $!.backtrace.delete_if{|s|/#{Regexp.escape(File.dirname __FILE__)}/ =~ s}
45
- $!.backtrace.first.sub!(/([^:]+)\z/){"in `#{name}='"}
46
- end
47
-
48
- raise
49
- end
50
-
51
- # @param [Symbol] autonym
52
- def __clear__(autonym)
53
- raise "can't modify frozen #{self.class}" if frozen?
54
-
55
- @db.delete autonym
56
- end
57
-
58
- # @endgroup
59
-
60
- end; end
1
+ class Striuct; module InstanceMethods
2
+
3
+ # @group Use Only Inner
4
+
5
+ private
6
+
7
+ def __get__(name)
8
+ autonym = autonym_for name
9
+ value = @db[autonym]
10
+
11
+ if safety_getter?(autonym) and !accept?(autonym, value)
12
+ raise ::Validation::InvalidReadingError,
13
+ "#{value.inspect} is deficient for #{name} in #{self.class}"
14
+ end
15
+
16
+ value
17
+ end
18
+
19
+ def __set__(name, value)
20
+ raise "can't modify frozen #{self.class}" if frozen?
21
+ autonym = autonym_for name
22
+ raise "can't modify locked member #{name}" if lock? autonym
23
+
24
+ if has_adjuster? autonym
25
+ begin
26
+ value = instance_exec value, &adjuster_for(autonym)
27
+ rescue Exception
28
+ raise ::Validation::UnmanagebleError
29
+ end
30
+ end
31
+
32
+ if safety_setter?(autonym) and !accept?(autonym, value)
33
+ raise ::Validation::InvalidWritingError,
34
+ "#{value.inspect} is deficient for #{name} in #{self.class}"
35
+ end
36
+
37
+ if inference? autonym
38
+ self.class.__send__ :__found_family__!, self, autonym, value
39
+ end
40
+
41
+ @db[autonym] = value
42
+ rescue ::Validation::InvalidError
43
+ unless /in \[\]=/ =~ caller[1].slice(/([^:]+)\z/)
44
+ $!.backtrace.delete_if{|s|/#{Regexp.escape(File.dirname __FILE__)}/ =~ s}
45
+ $!.backtrace.first.sub!(/([^:]+)\z/){"in `#{name}='"}
46
+ end
47
+
48
+ raise
49
+ end
50
+
51
+ # @param [Symbol] autonym
52
+ def __clear__(autonym)
53
+ raise "can't modify frozen #{self.class}" if frozen?
54
+
55
+ @db.delete autonym
56
+ end
57
+
58
+ # @endgroup
59
+
60
+ end; end
@@ -1,14 +1,14 @@
1
- require 'keyvalidatable'
2
-
3
- class Striuct; module InstanceMethods
4
-
5
- extend Forwardable
6
- include KeyValidatable
7
-
8
- KeyValidatable.instance_methods.each do |feature|
9
- if specific_feature = feature.to_s.sub!('members'){'autonyms'}
10
- def_delegator :self, feature, specific_feature.to_sym
11
- end
12
- end
13
-
14
- end; end
1
+ require 'keyvalidatable'
2
+
3
+ class Striuct; module InstanceMethods
4
+
5
+ extend Forwardable
6
+ include KeyValidatable
7
+
8
+ KeyValidatable.instance_methods.each do |feature|
9
+ if specific_feature = feature.to_s.sub!('members'){'autonyms'}
10
+ def_delegator :self, feature, specific_feature.to_sym
11
+ end
12
+ end
13
+
14
+ end; end