striuct 0.6.1 → 0.9.0

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 (89) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE.txt +21 -0
  3. data/README.md +44 -60
  4. data/lib/striuct/{requirements.rb → bootstrap.rb} +6 -2
  5. data/lib/striuct/classmethods/README.md +2 -2
  6. data/lib/striuct/classmethods/adjustment.rb +77 -13
  7. data/lib/striuct/classmethods/attributes.rb +94 -92
  8. data/lib/striuct/classmethods/conflict_management.rb +109 -106
  9. data/lib/striuct/classmethods/constructor.rb +55 -65
  10. data/lib/striuct/classmethods/copy.rb +45 -43
  11. data/lib/striuct/classmethods/default.rb +23 -21
  12. data/lib/striuct/classmethods/enum.rb +46 -44
  13. data/lib/striuct/classmethods/fix.rb +23 -21
  14. data/lib/striuct/classmethods/hashdeepdupulicatable.rb +15 -15
  15. data/lib/striuct/classmethods/inner.rb +57 -55
  16. data/lib/striuct/classmethods/length.rb +12 -10
  17. data/lib/striuct/classmethods/macro.rb +99 -106
  18. data/lib/striuct/classmethods/names.rb +69 -65
  19. data/lib/striuct/classmethods/predicate.rb +131 -129
  20. data/lib/striuct/classmethods/requirements.rb +2 -2
  21. data/lib/striuct/classmethods/to_struct.rb +21 -18
  22. data/lib/striuct/classmethods/validation.rb +15 -13
  23. data/lib/striuct/instancemethods/README.md +2 -2
  24. data/lib/striuct/instancemethods/assign.rb +29 -27
  25. data/lib/striuct/instancemethods/cast.rb +28 -26
  26. data/lib/striuct/instancemethods/compare.rb +26 -24
  27. data/lib/striuct/instancemethods/default.rb +31 -29
  28. data/lib/striuct/instancemethods/delegate_class_methods.rb +35 -33
  29. data/lib/striuct/instancemethods/enum.rb +86 -84
  30. data/lib/striuct/instancemethods/getter.rb +39 -37
  31. data/lib/striuct/instancemethods/hashy.rb +99 -97
  32. data/lib/striuct/instancemethods/lock.rb +46 -44
  33. data/lib/striuct/instancemethods/object.rb +50 -48
  34. data/lib/striuct/instancemethods/requirements.rb +9 -13
  35. data/lib/striuct/instancemethods/safety.rb +11 -9
  36. data/lib/striuct/instancemethods/setter.rb +46 -45
  37. data/lib/striuct/instancemethods/to_s.rb +39 -37
  38. data/lib/striuct/instancemethods/validation.rb +46 -29
  39. data/lib/striuct/instancemethods/values.rb +50 -48
  40. data/lib/striuct/singleton_class.rb +25 -18
  41. data/lib/striuct/structs.rb +3 -3
  42. data/lib/striuct.rb +7 -3
  43. metadata +18 -209
  44. data/.gitignore +0 -35
  45. data/.travis.yml +0 -17
  46. data/.yardopts +0 -1
  47. data/Gemfile +0 -11
  48. data/MIT-LICENSE +0 -22
  49. data/Rakefile +0 -11
  50. data/benchmark/basics.rb +0 -56
  51. data/example/README.rb +0 -50
  52. data/example/example1.rb +0 -195
  53. data/example/example2.rb +0 -20
  54. data/example/see_trace.rb +0 -32
  55. data/lib/striuct/instancemethods/keyvalidatable.rb +0 -9
  56. data/striuct.gemspec +0 -45
  57. data/test/helper.rb +0 -3
  58. data/test/test_sglc-constructor.rb +0 -36
  59. data/test/test_subc-c-add_members.rb +0 -13
  60. data/test/test_subc-c-close_member.rb +0 -36
  61. data/test/test_subc-c-constructor.rb +0 -92
  62. data/test/test_subc-c-copy.rb +0 -71
  63. data/test/test_subc-c-freeze.rb +0 -36
  64. data/test/test_subc-c-inheritable.rb +0 -58
  65. data/test/test_subc-c-safety_naming.rb +0 -73
  66. data/test/test_subc-f-alias_member.rb +0 -54
  67. data/test/test_subc-f-enum.rb +0 -159
  68. data/test/test_subc-f-predicate.rb +0 -647
  69. data/test/test_subc-f-to_struct.rb +0 -61
  70. data/test/test_subc-f-validation_util.rb +0 -59
  71. data/test/test_subc-f_debug.rb +0 -39
  72. data/test/test_subc-f_name.rb +0 -525
  73. data/test/test_subc-i-accessor.rb +0 -138
  74. data/test/test_subc-i-adjuster.rb +0 -95
  75. data/test/test_subc-i-assign.rb +0 -53
  76. data/test/test_subc-i-basic.rb +0 -43
  77. data/test/test_subc-i-cast.rb +0 -27
  78. data/test/test_subc-i-compare.rb +0 -52
  79. data/test/test_subc-i-copy.rb +0 -23
  80. data/test/test_subc-i-default_value.rb +0 -129
  81. data/test/test_subc-i-freeze.rb +0 -19
  82. data/test/test_subc-i-hashlike.rb +0 -144
  83. data/test/test_subc-i-keyvalidatable.rb +0 -25
  84. data/test/test_subc-i-lock.rb +0 -40
  85. data/test/test_subc-i-must.rb +0 -30
  86. data/test/test_subc-i-to_s_family.rb +0 -26
  87. data/test/test_subc-i-validation_specific_conditions.rb +0 -297
  88. data/test/test_subc-i-validation_with_getter.rb +0 -33
  89. data/test/test_version.rb +0 -13
@@ -1,98 +1,100 @@
1
- class Striuct; module InstanceMethods
1
+ # frozen_string_literal: true
2
2
 
3
- # @group Enumerative
3
+ class Striuct
4
+ module InstanceMethods
5
+ # @group Enumerative
4
6
 
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__) { self.class.size } unless block_given?
7
+ # @yield [autonym]
8
+ # @yieldparam [Symbol] autonym - sequential under defined
9
+ # @yieldreturn [self]
10
+ # @return [Enumerator]
11
+ def each_autonym(&block)
12
+ return to_enum(__callee__) { self.class.size } unless block
11
13
 
12
- self.class.each_autonym(&block)
13
- self
14
- end
14
+ self.class.each_autonym(&block)
15
+ self
16
+ end
15
17
 
16
- alias_method :each_member, :each_autonym
17
-
18
- # @yield [value]
19
- # @yieldparam [Object] value - sequential under defined
20
- # @see #each_autonym
21
- # @yieldreturn [self]
22
- # @return [Enumerator]
23
- def each_value
24
- return to_enum(__callee__) { self.class.size } unless block_given?
25
-
26
- each_autonym{|autonym|yield _get(autonym)}
27
- end
28
-
29
- alias_method :each, :each_value
30
-
31
- # @yield [autonym, value]
32
- # @yieldparam [Symbol] autonym
33
- # @yieldparam [Object] value
34
- # @yieldreturn [self]
35
- # @return [Enumerator]
36
- # @see #each_autonym
37
- # @see #each_value
38
- def each_pair
39
- return to_enum(__callee__) { self.class.size } unless block_given?
40
-
41
- each_autonym{|autonym|yield autonym, _get(autonym)}
42
- end
18
+ alias_method :each_member, :each_autonym
43
19
 
44
- # @yield [index]
45
- # @yieldparam [Integer] index
46
- # @yieldreturn [self]
47
- # @return [Enumerator]
48
- def each_index
49
- return to_enum(__callee__) { self.class.size } unless block_given?
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__) { self.class.size } unless block_given?
50
27
 
51
- self.class.each_index{|index|yield index}
52
- self
53
- end
28
+ each_autonym { |autonym| yield _get(autonym) }
29
+ end
54
30
 
55
- # @yield [autonym, index]
56
- # @yieldparam [Symbol] autonym
57
- # @yieldparam [Integer] index
58
- # @yieldreturn [self]
59
- # @return [Enumerator]
60
- def each_autonym_with_index
61
- return to_enum(__callee__) { self.class.size } unless block_given?
31
+ alias_method :each, :each_value
62
32
 
63
- self.class.each_autonym_with_index{|autonym, index|yield autonym, index}
64
- self
65
- end
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__) { self.class.size } unless block_given?
66
42
 
67
- alias_method :each_member_with_index, :each_autonym_with_index
43
+ each_autonym { |autonym| yield autonym, _get(autonym) }
44
+ end
68
45
 
69
- # @yield [value, index]
70
- # @yieldparam [Integer] index
71
- # @yieldreturn [self]
72
- # @return [Enumerator]
73
- def each_value_with_index
74
- return to_enum(__callee__) { self.class.size } unless block_given?
46
+ # @yield [index]
47
+ # @yieldparam [Integer] index
48
+ # @yieldreturn [self]
49
+ # @return [Enumerator]
50
+ def each_index
51
+ return to_enum(__callee__) { self.class.size } unless block_given?
75
52
 
76
- each_value.with_index{|value, index|yield value, index}
77
- end
78
-
79
- alias_method :each_with_index, :each_value_with_index
80
-
81
- # @yield [autonym, value, index]
82
- # @yieldparam [Symbol] autonym
83
- # @yieldparam [Integer] index
84
- # @yieldreturn [self]
85
- # @return [Enumerator]
86
- def each_pair_with_index
87
- return to_enum(__callee__) { self.class.size } unless block_given?
88
-
89
- index = 0
90
- each_pair do |autonym, value|
91
- yield autonym, value, index
92
- index += 1
53
+ self.class.each_index { |index| yield index }
54
+ self
93
55
  end
94
- end
95
56
 
96
- # @endgroup
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__) { self.class.size } unless block_given?
97
64
 
98
- end; end
65
+ self.class.each_autonym_with_index { |autonym, index| yield autonym, index }
66
+ self
67
+ end
68
+
69
+ alias_method :each_member_with_index, :each_autonym_with_index
70
+
71
+ # @yield [value, index]
72
+ # @yieldparam [Integer] index
73
+ # @yieldreturn [self]
74
+ # @return [Enumerator]
75
+ def each_value_with_index
76
+ return to_enum(__callee__) { self.class.size } unless block_given?
77
+
78
+ each_value.with_index { |value, index| yield value, index }
79
+ end
80
+
81
+ alias_method :each_with_index, :each_value_with_index
82
+
83
+ # @yield [autonym, value, index]
84
+ # @yieldparam [Symbol] autonym
85
+ # @yieldparam [Integer] index
86
+ # @yieldreturn [self]
87
+ # @return [Enumerator]
88
+ def each_pair_with_index
89
+ return to_enum(__callee__) { self.class.size } unless block_given?
90
+
91
+ index = 0
92
+ each_pair do |autonym, value|
93
+ yield autonym, value, index
94
+ index += 1
95
+ end
96
+ end
97
+
98
+ # @endgroup
99
+ end
100
+ end
@@ -1,47 +1,49 @@
1
- class Striuct; module InstanceMethods
1
+ # frozen_string_literal: true
2
2
 
3
- # @group Getter
3
+ class Striuct
4
+ module InstanceMethods
5
+ # @group Getter
4
6
 
5
- # @param [Symbol, String, #to_sym] autonym
6
- def fetch_by_autonym(autonym)
7
- autonym = autonym.to_sym
8
- raise NameError unless autonym? autonym
7
+ # @param [Symbol, String, #to_sym] autonym
8
+ def fetch_by_autonym(autonym)
9
+ autonym = autonym.to_sym
10
+ raise NameError unless autonym?(autonym)
9
11
 
10
- _get autonym
11
- end
12
-
13
- # @param [Symbol, String, #to_sym] member
14
- def fetch_by_member(member)
15
- _get autonym_for_member(member)
16
- end
12
+ _get(autonym)
13
+ end
17
14
 
18
- # @param [Integer, #to_int] index
19
- def fetch_by_index(index)
20
- _get autonym_for_index(index)
21
- end
15
+ # @param [Symbol, String, #to_sym] member
16
+ def fetch_by_member(member)
17
+ _get(autonym_for_member(member))
18
+ end
22
19
 
23
- # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
24
- def fetch_by_key(key)
25
- _get autonym_for_key(key)
26
- end
20
+ # @param [Integer, #to_int] index
21
+ def fetch_by_index(index)
22
+ _get(autonym_for_index(index))
23
+ end
27
24
 
28
- alias_method :[], :fetch_by_key
29
- alias_method :fetch, :fetch_by_key
30
-
31
- protected
32
-
33
- # @param [Symbol] autonym - MUST already converted to native autonym
34
- def _get(autonym)
35
- value = @db[autonym]
36
-
37
- if with_safety_getter?(autonym) and !accept?(autonym, value)
38
- raise ::Validation::InvalidReadingError,
39
- "#{value.inspect} is deficient for #{autonym} in #{self.class}"
25
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
26
+ def fetch_by_key(key)
27
+ _get(autonym_for_key(key))
40
28
  end
41
29
 
42
- value
43
- end
30
+ alias_method :[], :fetch_by_key
31
+ alias_method :fetch, :fetch_by_key
32
+
33
+ protected
34
+
35
+ # @param [Symbol] autonym - MUST already converted to native autonym
36
+ def _get(autonym)
37
+ value = @db[autonym]
44
38
 
45
- # @endgroup
39
+ if with_safety_getter?(autonym) && !accept?(autonym, value)
40
+ raise InvalidReadingError,
41
+ "#{value.inspect} is deficient for #{autonym} in #{self.class}"
42
+ end
46
43
 
47
- end; end
44
+ value
45
+ end
46
+
47
+ # @endgroup
48
+ end
49
+ end
@@ -1,118 +1,120 @@
1
- class Striuct; module InstanceMethods
1
+ # frozen_string_literal: true
2
2
 
3
- # @group Like Ruby's Hash
3
+ class Striuct
4
+ module InstanceMethods
5
+ # @group Like Ruby's Hash
4
6
 
5
- def has_value?(value)
6
- @db.has_value? value
7
- end
7
+ def has_value?(value)
8
+ @db.value?(value)
9
+ end
8
10
 
9
- alias_method :value?, :has_value?
10
-
11
- # keep truthy only (unassign falsy member)
12
- # @yield [autonym, value]
13
- # @yieldparam [Symbol] autonym
14
- # @see #each_pair
15
- # @return [Enumerator]
16
- # @yieldreturn [self]
17
- # @yieldreturn [nil]
18
- def select!
19
- _check_frozen
20
- return to_enum(__callee__) unless block_given?
21
-
22
- modified = false
23
- each_pair do |autonym, value|
24
- unless yield autonym, value
25
- unassign autonym
26
- modified = true
11
+ alias_method :value?, :has_value?
12
+
13
+ # keep truthy only (unassign falsy member)
14
+ # @yield [autonym, value]
15
+ # @yieldparam [Symbol] autonym
16
+ # @see #each_pair
17
+ # @return [Enumerator]
18
+ # @yieldreturn [self]
19
+ # @yieldreturn [nil]
20
+ def select!
21
+ _check_frozen
22
+ return to_enum(__callee__) unless block_given?
23
+
24
+ modified = false
25
+ each_pair do |autonym, value|
26
+ unless yield autonym, value
27
+ unassign(autonym)
28
+ modified = true
29
+ end
27
30
  end
31
+
32
+ modified ? self : nil
28
33
  end
29
-
30
- modified ? self : nil
31
- end
32
34
 
33
- # @see #select!
34
- # @yield [autonym, value]
35
- # @yieldparam [Symbol] autonym
36
- # @return [Enumerator]
37
- def keep_if(&block)
38
- _check_frozen
39
- return to_enum(__callee__) unless block_given?
35
+ # @see #select!
36
+ # @yield [autonym, value]
37
+ # @yieldparam [Symbol] autonym
38
+ # @return [Enumerator]
39
+ def keep_if(&block)
40
+ _check_frozen
41
+ return to_enum(__callee__) unless block
40
42
 
41
- select!(&block)
42
- self
43
- end
43
+ select!(&block)
44
+ self
45
+ end
44
46
 
45
- # @see #select!
46
- # keep falsy only (unassign truthy member)
47
- # @yield [autonym, value]
48
- # @yieldparam [Symbol] autonym
49
- # @return [Enumerator]
50
- def reject!
51
- _check_frozen
52
- return to_enum(__callee__) unless block_given?
53
-
54
- modified = false
55
- each_pair do |autonym, value|
56
- if yield autonym, value
57
- unassign autonym
58
- modified = true
47
+ # @see #select!
48
+ # keep falsy only (unassign truthy member)
49
+ # @yield [autonym, value]
50
+ # @yieldparam [Symbol] autonym
51
+ # @return [Enumerator]
52
+ def reject!
53
+ _check_frozen
54
+ return to_enum(__callee__) unless block_given?
55
+
56
+ modified = false
57
+ each_pair do |autonym, value|
58
+ if yield autonym, value
59
+ unassign(autonym)
60
+ modified = true
61
+ end
59
62
  end
63
+
64
+ modified ? self : nil
60
65
  end
61
-
62
- modified ? self : nil
63
- end
64
66
 
65
- # @see #reject!
66
- # @yield [autonym, value]
67
- # @yieldparam [Symbol] autonym
68
- # @return [Enumerator]
69
- def delete_if(&block)
70
- _check_frozen
71
- return to_enum(__callee__) unless block_given?
67
+ # @see #reject!
68
+ # @yield [autonym, value]
69
+ # @yieldparam [Symbol] autonym
70
+ # @return [Enumerator]
71
+ def delete_if(&block)
72
+ _check_frozen
73
+ return to_enum(__callee__) unless block
72
74
 
73
- reject!(&block)
74
- self
75
- end
76
-
77
- # @param [Symbol, String] name
78
- # @return [Array] e.g [autonym, value]
79
- def assoc(name)
80
- autonym = autonym_for_member name
75
+ reject!(&block)
76
+ self
77
+ end
81
78
 
82
- [autonym, self[name]]
83
- end
79
+ # @param [Symbol, String] name
80
+ # @return [Array] e.g [autonym, value]
81
+ def assoc(name)
82
+ autonym = autonym_for_member(name)
84
83
 
85
- # @return [Array] [autonym, value]
86
- def rassoc(value)
87
- each_pair.find{|_, val|val == value}
88
- end
84
+ [autonym, self[name]]
85
+ end
89
86
 
90
- # @see Hash#flatten
91
- # @return [Array]
92
- def flatten(level=1)
93
- each_pair.to_a.flatten level
94
- end
87
+ # @return [Array] [autonym, value]
88
+ def rassoc(value)
89
+ each_pair.find { |_, val| val == value }
90
+ end
95
91
 
96
- # @see #select!
97
- # @yield [autonym, value]
98
- # @yieldparam [Symbol] autonym
99
- # @return [Striuct]
100
- def select(&block)
101
- return to_enum(__callee__) unless block_given?
92
+ # @see Hash#flatten
93
+ # @return [Array]
94
+ def flatten(level=1)
95
+ each_pair.to_a.flatten(level)
96
+ end
102
97
 
103
- dup.tap {|r|r.select!(&block)}
104
- end
98
+ # @see #select!
99
+ # @yield [autonym, value]
100
+ # @yieldparam [Symbol] autonym
101
+ # @return [Striuct]
102
+ def select(&block)
103
+ return to_enum(__callee__) unless block
105
104
 
106
- # @see #reject!
107
- # @yield [autonym, value]
108
- # @yieldparam [Symbol] autonym
109
- # @return [Striuct]
110
- def reject(&block)
111
- return to_enum(__callee__) unless block_given?
105
+ dup.tap { |r| r.select!(&block) }
106
+ end
112
107
 
113
- dup.tap {|r|r.reject!(&block)}
114
- end
108
+ # @see #reject!
109
+ # @yield [autonym, value]
110
+ # @yieldparam [Symbol] autonym
111
+ # @return [Striuct]
112
+ def reject(&block)
113
+ return to_enum(__callee__) unless block
115
114
 
116
- # @endgroup
115
+ dup.tap { |r| r.reject!(&block) }
116
+ end
117
117
 
118
- end; end
118
+ # @endgroup
119
+ end
120
+ end
@@ -1,53 +1,55 @@
1
- class Striuct; module InstanceMethods
1
+ # frozen_string_literal: true
2
2
 
3
- # @group Lock / Unlock
4
-
5
- # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
6
- # @return [self]
7
- def lock(key)
8
- _check_frozen
3
+ class Striuct
4
+ module InstanceMethods
5
+ # @group Lock / Unlock
9
6
 
10
- @locks[autonym_for_key(key)] = true
11
- self
12
- end
13
-
14
- # @return [self]
15
- def lock_all
16
- _check_frozen
17
-
18
- each_autonym do |autonym|
19
- @locks[autonym] = true
7
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
8
+ # @return [self]
9
+ def lock(key)
10
+ _check_frozen
11
+
12
+ @locks[autonym_for_key(key)] = true
13
+ self
20
14
  end
21
- end
22
15
 
23
- # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
24
- def locked?(key)
25
- @locks.has_key? autonym_for_key(key)
26
- end
27
-
28
- def all_locked?
29
- _autonyms.all?{|autonym|@locks.has_key? autonym}
30
- end
31
-
32
- private
16
+ # @return [self]
17
+ def lock_all
18
+ _check_frozen
33
19
 
34
- # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
35
- # @return [self]
36
- def unlock(key)
37
- _check_frozen
20
+ each_autonym do |autonym|
21
+ @locks[autonym] = true
22
+ end
23
+ end
38
24
 
39
- @locks.delete autonym_for_key(key)
40
- self
41
- end
42
-
43
- # @return [self]
44
- def unlock_all
45
- _check_frozen
25
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
26
+ def locked?(key)
27
+ @locks.key?(autonym_for_key(key))
28
+ end
46
29
 
47
- @locks.clear
48
- self
49
- end
30
+ def all_locked?
31
+ _autonyms.all? { |autonym| @locks.key?(autonym) }
32
+ end
33
+
34
+ private
35
+
36
+ # @param [Symbol, String, #to_sym, Integer, #to_int] key - name / index
37
+ # @return [self]
38
+ def unlock(key)
39
+ _check_frozen
40
+
41
+ @locks.delete(autonym_for_key(key))
42
+ self
43
+ end
44
+
45
+ # @return [self]
46
+ def unlock_all
47
+ _check_frozen
50
48
 
51
- # @endgroup
49
+ @locks.clear
50
+ self
51
+ end
52
52
 
53
- end; end
53
+ # @endgroup
54
+ end
55
+ end